#1813

feat(edge): send RateLimit headers on route-rule 429s

OpenSibilSorenwants to merge intofrom

Summary

When a route rule's rate limit trips, the edge now sends RateLimit-Policy and RateLimit along with Retry-After, so the client can see which limit it hit and when it resets.

Motivation

#1227 asks for the IETF RateLimit fields on rule-limited responses. Right now a 429 from a rule carries only Retry-After: 1 and nginx's default body, so a client has no way to know what the limit is.

I described this approach on the issue on 8 Nov. One of the two questions I asked there has since been settled on main: since #1739 the guard writes rule-defined response headers, and the contract test no longer pins Retry-After as the only header it sets.

Related issue

Closes #1227

Changes

packages/adapters

  • rules_guard.lua: on the rate-limit 429 path, set RateLimit-Policy: "rule";q=<limit>;w=1 and RateLimit: "rule";r=0;t=1 before Retry-After. q is the compiled rl.limit (rps + burst), already an integer after compile_spec, and it's written with %d. Everything else is a literal. Nothing changes outside the 429 path.
  • lua-embedded.ts regenerated with bun run embed:lua.

apps/api

  • routing-rules.e2e.test.ts: the existing real-OpenResty rate-limit test now also checks both fields on every 429, and checks that the requests the limit lets through don't get them.

apps/web

  • guides/the-edge.mdx: a short paragraph under the rule examples, with the 429 that the /login example rule sends.

Verification

# The new assertions against main's rules_guard.lua (without this change)
$ RUN_DOCKER_E2E=1 bunx vitest run -c vitest.e2e.config.ts test/e2e/routing-rules.e2e.test.ts -t "rate-limits a null-prefix"
 × routing rules, real OpenResty (source Lua) > rate-limits a null-prefix rule across paths with separate host and prefix buckets
 × routing rules, real OpenResty (embedded Lua) > rate-limits a null-prefix rule across paths with separate host and prefix buckets
AssertionError: expected undefined to be '"rule";q=2;w=1' // Object.is equality
      Tests  2 failed | 100 skipped (102)

# With this change (same result in 3 runs)
      Tests  2 passed | 100 skipped (102)

# The whole file, both Lua copies
$ RUN_DOCKER_E2E=1 bunx vitest run -c vitest.e2e.config.ts test/e2e/routing-rules.e2e.test.ts
      Tests  102 passed (102)

$ bunx vitest run test/modules/route-rules          # apps/api, includes rules-lua-contract
      Tests  73 passed (73)
$ bunx vitest run src/infra/lua-embedded.test.ts    # packages/adapters, embedded copy matches source
      Tests  2 passed (2)

$ bun run --cwd packages/adapters lint
$ bun run --cwd apps/api lint
$ tsc --noEmit

$ bun run test --continue
 Tasks:    12 successful, 13 total
Failed:    @repo/api#test

The api failures are the same 4 timezone-dependent tests in domain-diagnostics.test.ts as in #1743, #1808 and #1809. They fail on main in IST and pass with TZ=UTC. Every other workspace passed, including @repo/adapters (6172 passed, 7 skipped) and @repo/web.

A 429 and a 200 from that test (openresty/1.27.1.1, rps: 1, burst: 1), printed with a temporary log line I didn't keep:

429  ratelimit-policy: "rule";q=2;w=1
     ratelimit: "rule";r=0;t=1
     retry-after: 1
200  no ratelimit-* or retry-after headers

A few choices worth checking:

  • It's not opt-in per rule. Only responses that are already 429 change, and clients that don't know these fields ignore them, so a toggle didn't seem worth a new field in the spec, the UI and the CLI. I can add one if you'd prefer.
  • t and Retry-After stay at 1 because the window is fixed at one second. If longer windows come in (#1225), w, t and Retry-After should come from the rule's window instead.
  • The policy name is the fixed string "rule", so no rule value except the integer limit ends up in a header.

Checklist

  • One change per PR — one bug, or one agreed feature, with nothing unrelated bundled in
  • The diff is scoped — no reformatting or lint fixes on lines I wasn't otherwise changing
  • A test fails without this change and passes with it (or I explained above why there isn't one)
  • bun run test, bun run --cwd <workspace> lint, and bun format all pass locally (all pass except the timezone-dependent api tests above; routing-rules.e2e.test.ts already has Prettier drift on main, so I checked that Prettier has nothing to change on the lines I touched)
  • I understand every line of this diff and can explain it in review

Activity

2
SibilSoren7d ago

@Hydralerne, could you take a look at this when you get a chance? Nothing on main has touched these files since I opened it, so it still merges cleanly and CI passes. I'm holding off on #1225 until this lands, since it changes the same 429 path.