AIStudio2API icon indicating copy to clipboard operation
AIStudio2API copied to clipboard

[Feature] Add short jittered backoff between retries on upstream 503 (code 14 "high demand") inside generateWithRetry

Open photobrand opened this issue 1 week ago • 1 comments

想要什么,用来做什么 / What you want and what for

Problem & Observed Evidence (v0.3.14)

Note: Unlike #202 / ProtocolLab-public#15, this is not asking for per-account cooldowns on 503 or bypassing MAX_ACCOUNT_SWITCHES. Upstream HTTP 503, code 14: This model is currently experiencing high demand. Spikes in demand are usually temporary is clearly an upstream cluster spike, not an account defect.

The problem in v0.3.14 is that generateWithRetry (internal/app/runtime.go:2403-2422) fires every retry attempt immediately with 0ms delay. Because Google's 503 fails fast (~4.5s), all MAX_ACCOUNT_SWITCHES attempts are burned inside a single 20–25s demand spike before the spike clears.

Real sanitized trace from v0.3.14 (gemini-4-hydrogen, MAX_ACCOUNT_SWITCHES=5):

  • 17:30:31.270 account_1 -> 503 code 14 (4.5s)
  • 17:30:35.880 account_2 -> 503 code 14 (4.6s, 0ms pause)
  • 17:30:40.418 account_3 -> 503 code 14 (4.5s, 0ms pause)
  • 17:30:44.834 account_4 -> 503 code 14 (4.4s, 0ms pause)
  • 17:30:49.495 account_5 -> 503 code 14 (4.7s, 0ms pause)
  • 17:30:53.763 account_6 -> 503 code 14 (4.3s) -> request fails after just 22s total.

On a separate request 7 minutes earlier (17:23:18–17:23:41), 5 attempts failed over 23s and attempt 6 succeeded (200 OK); when attempts are spread across ~40–60s, temporary spikes resolve cleanly without interrupting the user's turn.

Proposed Minimal Solution (No New Flags, No Account Cooldowns)

  1. Short jittered backoff on 503 (code 14) before the next retry in generateWithRetry:
    • Right after settleAttempt (after lease.Release() has already freed the account slot and Camoufox tab, so zero resources are held during the wait), sleep with short capped exponential backoff + jitter (e.g. 1s -> 2s -> 4s -> 8s -> 15s, selectable on <-requestCtx.Done() so client cancellation returns immediately).
    • This spreads the exact same MAX_ACCOUNT_SWITCHES budget across ~50–60s instead of hammering Google 6 times in 22s, riding out most temporary high-demand spikes automatically.
  2. Small-pool parity (optional): For pools with fewer enabled accounts than MAX_ACCOUNT_SWITCHES (e.g., 1–2 accounts), 503 is cluster-wide rather than account-specific; allowing backoff retries up to MAX_ACCOUNT_SWITCHES (without pinning a broken account) prevents 1–2 account setups from failing after a single 4s spike.
  3. Preserve passthrough when exhausted: If all MAX_ACCOUNT_SWITCHES attempts still return 503, pass 503 through to the client as before (optionally with a short Retry-After if upstream omitted it).

版本 / Version

v0.3.14

photobrand avatar Nov 22 '26 15:11 photobrand

每人最多同时打开 3 个 Issue,这个 Issue 已自动关闭。请等已有的 Issue 处理完再提,或把内容补充到已有的 Issue 里。

Each user can have at most 3 open issues at a time, so this one was closed automatically. Please wait until your existing issues are resolved, or add this to one of them.

github-actions[bot] avatar Nov 22 '26 15:11 github-actions[bot]