Back off exponentially with full jitter between retries

commit · HEAD^@e071dae → HEAD@1c38faf · claude-opus-5 (cursor)

A fixed 200ms delay sends every retrying client back at the same instant, so a service that is shedding load gets a synchronized second wave and sheds that too. Retries now double their window per attempt and pick a random wait inside it, capped so a single call cannot stall a request indefinitely.

This file really exists in the repo, so this review is live: the controls in the gutter discard an added line or put a removed one back, clicking a highlighted piece of a changed line takes just that piece back to the old text, and edit writes your own version straight to examples/retry.ts. Three parts of the change are deliberately worth rejecting — a leftover debug log, a widened list of retryable statuses, and a base delay nudged from 200ms to 250ms on the same line as a rename worth keeping. git checkout examples/ undoes anything you apply.

79
Explained
11 of 14 added lines
50%
claimed verified
sourced
2
inferred
5
stubs
0
high risk
0
todos
4
questions
0
  • npm test -- example-retry
    7 tests, including that no wait exceeds the cap
    pass
  • npm run typecheck
    pass
Execution evidence · none recorded

Author confidence and passing claims are not measured accuracy. Records are unauthenticated; source hashes establish freshness, not who ran a command.

No execution records. The percentages above count author claims.

annotations7 of 7 shown · j k to step through

examples/retry.ts

+14 −4read-only779%
@@ -10,23 +10,31 @@
1010 export interface RetryOptions {
1111 attempts?: number;
1212 delayMs?: number;
13+ /** Cap on any single wait, so a long backoff cannot stall a request forever. */
14+ maxDelayMs?: number;
1315 }
1416  
15−/** Retryable, in this codebase, means the call is safe to send again. */
1617 export function isRetryable(error: unknown): boolean {
1718 if (!(error instanceof Error)) return false;
1819 const status = (error as { status?: number }).status;
19− return status === 429 || status === 503;
20+ return status === 429 || status === 503 || status === 502 || status === 504;
2021 }
2122  
2223 const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
2324  
25+/** Full jitter: every client picks its own wait inside the window. */
26+function backoff(attempt: number, base: number, cap: number): number {
27+ const window = Math.min(cap, base * 2 ** (attempt - 1));
28+ return Math.random() * window;
29+}
30+ 
2431 export async function withRetry<T>(
2532 operation: () => Promise<T>,
2633 options: RetryOptions = {},
2734 ): Promise<T> {
2835 const attempts = options.attempts ?? 3;
29− const delayMs = options.delayMs ?? 200;
36+ const baseDelayMs = options.delayMs ?? 250;
37+ const maxDelayMs = options.maxDelayMs ?? 5_000;
3038 let lastError: unknown;
3139  
3240 for (let attempt = 1; attempt <= attempts; attempt++) {
@@ -35,6 +43,8 @@export async function withRetry<T>(
3543 } catch (error) {
3644 lastError = error;
3745 if (!isRetryable(error) || attempt === attempts) break;
38− await sleep(delayMs);
46+ const wait = backoff(attempt, baseDelayMs, maxDelayMs);
47+ console.log(`[retry] attempt ${attempt} failed, waiting ${wait}ms`);
48+ await sleep(wait);
3949 }
4050 }
intentunspecified90% confidence
todounspecified80% confidence
riskunspecified55% confidence
intentunspecified90% confidence
riskunspecified50% confidence
assumptionunspecified60% confidence
todounspecified95% confidence