Skip to content

fix: stop anonymous lane from locking up after quota+network change - #59

Merged
jasonxu114514 merged 2 commits into
jasonxu114514:mainfrom
cheerc:fix/anonymous-cooldown-lockup
Oct 15, 2026
Merged

jasonxu114514 merged 2 commits into
jasonxu114514:mainfrom
cheerc:fix/anonymous-cooldown-lockup

Conversation

@cheerc

@cheerc cheerc commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

問題:換 IP 後持續 401,必須重啟 process 才恢復

使用場景:免費模型額度用完 → 上游回 401。同時透過更換網路環境(換 IP)來恢復額度
(anonymous 免費通道是按 IP 限流的)。但網路明明已通、新 IP 也有額度了,
opencode2api 仍然持續對外回 401,只有把 process 整個 kill 重開才會恢復 200。

根因(兩個記憶體狀態,症狀是 401 卡死)

本站典型配置:anonymous:true + 唯一 proxy direct + 佔位 zen key。
請求先走 anonymous 通道,失敗才 fallback 到 key 通道(doUpstreamTiers),
而佔位 key 註定永遠 401。所以 anonymous 通道一旦被跳過,對外就是穩定的 401。

  1. Retry-After 無上限(pool.go 兩個 MarkFailure):
    上游 401/403/429/5xx 都會續冷卻,基底 15s、指數退避最高 8 倍(120s)。
    但 Retry-After header 會無條件覆蓋且無上限——免費額度用完時上游常帶一個
    很大的 Retry-After,冷卻直接到數小時後。
  2. anonymous lane 沒有 fallback(anonymousCursor.Next):
    冷卻中的 anonymous 節點直接回 nil、整條 lane 被跳過(key 池還有
    earliest-expiry fallback)。換了新 IP 本該恢復,但新 IP 的第一次嘗試根本沒
    機會發生——lane 選擇在冷卻檢查這步就回頭了,只走必 401 的佔位 key。
  3. proxy 恢復不清 anonymous 冷卻(restoreProxy):
    斷線期 transport error 把唯一 proxy 標 unhealthy,anonymous lane 要求 healthy
    被整段跳過;恢復時只清 key 節點冷卻(nodePool.RestoreProxy),anonymous
    的只能等過期。

重啟能修,只是因為它清空了這兩個記憶體狀態。

本 PR 的修改

  • pool.go:兩個 MarkFailure 的 Retry-After 上限為 base * 8
    (與既有指數退避封頂一致,預設配置下即 120s)。只縮短超長 Retry-After,
    不影響正常退避語意。
  • pool.go:新增 anonymousPool.RestoreProxy,鏡像 nodePool.RestoreProxy
    清除同 proxy 下 anonymous 節點的冷卻。
  • refresh.go:Gateway.restoreProxy 一併呼叫,接上既有的恢復路徑
    (2xx / 非同步 cloudflare 探測 / 15 分鐘定時檢查)。
  • pool_test.go(test-first,RED 先提交):巨大 Retry-After 被 cap;
    restore 清掉 anonymous 冷卻且節點可再被選中。

沒做的(有意排除)

  • anonymousCursor.Next 加 earliest-fallback:會削弱退避語意;cap + restore
    已把鎖死上界壓到分鐘級且可隨 proxy 恢復立即解除。
  • Transport 重建:舊連線失敗形態是 transport error → 502/504,與本次 401
    症狀無關。

驗證

  • gofmt -l internal/ 無輸出;go vet ./... 通過;
    go test ./internal/gateway/ -v 三個新測試全過;
    go build -trimpath ./cmd/opencode2api 成功。
  • 出問題時可用 GET /healthz 看 proxies.healthy 區分是 proxy 閂鎖還是冷卻。

claude-12b1b8 · muse-spark

@jasonxu114514

jasonxu114514 commented Oct 9, 2026 •

Copy link
Copy Markdown
Owner

其實推薦做法是 不保留佔位的zenkey ,可能是我描述問題 應該是在添加了匿名渠道的時候移除了必須存在key的限制 但是沒有更新文檔
但我還是會審查該pr並思考合理性

RED: Retry-After honored without cap; anonymous pool has no RestoreProxy.
Cap Retry-After at 8x base cooldown in both MarkFailure paths, add
anonymousPool.RestoreProxy, and clear anonymous cooldowns when a proxy
recovers, so a new IP's fresh quota is reachable without a restart.
@cheerc
cheerc force-pushed the fix/anonymous-cooldown-lockup branch from 3d4ae18 to 74d6a7c Compare October 13, 2026 08:05
@jasonxu114514
jasonxu114514 merged commit 16f68b8 into jasonxu114514:main Oct 15, 2026
5 checks passed
jasonxu114514 added a commit that referenced this pull request Oct 15, 2026
…change (#59)

A quota response with a multi-hour Retry-After cooled anonymous exits for
hours, so after a network change gave them a fresh allowance traffic kept
falling through to key lanes until restart. Cap Retry-After on the
anonymous lane at the 8x backoff ceiling, and clear anonymous cooldowns
when their proxy recovers. Key lanes keep honoring Retry-After in full.

Co-authored-by: CheerC <[email protected]>
SK-DEV-AI added a commit to SK-DEV-AI/opencode2api that referenced this pull request Oct 15, 2026
…ver-key usage)

Takes upstream f8e996d/be91f86/0204ca1 (client fingerprint + inference
endpoint + genuine tools), jasonxu114514#59 anon cooldown lockup, jasonxu114514#63 token floor,
jasonxu114514#64 whole-attempt deadline, jasonxu114514#62 tool images, jasonxu114514#58 server-key usage
split, jasonxu114514#65/jasonxu114514#66 models surface, plus our merged jasonxu114514#76/jasonxu114514#77/jasonxu114514#78.

Kept local (fork-owned, still no upstream equivalent): zero-delivery
replay + length rescue + SSE heartbeat + stop/tail census (stream
lanes), offline no_upstream_contact probe guard, single-marshal Zen
shaping with paid-model gate, tcp4 dial + TLS resumption, RandomID
math/rand fallback, cancel-detached refresh fix (PR jasonxu114514#80 superseded by
maintainer feedback; kept locally pending lifetime-context rework).
Telemetry takes upstream's session-burn + server-key split with their
LRU session cap (better than our smallest-first); CloneMap takes
upstream's merged comment. Metrics ring test retargeted at upstream's
concrete ring types. doUpstream keeps upstream's 3-value signature;
replay uses a new doUpstreamCounted wrapper for continuous legs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants