[Feature] Add short jittered backoff between retries on upstream 503 (code 14 "high demand") inside generateWithRetry
想要什么,用来做什么 / 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.270account_1->503 code 14(4.5s)17:30:35.880account_2->503 code 14(4.6s, 0ms pause)17:30:40.418account_3->503 code 14(4.5s, 0ms pause)17:30:44.834account_4->503 code 14(4.4s, 0ms pause)17:30:49.495account_5->503 code 14(4.7s, 0ms pause)17:30:53.763account_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)
- Short jittered backoff on
503(code 14) before the next retry ingenerateWithRetry:- Right after
settleAttempt(afterlease.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_SWITCHESbudget across ~50–60s instead of hammering Google 6 times in 22s, riding out most temporary high-demand spikes automatically.
- Right after
- Small-pool parity (optional): For pools with fewer enabled accounts than
MAX_ACCOUNT_SWITCHES(e.g., 1–2 accounts),503is cluster-wide rather than account-specific; allowing backoff retries up toMAX_ACCOUNT_SWITCHES(without pinning a broken account) prevents 1–2 account setups from failing after a single 4s spike. - Preserve passthrough when exhausted: If all
MAX_ACCOUNT_SWITCHESattempts still return503, pass503through to the client as before (optionally with a shortRetry-Afterif upstream omitted it).
版本 / Version
v0.3.14
每人最多同时打开 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.