|
| 1 | +/** Options for configuring a {@link BackoffPolicy}. */ |
| 2 | +export interface BackoffPolicyOptions { |
| 3 | + /** Initial delay in milliseconds; defaults to 1000. */ |
| 4 | + initial?: number; |
| 5 | + |
| 6 | + /** Maximum delay in milliseconds; defaults to 60000. */ |
| 7 | + maximum?: number; |
| 8 | + |
| 9 | + /** |
| 10 | + * Factor by which the delay is multiplied after each retry. The value must |
| 11 | + * be greater or equal to 1. If not, it defaults to 2. |
| 12 | + */ |
| 13 | + factor?: number; |
| 14 | +} |
| 15 | + |
| 16 | +// Random number generation, wrapped in an object for testability. |
| 17 | +export const rand = { |
| 18 | + // Returns a random integer in [0, n). |
| 19 | + int(n: number): number { |
| 20 | + return Math.floor(Math.random() * n); |
| 21 | + }, |
| 22 | +}; |
| 23 | + |
| 24 | +/** |
| 25 | + * BackoffPolicy implements an exponential backoff policy. The delay between |
| 26 | + * retries is randomly computed between 0 and the "exponential delay" as |
| 27 | + * recommended in [Exponential Backoff And Jitter]. The retry delay starts from |
| 28 | + * initial and grows exponentially by factor at every retry. The maximum retry |
| 29 | + * delay is capped by maximum. |
| 30 | + * |
| 31 | + * There is no parameter to limit the number of retries. This is intended as |
| 32 | + * such logic should be implemented upstream (e.g. in a Retrier). |
| 33 | + * |
| 34 | + * [Exponential Backoff And Jitter]: https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/ |
| 35 | + */ |
| 36 | +export class BackoffPolicy { |
| 37 | + /** Initial delay in milliseconds. */ |
| 38 | + readonly initial: number; |
| 39 | + |
| 40 | + /** Maximum delay in milliseconds. */ |
| 41 | + readonly maximum: number; |
| 42 | + |
| 43 | + /** Factor by which the delay is multiplied after each retry. */ |
| 44 | + readonly factor: number; |
| 45 | + |
| 46 | + // Current delay before the next retry. |
| 47 | + private current: number; |
| 48 | + |
| 49 | + constructor(options?: BackoffPolicyOptions) { |
| 50 | + let initial = options?.initial ?? 1000; // Default initial delay of 1 second. |
| 51 | + const maximum = options?.maximum ?? 60000; // Default maximum delay of 60 seconds. |
| 52 | + |
| 53 | + if (initial > maximum) { |
| 54 | + // Initial cannot be greater than maximum. |
| 55 | + initial = maximum; |
| 56 | + } |
| 57 | + |
| 58 | + this.initial = initial; |
| 59 | + this.maximum = maximum; |
| 60 | + this.factor = |
| 61 | + options?.factor !== undefined && options.factor >= 1 ? options.factor : 2; |
| 62 | + this.current = this.initial; |
| 63 | + } |
| 64 | + |
| 65 | + /** Returns a random delay in [0, current] and grows the current delay. */ |
| 66 | + delay(): number { |
| 67 | + // Random duration in the range [0, this.current]. |
| 68 | + const d = rand.int(this.current + 1); |
| 69 | + |
| 70 | + // Grow delay for the next call. |
| 71 | + this.current = Math.min(this.current * this.factor, this.maximum); |
| 72 | + |
| 73 | + return d; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/** Retrier defines a retry behavior. */ |
| 78 | +export interface Retrier { |
| 79 | + /** |
| 80 | + * Returns the delay in milliseconds before the next retry, or undefined if |
| 81 | + * the error is not retriable. Implementations should assume that the given |
| 82 | + * error is never undefined. |
| 83 | + */ |
| 84 | + isRetriable(err: Error): number | undefined; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Returns a Retrier that retries based on the isRetriable predicate and relies |
| 89 | + * on an internal backoff policy to decide how long to wait between retries. |
| 90 | + * |
| 91 | + * Important: the retrier has its own backoff policy which cannot be trivially |
| 92 | + * reset by design. Users who need to reset the backoff policy should rather |
| 93 | + * create a new retrier. |
| 94 | + */ |
| 95 | +export function retryOn( |
| 96 | + options: BackoffPolicyOptions, |
| 97 | + isRetriable: (err: Error) => boolean |
| 98 | +): Retrier { |
| 99 | + const bp = new BackoffPolicy(options); |
| 100 | + return { |
| 101 | + isRetriable(err: Error): number | undefined { |
| 102 | + if (!isRetriable(err)) { |
| 103 | + return undefined; |
| 104 | + } |
| 105 | + return bp.delay(); |
| 106 | + }, |
| 107 | + }; |
| 108 | +} |
0 commit comments