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.28 kB
/** * @module Async */ import { callInvokable, isInvokable, TimeSpan, } from "../../../utilities/_module-exports.js"; import { withJitter } from "../../../async/backof-policies/_shared.js"; /** * Polynomial backoff policy with jitter * * IMPORT_PATH: `"@daiso-tech/core/async"` * @group BackoffPolicies */ export function polynomialBackoffPolicy(settings = {}) { return (attempt, error) => { if (isInvokable(settings)) { const dynamicSettings = callInvokable(settings, error); if (dynamicSettings === undefined) { settings = {}; } else { settings = dynamicSettings; } } let { maxDelay = 6000, minDelay = 1_000 } = settings; if (maxDelay instanceof TimeSpan) { maxDelay = maxDelay.toMilliseconds(); } if (minDelay instanceof TimeSpan) { minDelay = minDelay.toMilliseconds(); } const { degree = 2, jitter = 0.5, _mathRandom = Math.random, } = settings; const polynomial = Math.min(maxDelay, minDelay * Math.pow(attempt, degree)); return TimeSpan.fromMilliseconds(withJitter(jitter, polynomial, _mathRandom)); }; } //# sourceMappingURL=polynomial-backoff-policy.js.map