From 79195d8767da3fec12f989dfaea412a6a5fd7d21 Mon Sep 17 00:00:00 2001 From: CheerC Date: Sat, 10 Oct 2026 03:26:12 +0800 Subject: [PATCH 1/2] test: reproduce anonymous 401 lockup after quota+network change RED: Retry-After honored without cap; anonymous pool has no RestoreProxy. --- internal/gateway/pool_test.go | 103 ++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 internal/gateway/pool_test.go diff --git a/internal/gateway/pool_test.go b/internal/gateway/pool_test.go new file mode 100644 index 0000000..a8cf8f4 --- /dev/null +++ b/internal/gateway/pool_test.go @@ -0,0 +1,103 @@ +package gateway + +import ( + "net/http" + "strconv" + "testing" + "time" +) + +// Reproduction for: after free-tier quota exhaustion + network change (new IP), +// the gateway keeps answering 401 until the process is restarted, even though +// the network is back and the new IP has fresh quota. +// +// Root causes covered here: +// 1. MarkFailure honors an upstream Retry-After header with no upper bound, +// so one quota response can cool a lane for hours. +// 2. The anonymous lane skips cooling nodes outright (no earliest-expiry +// fallback like the key pool), so a long cooldown reads as a permanent +// outage and traffic falls through to key lanes that always 401. +// 3. Proxy recovery clears key-node cooldowns but never anonymous ones. + +func testTransports() *transportPool { + proxy := &proxyTransport{name: "direct", client: &http.Client{}} + proxy.healthy.Store(true) + return &transportPool{items: []*proxyTransport{proxy}} +} + +func retryAfterResponse(seconds int) *http.Response { + resp := &http.Response{StatusCode: http.StatusTooManyRequests, Header: make(http.Header)} + resp.Header.Set("Retry-After", strconv.Itoa(seconds)) + return resp +} + +func nextAnonymous(t *testing.T, pool *anonymousPool) *anonymousNode { + t.Helper() + cursor := pool.CursorFor("") + node := cursor.Next() + if node == nil { + t.Fatal("expected an anonymous node") + } + return node +} + +func remainingCooldown(t *testing.T, untilUnixNano int64) time.Duration { + t.Helper() + remaining := time.Until(time.Unix(0, untilUnixNano)) + if remaining < 0 { + t.Fatalf("expected a future cooldown, got %v", remaining) + } + return remaining +} + +func TestAnonymousMarkFailureCapsRetryAfter(t *testing.T) { + pool := newAnonymousPool(true, testTransports(), 15*time.Second) + node := nextAnonymous(t, pool) + pool.MarkFailure(node, retryAfterResponse(3600), nil) + remaining := remainingCooldown(t, node.cooldownUntil.Load()) + // Base 15s with an 8x ceiling: a 1h Retry-After must be capped at ~120s. + if remaining > 150*time.Second { + t.Fatalf("Retry-After was honored without a cap: cooldown=%v, want <=150s", remaining) + } + if remaining < 100*time.Second { + t.Fatalf("Retry-After was ignored instead of capped: cooldown=%v, want ~120s", remaining) + } +} + +func TestKeyMarkFailureCapsRetryAfter(t *testing.T) { + pool, err := newNodePool([]string{"k1"}, testTransports(), 15*time.Second) + if err != nil { + t.Fatal(err) + } + cursor := pool.Cursor() + node := cursor.Next() + if node == nil { + t.Fatal("expected a key node") + } + pool.MarkFailure(node, retryAfterResponse(7200), nil) + remaining := remainingCooldown(t, node.cooldownUntil.Load()) + if remaining > 150*time.Second { + t.Fatalf("Retry-After was honored without a cap: cooldown=%v, want <=150s", remaining) + } +} + +func TestAnonymousRestoreProxyClearsCooldown(t *testing.T) { + transports := testTransports() + pool := newAnonymousPool(true, transports, 15*time.Second) + node := nextAnonymous(t, pool) + pool.MarkFailure(node, retryAfterResponse(3600), nil) + cursor := pool.CursorFor("") + if got := cursor.Next(); got != nil { + t.Fatal("expected the cooling node to be skipped before restore") + } + if cleared := pool.RestoreProxy(transports.items[0]); cleared != 1 { + t.Fatalf("RestoreProxy cleared %d nodes, want 1", cleared) + } + after := pool.CursorFor("") + if got := after.Next(); got == nil { + t.Fatal("expected the node to be selectable again after proxy restore") + } + if failures := node.failures.Load(); failures != 0 { + t.Fatalf("failures=%d after restore, want 0", failures) + } +} From 74d6a7c71eb57ecee2260f08470ef47b9385c957 Mon Sep 17 00:00:00 2001 From: CheerC Date: Sat, 10 Oct 2026 03:27:11 +0800 Subject: [PATCH 2/2] 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. --- internal/gateway/pool.go | 34 ++++++++++++++++++++++++++++++++-- internal/gateway/refresh.go | 7 +++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/internal/gateway/pool.go b/internal/gateway/pool.go index 93d074c..23fc335 100644 --- a/internal/gateway/pool.go +++ b/internal/gateway/pool.go @@ -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< 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()) diff --git a/internal/gateway/refresh.go b/internal/gateway/refresh.go index deb6f28..5f6192d 100644 --- a/internal/gateway/refresh.go +++ b/internal/gateway/refresh.go @@ -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 }