feat(rate-limit): structured 429 + Retry-After header (#10) - #44
Merged
Conversation
When the rate-limit middleware refuses a request, the response now
carries enough information for the frontend to render a precise
countdown instead of a generic "try again later":
- ``Retry-After`` header with integer seconds until the next request
will succeed (per RFC 7231 §7.1.3).
- Structured body shape
``{"detail": {"code, "message", "limit", "retry_after"}}``. The
``code`` is a stable ``rate_limited`` string so the frontend can
branch on it; ``message`` is a human-readable string with the
correct singular/plural for ``second`` vs ``seconds``; ``limit`` is
the configured ratio (e.g. ``"5 per 1 minute"``) for diagnostic
surfacing; ``retry_after`` mirrors the header so callers can use
either source.
The reset timestamp comes from ``limits.strategies.MovingWindowRateLimiter.
get_window_stats(item, key)``, which after a refused hit reports when
the oldest in-window request rolls off. Clamped to ≥1s so the frontend
never gets a zero/negative countdown from edge timing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #10.
Summary
When the rate-limit middleware refuses a request, the response now carries enough information for the frontend to render a precise countdown instead of a generic "try again later":
```json
{
"detail": {
"code": "rate_limited",
"message": "Too many requests on POST /auth/jwt/login. Try again in 42 seconds.",
"limit": "5 per 1 minute",
"retry_after": 42
}
}
```
The reset timestamp comes from `limits.strategies.MovingWindowRateLimiter.get_window_stats(item, key)`, which after a refused hit reports when the oldest in-window request rolls off — i.e., when capacity will next open up. Clamped to ≥1s so the frontend never gets a zero/negative countdown from edge timing.
Stable contract
Test plan
Frontend follow-up
Auth-web's existing login / register error handling currently shows a generic error on 429. Once this ships, the frontend can read `detail.retry_after` and render a countdown ("Try again in 0:42") for a much better UX. Not blocking — backwards compatible because old handlers will just show whatever they did for the generic 429 string, and the new error shape doesn't break form-validation flows.