From 798ccdf32cefdd037bb09e6aaf26f8e6dea0d97f Mon Sep 17 00:00:00 2001 From: Harvmaster Date: Sat, 18 Jul 2026 13:53:04 +0000 Subject: [PATCH] Relocate statics below constructor --- source/exponential-backoff.ts | 94 +++++++++++++++++------------------ 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/source/exponential-backoff.ts b/source/exponential-backoff.ts index f943344..47259bc 100644 --- a/source/exponential-backoff.ts +++ b/source/exponential-backoff.ts @@ -10,6 +10,31 @@ import { ExponentialBackoffStoppedRetriesError, ExponentialBackoffMaxRetriesHitE * The growth rate is the factor by which the delay increases with each attempt. */ export class ExponentialBackoff { + private readonly options: ExponentialBackoffOptions; + + /** + * Creates a new exponential-backoff instance. + * + * Unspecified options use the defaults listed below. + * + * @param options - Exponential-backoff configuration overrides. + * @param options.maxDelay - Maximum delay between retries. Default: `10_000` ms. + * @param options.maxAttempts - Maximum number of attempts; `0` retries indefinitely. Default: `10`. + * @param options.baseDelay - Delay used as the basis for the first retry. Default: `1_000` ms. + * @param options.growthRate - Multiplier applied to the delay after each attempt. Default: `2`. + * @param options.jitter - Maximum proportional reduction subtracted from each delay (0–1). Default: `0.1`. + */ + constructor(options: Partial = {}) { + this.options = { + maxDelay: 10_000, + maxAttempts: 10, + baseDelay: 1_000, + growthRate: 2, + jitter: 0.1, + ...options, + }; + } + /** * Create a new ExponentialBackoff instance * @@ -43,29 +68,31 @@ export class ExponentialBackoff { return backoff.run(taskFn, onError); } - private readonly options: ExponentialBackoffOptions; - /** - * Creates a new exponential-backoff instance. + * Calculate the delay before we should attempt to retry * - * Unspecified options use the defaults listed below. - * - * @param options - Exponential-backoff configuration overrides. - * @param options.maxDelay - Maximum delay between retries. Default: `10_000` ms. - * @param options.maxAttempts - Maximum number of attempts; `0` retries indefinitely. Default: `10`. - * @param options.baseDelay - Delay used as the basis for the first retry. Default: `1_000` ms. - * @param options.growthRate - Multiplier applied to the delay after each attempt. Default: `2`. - * @param options.jitter - Maximum proportional reduction subtracted from each delay (0–1). Default: `0.1`. + * @param options - The configuration for the exponential backoff + * @param attempt - The current attempt number + * @returns The time in milliseconds before another attempt should be made */ - constructor(options: Partial = {}) { - this.options = { - maxDelay: 10_000, - maxAttempts: 10, - baseDelay: 1_000, - growthRate: 2, - jitter: 0.1, - ...options, - }; + public static calculateDelay(options: ExponentialBackoffOptions, attempt: number): number { + // Get the power of the growth rate + const power = options.growthRate ** attempt; + + // Get the delay before jitter or limit + const rawDelay = options.baseDelay * power; + + // Cap the delay to the maximum. Do this before the jitter so jitter does not become larger than delay + const cappedDelay = Math.min(rawDelay, options.maxDelay); + + // Get a random number for the amount to "jitter" the delay by + const jitterAmount = Math.random(); + + // Calculate the jitter + const jitter = jitterAmount * options.jitter * cappedDelay; + + // Subtract the jitter from the delay + return cappedDelay - jitter; } /** @@ -142,33 +169,6 @@ export class ExponentialBackoff { // We completed the loop without ever succeeding. Throw an ExponentialBackoffMaxRetriesHitError with all the errors we got throw new ExponentialBackoffMaxRetriesHitError(errors); } - - /** - * Calculate the delay before we should attempt to retry - * - * @param options - The configuration for the exponential backoff - * @param attempt - The current attempt number - * @returns The time in milliseconds before another attempt should be made - */ - public static calculateDelay(options: ExponentialBackoffOptions, attempt: number): number { - // Get the power of the growth rate - const power = options.growthRate ** attempt; - - // Get the delay before jitter or limit - const rawDelay = options.baseDelay * power; - - // Cap the delay to the maximum. Do this before the jitter so jitter does not become larger than delay - const cappedDelay = Math.min(rawDelay, options.maxDelay); - - // Get a random number for the amount to "jitter" the delay by - const jitterAmount = Math.random(); - - // Calculate the jitter - const jitter = jitterAmount * options.jitter * cappedDelay; - - // Subtract the jitter from the delay - return cappedDelay - jitter; - } } export type ExponentialBackoffOptions = {