UNPKG

@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.

35 lines 1.29 kB
/** * @module Async */ import { callInvokable, isInvokable, TimeSpan, } from "../../../utilities/_module-exports.js"; import { withJitter } from "../../../async/backof-policies/_shared.js"; /** * Exponential backoff policy with jitter * * IMPORT_PATH: `"@daiso-tech/core/async"` * @group BackoffPolicies */ export function exponentialBackoffPolicy(settings = {}) { return (attempt, error) => { if (isInvokable(settings)) { const dynamicSettings = callInvokable(settings, error); if (dynamicSettings === undefined) { settings = {}; } else { settings = dynamicSettings; } } let { maxDelay = 60_000, minDelay = 1_000 } = settings; if (maxDelay instanceof TimeSpan) { maxDelay = maxDelay.toMilliseconds(); } if (minDelay instanceof TimeSpan) { minDelay = minDelay.toMilliseconds(); } const { multiplier = 2, jitter = 0.5, _mathRandom = Math.random, } = settings; const exponential = Math.min(maxDelay, minDelay * Math.pow(multiplier, attempt)); return TimeSpan.fromMilliseconds(withJitter(jitter, exponential, _mathRandom)); }; } //# sourceMappingURL=exponential-backoff-policy.js.map