MCPcopy Create free account
hub / github.com/sourcebot-dev/sourcebot / fetchWithRetry

Function fetchWithRetry

packages/web/src/lib/utils.ts:637–657  ·  view source on GitHub ↗
(
    input: RequestInfo | URL,
    init?: RequestInit,
    { retries = 3, backoffMs = 1000 }: { retries?: number; backoffMs?: number } = {},
)

Source from the content-addressed store, hash-verified

635 status >= 500 || status === 408 || status === 429;
636
637export const fetchWithRetry = async (
638 input: RequestInfo | URL,
639 init?: RequestInit,
640 { retries = 3, backoffMs = 1000 }: { retries?: number; backoffMs?: number } = {},
641): Promise<Response> => {
642 for (let attempt = 0; attempt <= retries; attempt++) {
643 try {
644 const response = await fetch(input, init);
645 if (response.ok || !isRetryableStatus(response.status) || attempt === retries) {
646 return response;
647 }
648 } catch (error) {
649 if (attempt === retries) {
650 throw error;
651 }
652 }
653 await sleep(backoffMs * Math.pow(2, attempt));
654 }
655 // Unreachable, but TypeScript needs it
656 throw new Error('fetchWithRetry: exhausted retries');
657}

Callers 1

requestLighthouseFunction · 0.90

Calls 2

isRetryableStatusFunction · 0.85
sleepFunction · 0.85

Tested by

no test coverage detected