MCPcopy Create free account
hub / github.com/ConsortiumAI/Consortium / createBackoff

Function createBackoff

packages/consortium-cli/src/utils/time.ts:12–39  ·  view source on GitHub ↗
(
    opts?: {
        onError?: (e: any, failuresCount: number) => void,
        minDelay?: number,
        maxDelay?: number,
        maxFailureCount?: number
    })

Source from the content-addressed store, hash-verified

10export type BackoffFunc = <T>(callback: () => Promise<T>) => Promise<T>;
11
12export function createBackoff(
13 opts?: {
14 onError?: (e: any, failuresCount: number) => void,
15 minDelay?: number,
16 maxDelay?: number,
17 maxFailureCount?: number
18 }): BackoffFunc {
19 return async <T>(callback: () => Promise<T>): Promise<T> => {
20 let currentFailureCount = 0;
21 const minDelay = opts && opts.minDelay !== undefined ? opts.minDelay : 250;
22 const maxDelay = opts && opts.maxDelay !== undefined ? opts.maxDelay : 1000;
23 const maxFailureCount = opts && opts.maxFailureCount !== undefined ? opts.maxFailureCount : 50;
24 while (true) {
25 try {
26 return await callback();
27 } catch (e) {
28 if (currentFailureCount < maxFailureCount) {
29 currentFailureCount++;
30 }
31 if (opts && opts.onError) {
32 opts.onError(e, currentFailureCount);
33 }
34 let waitForRequest = exponentialBackoffDelay(currentFailureCount, minDelay, maxDelay, maxFailureCount);
35 await delay(waitForRequest);
36 }
37 }
38 };
39}
40
41export let backoff = createBackoff();

Callers 1

time.tsFile · 0.85

Calls 2

exponentialBackoffDelayFunction · 0.85
delayFunction · 0.85

Tested by

no test coverage detected