fix: stop the anonymous lane from locking up after quota and network 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 <cheerc@gmail.com>
This commit is contained in:
Jason XuandCheerC committed 2026-10-15 07:52:23 +08:00
1 parent 0569f953a8
commit d27a3ec8bf
2 files changed
+38 -3

No files matched your search

+33 -1
View File
@@ -112,6 +112,38 @@ 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
}
// maxAnonymousRetryAfterMultiplier bounds an upstream Retry-After on the
// anonymous lane to the same 8x ceiling as its exponential backoff. The lane
// is rate limited per exit IP and, unlike key lanes, has no earliest-expiry
// fallback: a quota response asking for hours would keep the exit skipped long
// after a network change gave it a fresh allowance. Key lanes keep honoring
// Retry-After in full, since their cursor already falls back to the node that
// recovers first.
const maxAnonymousRetryAfterMultiplier = 8
func (p *anonymousPool) MarkFailure(node *anonymousNode, resp *http.Response, err error) {
if node == nil {
return
@@ -123,7 +155,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*maxAnonymousRetryAfterMultiplier)
}
}
node.cooldownUntil.Store(time.Now().Add(delay).UnixNano())
+5 -2
View File
@@ -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
}