// Retries a function up to a maximum number of times, with exponential backoff. // Current settings, max total wait time is ~10 seconds. export async function retryBackoff( name: string, fn: () => Promise, maxRetries = 5, backoffFn: (i: number) => number = (i) => Math.min(2000, 250 * 2 ** i), ): Promise { for (let i = 1; ; i++) { try { return await fn(); } catch (e) { if (i <= maxRetries) { const sleepMs = backoffFn(i); console.error( `[RETRY] ${name} sleeping ${sleepMs}ms after try ${i}, error:`, e, ); await new Promise((r) => setTimeout(r, sleepMs)); } else { console.warn(`[RETRY] ${name} QUITTING after try ${i}, error: ${e}`); break; } } } // TODO: add performance logging throw new Error(`too many retries: ${name}`); }