@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
79 lines (78 loc) • 2.44 kB
TypeScript
/**
* @module BackoffPolicy
*/
import { type BackoffPolicy, type DynamicBackoffPolicy } from "../../../backoff-policies/contracts/_module.js";
import { type ITimeSpan } from "../../../time-span/contracts/_module.js";
/**
* Configuration for the exponential backoff policy.
* The wait time grows by `multiplier` after each failed attempt until capped by
* `maxDelay`. An optional `jitter` factor randomises the delay to
* avoid thundering-herd effects when multiple clients retry simultaneously.
*
* IMPORT_PATH: `"@daiso-tech/core/backoff-policies"`
* @group Implementations
*/
export type ExponentialBackoffSettings = {
/**
* Upper bound on the computed delay. The wait time will never exceed this value.
* @default
* ```ts
* import { TimeSpan } from "@daiso-tech/core/time-span";
*
* TimeSpan.fromSeconds(60)
* ```
*/
maxDelay?: ITimeSpan;
/**
* Starting delay for the first retry. Subsequent delays grow from this base.
* @default
* ```ts
* import { TimeSpan } from "@daiso-tech/core/time-span";
*
* TimeSpan.fromMilliseconds(500)
* ```
*/
minDelay?: ITimeSpan;
/**
* Base multiplication factor applied to the delay after each retry attempt.
* Larger values produce more aggressive growth in wait times.
* @default 2
*/
multiplier?: number;
/**
* Adds randomness to the delay to avoid thundering-herd effects.
* Set to `null` to disable jitter.
* @default 0.5
*/
jitter?: number | null;
/**
* @internal
* Should only be used for testing
*/
_mathRandom?: () => number;
};
/**
* @internal
*/
export declare function resolveExponentialBackoffSettings(settings: ExponentialBackoffSettings): Required<ExponentialBackoffSettings>;
/**
* Exponential backoff policy with jitter
*
* IMPORT_PATH: `"@daiso-tech/core/backoff-policies"`
* @group Implementations
*/
export declare function exponentialBackoff(settings?: DynamicBackoffPolicy<ExponentialBackoffSettings>): BackoffPolicy;
/**
* @internal
*/
export type SerializedExponentialBackoffSettings = {
maxDelay?: number;
minDelay?: number;
multiplier?: number;
jitter?: number | null;
_mathRandom?: number;
};
/**
* @internal
*/
export declare function serializeExponentialBackoffSettings(settings: ExponentialBackoffSettings): Required<SerializedExponentialBackoffSettings>;