feat: implement rate limiting middleware and enhance route organization - #15
Merged
Conversation
- Added Redis-based rate limiting middleware for sensitive and write routes. - Introduced new error handling for rate limit exceeded scenarios. - Refactored route structure to separate sensitive, write, and read operations for users, lobbies, and wallets. - Updated `Cargo.toml` to include `http-body-util` as a development dependency. - Enhanced error handling in the application to include new rate limit errors.
|
Bugbot is not enabled for your account, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
There was a problem hiding this comment.
Pull request overview
Implements Redis-backed fixed-window (60s) rate limiting middleware and reorganizes Axum routes into sensitive/write/read tiers so different endpoint groups can be governed by different rate-limit policies, including WebSocket connection limiting.
Changes:
- Added Redis rate limiting middleware (global/write/sensitive tiers) and WS connect limiting.
- Refactored route modules (users/lobbies/wallet/admin) into
*_router()functions by tier and updated route composition. - Updated server startup to include
ConnectInfo<SocketAddr>extraction support; added dev dependencies for middleware tests.
Reviewed changes
Copilot reviewed 11 out of 12 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/sw-server/src/ws/handler.rs | Applies WS connect rate limiting before upgrading to WebSocket. |
| crates/sw-server/src/routes/wallet.rs | Splits wallet endpoints into sensitive/write/read routers for tiered limiting. |
| crates/sw-server/src/routes/users.rs | Splits user endpoints into write/read routers and adds needed HTTP method routings. |
| crates/sw-server/src/routes/mod.rs | Rebuilds top-level router to layer sensitive/write/global rate limit middleware by subtree. |
| crates/sw-server/src/routes/lobbies.rs | Splits lobby endpoints into sensitive/write/read routers for tiered limiting. |
| crates/sw-server/src/routes/admin.rs | Splits admin endpoints into write/read routers to apply write-tier limits to mutations. |
| crates/sw-server/src/middleware/rate_limit.rs | New Redis-based fixed-window rate limiting implementation + tests. |
| crates/sw-server/src/middleware/mod.rs | Exposes the new rate_limit middleware module and updates boundary middleware docs. |
| crates/sw-server/src/main.rs | Enables ConnectInfo<SocketAddr> so middleware/extractors can access peer address. |
| crates/sw-server/src/error.rs | Adds RateLimited application error mapping to 429. |
| crates/sw-server/Cargo.toml | Adds dev dependencies used by new middleware tests. |
| Cargo.lock | Locks the new http-body-util dependency. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+89
to
+97
| let count: i64 = redis.incr(key, 1).await?; | ||
| if count == 1 { | ||
| let _: bool = redis.expire(key, WINDOW_SECS).await?; | ||
| } | ||
| let ttl: i64 = match redis.ttl(key).await { | ||
| Ok(t) => t, | ||
| Err(_) => WINDOW_SECS, | ||
| }; | ||
| let reset_secs = if ttl < 0 { WINDOW_SECS as u64 } else { ttl as u64 }; |
Comment on lines
+132
to
+140
| fn forwarded_for_first(headers: &HeaderMap) -> Option<String> { | ||
| let raw = headers.get("x-forwarded-for")?.to_str().ok()?; | ||
| let first = raw.split(',').next()?.trim(); | ||
| if first.is_empty() { | ||
| None | ||
| } else { | ||
| Some(first.to_owned()) | ||
| } | ||
| } |
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.
Cargo.tomlto includehttp-body-utilas a development dependency.