Skip to content

examples: add rate-limiting example#3682

Open
Sim-hu wants to merge 1 commit intotokio-rs:mainfrom
Sim-hu:rate-limiting-example
Open

examples: add rate-limiting example#3682
Sim-hu wants to merge 1 commit intotokio-rs:mainfrom
Sim-hu:rate-limiting-example

Conversation

@Sim-hu
Copy link

@Sim-hu Sim-hu commented Mar 8, 2026

Summary

  • Adds a new rate-limiting example showing how to use tower::limit::RateLimitLayer with axum
  • Demonstrates BufferLayer wrapping to satisfy axum's Clone requirement (since RateLimit is not Clone)
  • Uses HandleErrorLayer to convert middleware errors into HTTP responses

Motivation

Rate limiting is one of the most frequently asked-about patterns for axum, but there is no existing example covering it. This shows the tower-native approach using ServiceBuilder with proper error handling.

Checklist

  • cargo build -p example-rate-limiting
  • cargo run -p example-rate-limiting — tested with concurrent requests
  • cargo clippy -p example-rate-limiting
  • cargo fmt --check

Add an example showing how to apply `tower::limit::RateLimitLayer` to
an axum application. Since `RateLimit` does not implement `Clone`,
the example uses `BufferLayer` as a wrapper and `HandleErrorLayer` to
convert middleware errors into HTTP responses.

Rate limiting is one of the most common requirements for web
applications, and this pattern (tower middleware + error handling) comes
up frequently in discussions.
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.

1 participant