Files
suaveplan/packages/foundation/flow/retry/src/strategies/linear/linear.ts
2026-05-27 23:47:53 +01:00

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;
};
}