Expand description
§axum-limit
This crate provides an efficient rate limiting mechanism using token buckets, specifically designed for asynchronous web applications with a strong focus on extractor-based rate limits.
§Features
- Configurable rate limits using extractors, allowing for flexible limit strategies per route.
- Supports various time granularities for rate limits (per second, per minute, per hour, and per day).
- Easily integrates with Axum, using extractors to apply rate limits seamlessly within your application routes.
- Utilizes
DashMap
for concurrent state management across asynchronous tasks.
§Example
Here is a basic example showing how to use the crate with Axum routes:
use http::Uri;
use axum_limit::{Limit, LimitState, LimitPerSecond};
use axum::{Router, routing::get, response::IntoResponse};
async fn route_handler(_: LimitPerSecond<5, Uri>) -> impl IntoResponse {
// Handler logic here, automatically enforcing the rate limit
}
fn main() {
let _app: Router<()> = Router::new()
.route("/your_route", get(route_handler))
.with_state(LimitState::<Uri>::default());
}
This example demonstrates setting up a rate limit of 5 requests per second on a specific route. The Limit
extractor
automatically enforces these limits based on the incoming requests.
For more comprehensive examples, please check the examples
directory in this
repository.
Structs§
- Limit
- Represents a rate limit configuration with generic parameters for count and time period.
This struct uses generics to allow flexible integration with any extractor that implements the
Key
trait. - Limit
State - Manages the state of rate limits for various keys.
This struct holds a concurrent map of keys to their corresponding
TokenBucket
instances, enabling efficient state management across asynchronous tasks.
Enums§
- Limit
Rejection - Enumerates possible failure modes for rate limiting when extracting from request parts.
Traits§
- Key
- Trait defining the requirements for a key extractor, which is used to uniquely identify limit subjects and extract rate limit parameters dynamically in request processing.
Type Aliases§
- Limit
PerDay - Rate limit configured to apply per day.
- Limit
PerHour - Rate limit configured to apply per hour.
- Limit
PerMinute - Rate limit configured to apply per minute.
- Limit
PerSecond - Rate limit configured to apply per second.