A rate limit a restart resets is not a rate limit #2

Merged
paulhitt merged 1 commits from feat/durable-throttle into main 2026-08-24 20:35:18 +00:00
paulhitt commented 2026-08-24 20:35:06 +00:00 (Migrated from gitlab.com)

Throttle counters move from an actor's dictionary into Postgres.

Why. Every restart cleared them, so a deploy, a crash or an OOM kill handed out a fresh registration budget from every address. Found by exploiting it — restarting the accounts container to get past the limit during testing worked, and anything that works for me works for a script.

What changed

  • SQL/030_rate_limits.sql — rate_hits plus rate_allow/rate_reset/rate_sweep. One row per attempt, because the window slides and only timestamps answer "three in the last hour".
  • The decision happens inside rate_allow under a per-bucket advisory lock. Counting in the service and inserting afterwards is a check-then-act race. Negative control: without the lock, 40 concurrent callers against a limit of 5 admitted 7.
  • AddRateLimitCounters is a new migration, never an edit to an applied one.
  • allow throws rather than failing open — a limiter weakest when the database is sick is weakest exactly when it is most wanted.
  • The in-memory limiter stays as the reference implementation the unit tests pin.

Testing

  • scripts/test-rate-limits.sh — 18 checks against a real Postgres.
  • scripts/test-throttle-restart.sh — 11 checks driving the real binary over HTTP, ending in a restart that confirms the budget is still spent.

⚠️ The second script exists because the first was not enough: the SQL tests passed while the service returned 500 on every sign-in. psql sends literals the parser coerces; the driver binds binary with a declared type, so text and bigint reached a function taking interval and integer and no call resolved. Fixed with explicit casts.

Deploy. accounts-migrate runs before accounts starts, so the table is in place first. ENCLAVE_AUTO_MIGRATE is not set in prod and is not needed.

Throttle counters move from an actor's dictionary into Postgres. **Why.** Every restart cleared them, so a deploy, a crash or an OOM kill handed out a fresh registration budget from every address. Found by exploiting it — restarting the accounts container to get past the limit during testing worked, and anything that works for me works for a script. **What changed** - `SQL/030_rate_limits.sql` — `rate_hits` plus `rate_allow`/`rate_reset`/`rate_sweep`. One row per attempt, because the window slides and only timestamps answer "three in the last hour". - The decision happens inside `rate_allow` under a per-bucket advisory lock. Counting in the service and inserting afterwards is a check-then-act race. Negative control: without the lock, 40 concurrent callers against a limit of 5 admitted **7**. - `AddRateLimitCounters` is a new migration, never an edit to an applied one. - `allow` throws rather than failing open — a limiter weakest when the database is sick is weakest exactly when it is most wanted. - The in-memory limiter stays as the reference implementation the unit tests pin. **Testing** - `scripts/test-rate-limits.sh` — 18 checks against a real Postgres. - `scripts/test-throttle-restart.sh` — 11 checks driving the real binary over HTTP, ending in a restart that confirms the budget is still spent. ⚠️ The second script exists because the first was not enough: the SQL tests passed while the service returned **500 on every sign-in**. psql sends literals the parser coerces; the driver binds binary with a declared type, so `text` and `bigint` reached a function taking `interval` and `integer` and no call resolved. Fixed with explicit casts. **Deploy.** `accounts-migrate` runs before `accounts` starts, so the table is in place first. `ENCLAVE_AUTO_MIGRATE` is not set in prod and is not needed.
paulhitt commented 2026-08-24 20:35:19 +00:00 (Migrated from gitlab.com)

mentioned in commit 784ba853c8

mentioned in commit 784ba853c83d418b7449b057283339778334e295
paulhitt (Migrated from gitlab.com) merged commit 784ba853c8 into main 2026-08-24 20:35:19 +00:00
This repo is archived. You cannot comment on pull requests.
No Reviewers
No labels
1 Participants
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: extant2000/flamenet-files#2