Skip to content

fix(upstream): cap the number of anonymous exits tried per request - #127

Merged
jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
cuipengcx90:fix/anonymous-exit-cap
Oct 20, 2026
Merged

jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
cuipengcx90:fix/anonymous-exit-cap

Conversation

@cuipengcx90

Copy link
Copy Markdown
Contributor

Problem

doAnonymousUpstream sets limit = g.anonymous.Len(), so a request that every exit rejects (403/429) walks the entire proxyfile, with up to two legs per exit (v1 lane, then /inference), before it gives up.

Observed on a long-running instance with a ~24-exit proxyfile (log: request_routed events, 2104 anonymous requests before the change):

  • 163 requests (7.7%) used more than 8 attempts
  • worst case: 77 attempts / 666 s; others at 68-72 attempts / 220-510 s
  • these ended as 403, 429, 500 or a client cancel, so the client waited minutes for an answer that could not arrive

After capping the walk at 8 exits, the same instance served 180 anonymous requests with a maximum of 7 attempts and no request above 8.

Change

  • add maxAnonymousExits = 8 and use min(Len(), maxAnonymousExits) as the static-exit limit
  • the existing #pool rule is untouched: when a pool entry is present the limit still rises to retry.max_attempts, so a single rotating #pool entry keeps its fresh-IP-per-attempt behaviour

2 lines of logic plus comments, in internal/gateway/upstream.go only.

Not included / open points

  • refresh.go (the availability probe) has its own copy of the old limit formula and is deliberately left alone to keep this change minimal. Happy to apply the same cap there if you want it.
  • The repo has no test files on main, so none are added; gofmt -l, go vet ./... and go build -trimpath ./cmd/opencode2api pass locally.
  • 8 is a judgement call; it only needs to be larger than the number of exits that can plausibly help one 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants