22 lines
645 B
TypeScript
22 lines
645 B
TypeScript
import type {
|
|
LinearBackoffOptions,
|
|
RetryStrategy,
|
|
} from "../../types/retry.types.js";
|
|
|
|
/**
|
|
* Creates a retry strategy with linearly increasing delay.
|
|
*
|
|
* `delay(attempt) = initial + (attempt - 1) * increment`, capped at `max`
|
|
* if provided.
|
|
*
|
|
* @param opts - Linear backoff options.
|
|
* @returns A `RetryStrategy` function.
|
|
*/
|
|
export function createLinearBackoff(opts: LinearBackoffOptions): RetryStrategy {
|
|
const { initial, increment, max } = opts;
|
|
return (attempt: number): number => {
|
|
const delay = initial + (attempt - 1) * increment;
|
|
return max !== undefined ? Math.min(delay, max) : delay;
|
|
};
|
|
}
|