Skip to content

fix(ratelimit): check per-IP and API-key limits before the global budget - #2353

Merged
Quick104 merged 1 commit into
Silo-Server:mainfrom
Rhainland:fix/limiter-per-ip-before-global
Oct 16, 2026
Merged

Quick104 merged 1 commit into
Silo-Server:mainfrom
Rhainland:fix/limiter-per-ip-before-global

Conversation

@Rhainland

Copy link
Copy Markdown
Contributor

Problem

Closes #2330
Related issue: N/A
Validation tasks: changes #1217 C1

A client that is over its own per-IP or API-key rate limit keeps getting 429s, but each rejected request still spends a token from the server-wide budget. A single client hammering the server can drain that budget, and then unrelated clients get 429s too. With the Redis limiter the budget is shared across every API replica, so one client can push the whole cluster into rejecting everyone.

Approach

The shared rate-limit handler in internal/ratelimit now checks per-IP, then the API-key tier, and only then the global budget. AuthEndpointHandler already uses this order. The native v1 and v2 routes and the Jellyfin listener all use this handler, so the fix covers all three.

A request that the global check rejects keeps the per-IP and key tokens it already spent. Only the client that sent the request pays for that. Refunding them would need a reserve-then-commit operation on the limiter interface and a new Redis script, which isn't worth it for this. The 429 response headers are the same as before on every path.

Validation

  • Regression tests replay the issue's scenario through both 429 writers (native and Jellyfin): a client over its per-IP limit no longer drains the global budget, and a second client is still admitted. Another test checks that requests denied by the per-IP or key limit never reach the global limiter. All of these fail without the fix.
  • Go gate (build, gofmt, vet, make lint-changed, and the contract, fixture, route and docs checks) passes. In make test-go the touched packages pass; the other failures on the test machine (GPU probe tests in internal/playback, a macOS capability test in internal/apiv2, a case-insensitive filesystem test in internal/metadata/nfo) fail the same way on main. Database- and Redis-backed tests were skipped, as in CI.
  • Web gate not run: no web files changed.

Evidence

Evidence: none, no user-visible change

Risks

When the server is globally saturated, a client's retries now also count against its own per-IP and key allowance. Before, those retries were turned away before reaching the narrower checks.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.
  • The Evidence section shows every change a user can see, or says there is none.

AI Disclosure

  • Harness: Claude Code
  • Tool(s): Claude Code
  • Model(s): claude-opus-5-5
  • Involvement: Fully AI-generated, human verified
  • Adversarial review: a separate agent reviewed the diff for 429 header and counter changes, route-level bypasses, Redis and in-memory parity, and whether the tests fail on the old code. No findings.

The generic request limiter spent a server-wide token before checking the
client's per-IP and API-key allowances. A client already over its own limit
kept getting 429s while each rejected request still drained the global
budget, so unrelated clients were rejected too. With the Redis backend that
drained budget is shared across every replica.

The handler now checks per-IP, then the API-key tier, then the global
budget, the same order AuthEndpointHandler already uses. A request the
global check rejects keeps the narrower tokens it spent, which charges only
the client that sent it. The native API and the Jellyfin listener both go
through this handler, so both get the fix.

Closes Silo-Server#2330
@silo-kody

silo-kody Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

@coderabbitai

coderabbitai Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2b0a27c8-7d0a-45bc-a384-625543862bd7

📥 Commits

Reviewing files that changed from the base of the PR and between 88a301a and 0b0bcd3.


📒 Files selected for processing (2)
  • internal/ratelimit/middleware.go
  • internal/ratelimit/middleware_test.go

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.



📝 Walkthrough

Walkthrough

The rate-limit middleware now checks per-IP and API-key limits before the global limit. Regression tests verify that narrower-limit rejections do not consume global capacity.

Changes

Rate-limit budget ordering

Layer / File(s) Summary
Check narrower budgets before global capacity
internal/ratelimit/middleware.go, internal/ratelimit/middleware_test.go
The middleware checks per-IP and API-key limits before the global limit. Tests verify that rejected requests do not consume global capacity, including for native and custom rejection responses.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: quick104


Merge Risk: ⚪ Minimal · up to 0b0bc

Requests rejected by a per-IP or API-key limit no longer consume shared global capacity. This matches the linked issue, and regression tests cover both the native and custom-body 429 paths. No concrete merge-blocking risk remains.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: ordering per-IP and API-key checks before the global rate-limit budget.
Description check Passed The description directly explains the rate-limit issue, implementation approach, validation, scope, and risks. It is fully related to the changeset.
Linked Issues check Passed The PR satisfies issue #2330. internal/ratelimit/middleware.go checks the per-IP limit, then the API-key limit, then the global limiter. This prevents rejected narrower-limit requests from consuming…
Out of Scope Changes check Passed The reviewed changes are limited to the shared rate-limit handler and its tests in internal/ratelimit. The implementation, custom rejection-writer coverage, and limiter-call assertions directly supp…

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@silo-triage silo-triage Bot added bug Something isn't working go Pull requests that update go code priority: P1 Core-path bug; painful workaround — next after P0 labels Oct 12, 2026
@Quick104
Quick104 merged commit d1ddfbe into Silo-Server:main Oct 16, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update go code priority: P1 Core-path bug; painful workaround — next after P0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Requests rejected by per-IP limits still consume the global request budget

2 participants