Crates.io | axum-limit |
lib.rs | axum-limit |
version | 0.1.0-alpha.2 |
source | src |
created_at | 2024-04-24 07:00:10.435864 |
updated_at | 2024-08-27 02:36:51.191851 |
description | A rate limiting library using token buckets, centered around extractor-based limits for async web applications. |
homepage | https://github.com/gengteng/axum-limit |
repository | https://github.com/gengteng/axum-limit |
max_upload_size | |
id | 1218436 |
size | 64,984 |
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.
DashMap
for concurrent state management across asynchronous tasks.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.