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.
This commit is contained in:
cuipengcx90 committed 2026-10-19 11:59:39 +08:00
1 parent d04ff337d7
commit ea947bc5dc
1 file changed
+11 -5
+11 -5
View File
@@ -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
}