Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy
Rust
retry: add equal jitter to exponential backoff
Raine Virtae90e6ec4a6b8
diff --git a/src/providers/retry.test.ts b/src/providers/retry.test.tsindex 4318fd8..b7178da 100644--- a/src/providers/retry.test.ts+++ b/src/providers/retry.test.ts@@ -17,14 +17,20 @@ const silentLog = {}describe("computeBackoffDelay", () => {- it("uses exponential backoff without retry-after", () => {- expect(computeBackoffDelay(0).waitMs).toBe(RETRY_INITIAL_DELAY_MS)- expect(computeBackoffDelay(1).waitMs).toBe(RETRY_INITIAL_DELAY_MS * 2)- expect(computeBackoffDelay(2).waitMs).toBe(RETRY_INITIAL_DELAY_MS * 4)+ it("uses jittered exponential backoff without retry-after", () => {+ // equal jitter: result is in [cap/2, cap]+ for (const attempt of [0, 1, 2]) {+ const cap = RETRY_INITIAL_DELAY_MS * 2 ** attempt+ const { waitMs } = computeBackoffDelay(attempt)+ expect(waitMs).toBeGreaterThanOrEqual(cap / 2)+ expect(waitMs).toBeLessThanOrEqual(cap)+ }})it("caps exponential backoff at max delay", () => {- expect(computeBackoffDelay(20).waitMs).toBe(RETRY_MAX_DELAY_MS)+ const { waitMs } = computeBackoffDelay(20)+ expect(waitMs).toBeGreaterThanOrEqual(RETRY_MAX_DELAY_MS / 2)+ expect(waitMs).toBeLessThanOrEqual(RETRY_MAX_DELAY_MS)})it("respects numeric retry-after as seconds", () => {@@ -38,7 +44,9 @@ describe("computeBackoffDelay", () => {})it("rejects non-numeric retry-after garbage", () => {- expect(computeBackoffDelay(0, "1abc").waitMs).toBe(RETRY_INITIAL_DELAY_MS)+ const { waitMs } = computeBackoffDelay(0, "1abc")+ expect(waitMs).toBeGreaterThanOrEqual(RETRY_INITIAL_DELAY_MS / 2)+ expect(waitMs).toBeLessThanOrEqual(RETRY_INITIAL_DELAY_MS)})it("parses HTTP-date retry-after", () => {diff --git a/src/providers/retry.ts b/src/providers/retry.tsindex b42cd2b..99a9abe 100644--- a/src/providers/retry.ts+++ b/src/providers/retry.ts@@ -27,7 +27,11 @@ export function computeBackoffDelay(attempt: number, retryAfter?: string): Backo}}const exp = RETRY_INITIAL_DELAY_MS * Math.pow(RETRY_BACKOFF_FACTOR, attempt)- return { waitMs: Math.min(exp, RETRY_MAX_DELAY_MS), exceedsBudget: false }+ const capped = Math.min(exp, RETRY_MAX_DELAY_MS)+ // Equal jitter on the exponential fallback to avoid synchronized retries+ // across concurrent requests. Never jitter an explicit Retry-After.+ const jittered = capped / 2 + Math.random() * (capped / 2)+ return { waitMs: Math.round(jittered), exceedsBudget: false }}export function sleep(ms: number, signal?: AbortSignal): Promise<void> {