By BluffKing · · Updated
Three Layers of Rate Limiting
Caller rate, account quota and machine capacity need separate limits. Current Rust mechanisms: GCRA, address/session buckets and bounded admission.
When we audited the Rust backend’s rate limits in August 2026, the defences looked complete: more than twenty endpoints had per-IP limits, login and password reset were throttled per account, and AI features had daily quotas. But all of that answered only two questions: is this caller going too fast, and has this account used up today’s quota? Nobody answered the third: what happens when the whole machine is full? Requests had no timeout, and in-flight requests, WebSocket connections and live tables had no global cap; a hundred ordinary users could each pass every per-caller check and still overload the box together.
We added global admission control that sheds load with a 503 and Retry-After instead of queuing. A hand has a 15-second action clock and the web client gives up reconnecting after 30 seconds, so a “call” that lands eight seconds late is worse than a clear refusal; only the CPU permit waits for a short deadline, and still refuses after it. The main cap counts live table tasks, the thing that actually costs resources, and it refuses only opening a new table, never joining an existing one. Health checks and static assets are exempt, so a busy box is not mistaken for a failed one and pages still load; slots are released by a drop guard, so a client that disconnects mid-request does not leak one; and the global gate fails closed.
The per-caller layer changed in two places. A 60-per-minute fixed window can let 120 requests through within 200 milliseconds around the boundary, so per-IP and per-session budgets for HTTP and WebSocket now all use GCRA: one timestamp per key, no window edge, and an exact retry time. The session bucket used to be bypassable: the limiter runs before authentication, so a random fake cookie bought a fresh quota. Now every request must pass the address bucket first and the session bucket as well if it has one, so rotating cookies cannot escape the per-IP budget.
The limits come from measurements. On the real production box, the solo ladder kept 150 players / 150 tables inside the latency budget, missed it at 200, and reached about 4.1 seconds p99 at 300; two 60-player / 10-table multi-player runs had server p99s of 69 ms and 157 ms, and 120 players / 20 tables reached 339 ms. On August 21, 2026 we took 80% of the worse 60-player run, capped new WebSockets at 48, and reserved 12 more slots for reconnects and joins to existing tables; ADMISSION_MAX_LIVE_TABLES=2000 is only a memory fuse against a bug or script that opens tables forever. These are source defaults and a historical operating point (server/src/admission.rs, server/src/rate_limit.rs); environment variables can override them, and they do not describe current production configuration or capacity.
The lesson: rate limiting is three questions. The first two are about fairness and billing; only “is the machine full?” decides whether the service survives, and it cannot be assembled from per-caller counts. Every limit should be configurable and set from measurements, and every refusal should be logged with its reason; a limit that never reports how often it fired looks exactly like a misconfigured one that never fires.