|
| 1 | +/** |
| 2 | + * Type of jitter to apply to the delay. |
| 3 | + * - `"none"`: no jitter is applied |
| 4 | + * - `"full"`: full jitter is applied (random value between `0` and `delay`) |
| 5 | + */ |
1 | 6 | export type JitterType = "none" | "full";
|
2 | 7 |
|
3 | 8 | export type BackoffOptions = Partial<IBackOffOptions>;
|
4 | 9 |
|
5 | 10 | export interface IBackOffOptions {
|
| 11 | + /** |
| 12 | + * Decides whether the `startingDelay` should be applied before the first call. |
| 13 | + * If `false`, the first call will occur without a delay. |
| 14 | + * @defaultValue `false` |
| 15 | + */ |
6 | 16 | delayFirstAttempt: boolean;
|
| 17 | + /** |
| 18 | + * Decides whether a [jitter](https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/) |
| 19 | + * should be applied to the delay. Possible values are `"full"` and `"none"`. |
| 20 | + * @defaultValue `"none"` |
| 21 | + */ |
7 | 22 | jitter: JitterType;
|
| 23 | + /** |
| 24 | + * The maximum delay, in milliseconds, between two consecutive attempts. |
| 25 | + * @defaultValue `Infinity` |
| 26 | + */ |
8 | 27 | maxDelay: number;
|
| 28 | + /** |
| 29 | + * The maximum number of times to attempt the function. |
| 30 | + * Must be at least `1`. |
| 31 | + * @defaultValue `10` |
| 32 | + */ |
9 | 33 | numOfAttempts: number;
|
| 34 | + /** |
| 35 | + * The `retry` function can be used to run logic after every failed attempt (e.g. logging a message, |
| 36 | + * assessing the last error, etc.). |
| 37 | + * It is called with the last error and the upcoming attempt number. |
| 38 | + * Returning `true` will retry the function as long as the `numOfAttempts` has not been exceeded. |
| 39 | + * Returning `false` will end the execution. |
| 40 | + * @defaultValue a function that always returns `true`. |
| 41 | + * @param e The last error thrown by the function. |
| 42 | + * @param attemptNumber The upcoming attempt number. |
| 43 | + * @returns `true` to retry the function, `false` to end the execution |
| 44 | + */ |
10 | 45 | retry: (e: any, attemptNumber: number) => boolean | Promise<boolean>;
|
| 46 | + /** |
| 47 | + * The delay, in milliseconds, before executing the function for the first time. |
| 48 | + * @defaultValue `100` |
| 49 | + */ |
11 | 50 | startingDelay: number;
|
| 51 | + /** |
| 52 | + * The `startingDelay` is multiplied by the `timeMultiple` to increase the delay between reattempts. |
| 53 | + * @defaultValue `2` |
| 54 | + */ |
12 | 55 | timeMultiple: number;
|
13 | 56 | }
|
14 | 57 |
|
|
0 commit comments