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 }