UNPKG

dash-core

Version:

A foundational toolkit of types, collections, services, and architectural patterns designed to accelerate application development.

32 lines (31 loc) 1.59 kB
import { TimeSpan } from 'dash-core'; /** * Implements exponential backoff with optional jitter for retrying operations. */ export declare class ExponentialBackoff { private readonly options; /** * Creates an instance of the ExponentialBackoff class with the specified options. * @param {ExponentialBackoffOptions} options - Configuration options for the backoff mechanism. * @param {TimeSpan} options.initialDelay - The initial delay before the first retry attempt. * @param {TimeSpan} options.maxDelay - The maximum delay allowed between retries. * @param {number} options.factor - The factor by which the delay increases after each failure. * @param {TimeSpan} [options.jitter] - An optional jitter value that is added randomly to the delay (optional). */ constructor(options: ExponentialBackoffOptions); /** * Executes the given operation and applies exponential backoff with retries. * The operation will be retried until it succeeds or the maximum delay is reached. * @param {() => Promise<TResult>} operation - The asynchronous operation to execute and retry in case of failure. * @returns {Promise<TResult>} A promise that resolves with the result of the operation if it succeeds, or rejects with the last error if all attempts fail. */ execute<TResult>(operation: () => Promise<TResult>): Promise<TResult>; private attempt; private getUpdatedDelay; } export type ExponentialBackoffOptions = { initialDelay: TimeSpan; maxDelay: TimeSpan; factor: number; jitter?: TimeSpan; };