Skip to content

feat: implement rate limiting middleware and enhance route organization - #15

Merged
iflames1 merged 1 commit into
devfrom
feat/implement-rate-limiter
Aug 2, 2026
Merged

feat: implement rate limiting middleware and enhance route organization#15
iflames1 merged 1 commit into
devfrom
feat/implement-rate-limiter

Conversation

@iflames1

@iflames1 iflames1 commented Aug 2, 2026

Copy link
Copy Markdown
Member
  • 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.

- 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.
Copilot AI review requested due to automatic review settings August 2, 2026 02:45
@cursor

cursor Bot commented Aug 2, 2026

Copy link
Copy Markdown

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.

@iflames1
iflames1 merged commit 6df2233 into dev Aug 2, 2026
1 check passed
@iflames1
iflames1 deleted the feat/implement-rate-limiter branch August 2, 2026 02:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants