From ea947bc5dcaa0654f4fef5662e430aa1d1825111 Mon Sep 17 00:00:00 2001 From: cuipengcx90 <223995203+cuipengcx90@users.noreply.github.com> Date: Mon, 19 Oct 2026 11:59:39 +0800 Subject: [PATCH] fix(upstream): cap the number of anonymous exits tried per request doAnonymousUpstream used limit = len(pool), so a request that every exit rejects (403/429) walked the whole proxyfile, up to two legs per exit, before failing. With a ~24 exit proxyfile this showed up as requests with 19-77 attempts and 200-660s of wall time, all ending in a 403/429 or a client cancel. Cap the static exit walk at 8. The #pool rule is unchanged: when a pool entry is present the limit still rises to retry.max_attempts, so a single rotating #pool entry keeps its per-attempt fresh IP. --- internal/gateway/upstream.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/internal/gateway/upstream.go b/internal/gateway/upstream.go index e584b33..5093ad5 100644 --- a/internal/gateway/upstream.go +++ b/internal/gateway/upstream.go @@ -290,11 +290,17 @@ func (g *Gateway) doUpstreamTiers(ctx context.Context, route models.Route, bodie return nil, effectiveRoute, attempts, lastErr } +// maxAnonymousExits caps how many static proxies one request walks. Without it +// a request that every exit rejects (403/429) scans the whole proxy list, up to +// two legs per exit, and can spend minutes before failing over. +const maxAnonymousExits = 8 + // doAnonymousUpstream walks the session's anonymous proxy cursor. Each static -// proxy is tried at most once; rotating #pool proxies may be drawn again until -// Retry.MaxAttempts proxies were used. For each proxy it tries the upstreams in -// anonymousUpstreams order: the v1 lane (models.V1Base) before the v2 -// /inference upstream (Upstream.Anonymous). Any failure, including an HTTP +// proxy is tried at most once, up to maxAnonymousExits proxies; rotating #pool +// proxies may be drawn again until Retry.MaxAttempts proxies were used. For +// each proxy it tries the upstreams in anonymousUpstreams order: the v1 lane +// (models.V1Base) before the v2 /inference upstream (Upstream.Anonymous). Any +// failure, including an HTTP // error response, advances to the next upstream or proxy; a proxy-route // failure, or a proxy marked unhealthy, skips that proxy's remaining // upstreams. Only a successful response ends the anonymous phase; exhausting @@ -305,7 +311,7 @@ func (g *Gateway) doAnonymousUpstream(ctx context.Context, route models.Route, b var lastResponse *http.Response var lastErr error cursor := g.anonymous.CursorFor(ids.Session) - limit := g.anonymous.Len() + limit := min(g.anonymous.Len(), maxAnonymousExits) if g.anonymous.HasPool() && g.cfg.Retry.MaxAttempts > limit { limit = g.cfg.Retry.MaxAttempts }