options.d.ts 2.2 KB

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