Skip to content

feat(gateway): add an opt-in circuit breaker for the anonymous free-tier lane - #140

Merged
jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
tubimasky:pr-anon-breaker
Oct 20, 2026
Merged

jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
tubimasky:pr-anon-breaker

Conversation

@tubimasky

Copy link
Copy Markdown

Summary

When the anonymous free tier is blocked for the deployment's egress IP (the 403
"abusive traffic" FreeTierError), the lane currently keeps probing:
anonymousNodes.MarkFailure cools one exit for at most 8x
performance.failure_cooldown_seconds (default 120s), then the cursor draws the
next exit, so every client request repeats the whole 403 walk. The upstream
message explicitly warns that retrying extends the penalty. Related to #131.

This PR adds an optional lane-level circuit breaker, next to the existing
per-exit cooldown:

  • threshold consecutive failed anonymous attempts, or a single 403/429
    (an explicit block signal), pause the whole anonymous lane for base_hours;
  • every further trip doubles the window, capped at max_hours;
  • a successful attempt closes the breaker and restores the free lane;
  • while paused, no proxy is drawn and no upstream request is made: routing goes
    straight to the authenticated tiers, and availability probes honour the state
    instead of refreshing the block;
  • absent config leaves the behavior unchanged: the breaker is off by
    default
    and every existing configuration keeps working.

Usage

Enable it by adding the block to the configuration file:

{
  "models": {
    "anonymous_backoff": { "threshold": 6, "base_hours": 1, "max_hours": 24 }
  }
}
  • The block is optional. Omit it (or leave the gateway config untouched) and
    nothing changes.
  • All three fields are optional. Zero values take the documented defaults
    (6, 1, 24), so "anonymous_backoff": {} is the same as the example
    above.
  • Pause windows grow 1h -> 2h -> 4h -> ... and stop at max_hours; any
    anonymous success resets the strikes, so the free lane recovers by itself
    once the block lifts.
  • While paused, requests skip the anonymous lane entirely and fall through to
    the authenticated tiers in prefer order. Free models that are only
    available anonymously return the keyed-tier error until the window expires.
  • A WARN log event anonymous_breaker_open reports the upstream status and
    the paused_for window, so the state is visible in the logs.

This is the configuration used in the live test below:

"models": { "anonymous_backoff": { "threshold": 6, "base_hours": 1, "max_hours": 24 } }

Relationship to #131

#131 proposes a per-exit TTL plus 503 free_tier_ip_blocked when every exit is
blocked. This PR is complementary but deliberately differs in one respect:
when a Go key is configured, keyed requests to /zen/go are authenticated and
are not subject to the free-tier IP block (verified 200 from the same blocked
IP), so falling through to the keyed tiers can keep serving instead of returning
503. A Zen key shares the free gateway and will still 403. If maintainers
prefer the 503 semantics, that is a small change on top of this PR.

Testing

  • go build ./... and go vet ./internal/... pass; gofmt clean.
  • Live test on Debian 12 / aarch64, OpenCode 2.0.32 client, egress IP blocked:
    • before: a bare model ID spent 10–50s per request in anonymous 403 probes;
    • after: the first 403 tripped the breaker (anonymous_breaker_open,
      status=403, paused_for=1h0m0s), and the next request reached the Go tier
      in about 1.5s with no anonymous attempt.
  • The repository has no test suite and this PR does not add one.

AI disclosure

This change was generated by an AI coding agent (DeepSeek) working with the
human submitter, then built, vetted, and validated in a live deployment as
described above. The human submitter has reviewed the diff and can answer
follow-up questions.

A flagged free-tier exit keeps answering 403 (IP-level abuse detection)
and retrying only extends the penalty. Add an optional lane-level circuit
breaker, configured with models.anonymous_backoff:

- threshold consecutive failed anonymous attempts, or one 403/429, pause
  the whole lane for base_hours, doubling on every further trip up to
  max_hours;
- while paused, no proxy is drawn and no upstream request is made, so
  routing falls straight through to the authenticated tiers;
- a successful attempt closes the breaker and restores the free lane;
- absent config leaves behavior unchanged;
- availability probes honour the breaker instead of refreshing the block.
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