Repository navigation
fix(ratelimit): check per-IP and API-key limits before the global budget - #2353
Conversation
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 — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to 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 |
|
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/ratelimitnow checks per-IP, then the API-key tier, and only then the global budget.AuthEndpointHandleralready 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
make lint-changed, and the contract, fixture, route and docs checks) passes. Inmake test-gothe touched packages pass; the other failures on the test machine (GPU probe tests ininternal/playback, a macOS capability test ininternal/apiv2, a case-insensitive filesystem test ininternal/metadata/nfo) fail the same way onmain. Database- and Redis-backed tests were skipped, as in CI.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
AI Disclosure