UNPKG

durable-utils

Version:

Utilities for Cloudflare Durable Objects and Workers, including SQL migrations, sharding and retry utilities, and more.

71 lines (70 loc) 3.99 kB
/** * Returns the number of milliseconds to wait before retrying a request. * See the "Full Jitter" approach in https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/. * @param attempt The number of attempts so far. * @param baseDelayMs Number of milliseconds to use as multiplier for the exponential backoff. * @param maxDelayMs Maximum number of milliseconds to wait. * @returns Milliseconds to wait before retrying. */ export declare function jitterBackoff(attempt: number, baseDelayMs: number, maxDelayMs: number): number; export type TryNOptions = { /** * @param err Error thrown by the function. * @param nextAttempt Number of next attempt to make. * @returns Returns true if the error and nextAttempt number is retryable. */ isRetryable?: (err: unknown, nextAttempt: number) => boolean; /** * Number of milliseconds to use as multiplier for the exponential backoff. */ baseDelayMs?: number; /** * Maximum number of milliseconds to wait. */ maxDelayMs?: number; /** * If true, logs the error and attempt number to the console. */ verbose?: boolean; }; /** * @param n Number of total attempts to make. * @param fn The function to call for each attempt. Receives the attempt number. * @param options The options for the retry strategy. * @param options.isRetryable The function to call to determine if the error is retryable. Receives the error and the next attempt number. * @param options.baseDelayMs Number of milliseconds to use as multiplier for the exponential backoff. * @param options.maxDelayMs Maximum number of milliseconds to wait. * @param options.verbose If true, logs the error and attempt number to the console. * @returns The result of the `fn` function or propagates the last error thrown once `isRetryable` returns false or all retries failed. */ export declare function tryN<T>(n: number, fn: (attempt: number) => Promise<T>, options?: TryNOptions): Promise<T>; /** * @deprecated Use the overload with 3rd argument being `options`. This function overload will be removed in the next version. * @param n Number of total attempts to make. * @param fn The function to call for each attempt. Receives the attempt number. * @param isRetryable The function to call to determine if the error is retryable. Receives the error and the next attempt number. * @param options The options for the retry strategy. * @param options.baseDelayMs Number of milliseconds to use as multiplier for the exponential backoff. * @param options.maxDelayMs Maximum number of milliseconds to wait. * @param options.verbose If true, logs the error and attempt number to the console. * @returns The result of the `fn` function or propagates the last error thrown once `isRetryable` returns false or all retries failed. */ export declare function tryN<T>(n: number, fn: (attempt: number) => Promise<T>, isRetryable: (err: unknown, nextAttempt: number) => boolean, /** * @deprecated Use the `options` 3rd argument instead. */ options?: TryNOptions): Promise<T>; /** * @param fn The function to call for each attempt. Receives the attempt number. * @param isRetryable The function to call to determine if the error is retryable. Receives the error and the next attempt number. * @param options The options for the retry strategy. * @param options.baseDelayMs Number of milliseconds to use as multiplier for the exponential backoff. * @param options.maxDelayMs Maximum number of milliseconds to wait. * @param options.verbose If true, logs the error and attempt number to the console. * @returns The result of the `fn` function or propagates the last error thrown once `isRetryable` returns false or all retries failed. */ export declare function tryWhile<T>(fn: (attempt: number) => Promise<T>, isRetryable: (err: unknown, nextAttempt: number) => boolean, options?: { baseDelayMs?: number; maxDelayMs?: number; verbose?: boolean; }): Promise<T>;