MCPcopy Create free account
hub / github.com/pollinations/pollinations / exponentialBackoffDelay

Function exponentialBackoffDelay

shared/util.ts:39–63  ·  view source on GitHub ↗
(
    attempt: number,
    options: ExponentialBackoffOptions = {},
)

Source from the content-addressed store, hash-verified

37};
38
39export function exponentialBackoffDelay(
40 attempt: number,
41 options: ExponentialBackoffOptions = {},
42): number {
43 const {
44 minDelay = 100,
45 maxDelay = 10000,
46 maxAttempts = 5,
47 jitter = 0.25,
48 } = options;
49
50 if (attempt === 0) return 0;
51
52 const base = (maxDelay / minDelay) ** (1 / (maxAttempts - 1));
53 const delay = minDelay * base ** (attempt - 1);
54
55 if (jitter > 0) {
56 const jitterRange = delay * jitter;
57 const jitterOffset = jitterRange * (Math.random() * 2 - 1);
58 return delay + jitterOffset;
59 }
60
61 // return clamped delay
62 return Math.max(minDelay, Math.min(maxDelay, delay));
63}
64
65const resetColor = "\x1b[0m";
66

Callers 3

retryWithBackoffFunction · 0.90
delayForRetryFunction · 0.90
events.test.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected