mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
fix: stop anonymous lane from locking up after quota+network change
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.
This commit is contained in:
1 parent
79195d8767
commit
74d6a7c71e
2 files changed
+37
-4
No files matched your search
@@ -104,6 +104,13 @@ func (c *anonymousCursor) Next() *anonymousNode {
|
||||
return nil
|
||||
}
|
||||
|
||||
// maxRetryAfterMultiplier bounds an upstream Retry-After header to the same
|
||||
// 8x ceiling as the exponential backoff below. Without a cap, a single quota
|
||||
// response can cool a lane for hours; the anonymous lane skips cooling nodes
|
||||
// outright (see anonymousCursor.Next), so that reads as a permanent outage
|
||||
// and traffic falls through to key lanes until the process restarts.
|
||||
const maxRetryAfterMultiplier = 8
|
||||
|
||||
func (p *anonymousPool) MarkSuccess(node *anonymousNode) {
|
||||
if node == nil {
|
||||
return
|
||||
@@ -112,6 +119,29 @@ func (p *anonymousPool) MarkSuccess(node *anonymousNode) {
|
||||
node.cooldownUntil.Store(0)
|
||||
}
|
||||
|
||||
// RestoreProxy clears cooldowns for anonymous nodes bound to a recovered
|
||||
// proxy, mirroring nodePool.RestoreProxy for key nodes. Without this, an
|
||||
// outage-era cooldown survives the proxy's recovery and the anonymous lane
|
||||
// stays skipped even though the route is healthy again.
|
||||
func (p *anonymousPool) RestoreProxy(proxy *proxyTransport) int {
|
||||
if p == nil || proxy == nil {
|
||||
return 0
|
||||
}
|
||||
cleared := 0
|
||||
for _, node := range p.nodes {
|
||||
if node.proxy != proxy {
|
||||
continue
|
||||
}
|
||||
if node.cooldownUntil.Load() == 0 && node.failures.Load() == 0 {
|
||||
continue
|
||||
}
|
||||
node.failures.Store(0)
|
||||
node.cooldownUntil.Store(0)
|
||||
cleared++
|
||||
}
|
||||
return cleared
|
||||
}
|
||||
|
||||
func (p *anonymousPool) MarkFailure(node *anonymousNode, resp *http.Response, err error) {
|
||||
if node == nil {
|
||||
return
|
||||
@@ -123,7 +153,7 @@ func (p *anonymousPool) MarkFailure(node *anonymousNode, resp *http.Response, er
|
||||
delay := p.cooldown * time.Duration(1<<min(failures-1, 3))
|
||||
if resp != nil {
|
||||
if retryAfter := parseRetryAfter(resp.Header.Get("Retry-After")); retryAfter > delay {
|
||||
delay = retryAfter
|
||||
delay = min(retryAfter, p.cooldown*time.Duration(maxRetryAfterMultiplier))
|
||||
}
|
||||
}
|
||||
node.cooldownUntil.Store(time.Now().Add(delay).UnixNano())
|
||||
@@ -484,7 +514,7 @@ func (p *nodePool) MarkFailure(node *upstreamNode, resp *http.Response, err erro
|
||||
delay := p.cooldown * multiplier
|
||||
if resp != nil {
|
||||
if retryAfter := parseRetryAfter(resp.Header.Get("Retry-After")); retryAfter > delay {
|
||||
delay = retryAfter
|
||||
delay = min(retryAfter, p.cooldown*time.Duration(maxRetryAfterMultiplier))
|
||||
}
|
||||
}
|
||||
node.cooldownUntil.Store(time.Now().Add(delay).UnixNano())
|
||||
|
||||
@@ -82,8 +82,11 @@ func (g *Gateway) restoreProxy(proxy *proxyTransport) (zenMoved, goMoved int) {
|
||||
}
|
||||
zenMoved = g.zenNodes.RestoreProxy(proxy.index)
|
||||
goMoved = g.goNodes.RestoreProxy(proxy.index)
|
||||
if zenMoved+goMoved > 0 {
|
||||
g.logger.Info("proxy connectivity restored", "component", "proxy", "event", "proxy_restored", "proxy", config.RedactURL(proxy.name), "zen_keys_moved", zenMoved, "go_keys_moved", goMoved)
|
||||
// The anonymous lane carries its own per-proxy cooldowns; key recovery
|
||||
// alone would leave it skipped after an outage-era failure.
|
||||
anonCleared := g.anonymous.RestoreProxy(proxy)
|
||||
if zenMoved+goMoved > 0 || anonCleared > 0 {
|
||||
g.logger.Info("proxy connectivity restored", "component", "proxy", "event", "proxy_restored", "proxy", config.RedactURL(proxy.name), "zen_keys_moved", zenMoved, "go_keys_moved", goMoved, "anonymous_cleared", anonCleared)
|
||||
}
|
||||
return zenMoved, goMoved
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user