Expand description
Token-bucket rate limiting middleware — rate_limit::RateLimitLayer.
Per-IP, per-header, or global. Returns 429 with Retry-After when exhausted.
Token-bucket rate limiting middleware for axum routers.
Configurable per-IP or per-user limits with burst allowance. Returns
429 Too Many Requests when the bucket is exhausted.
§Quick start
ⓘ
use rustango::rate_limit::{RateLimitLayer, RateLimitRouterExt};
use std::time::Duration;
let app = Router::new()
.route("/api/login", post(login))
.rate_limit(RateLimitLayer::per_ip(5, Duration::from_secs(60))); // 5 req/min§Strategy
- Token bucket: each key (IP or user id) gets
capacitytokens. - On each request, one token is removed. If empty, return 429.
- Tokens refill at
capacity / refill_periodper second. - Buckets live in an in-process
tokio::sync::Mutex<HashMap>. Process-local — for distributed enforcement, integrate with the cache layer (future slice).
Structs§
- Rate
Limit Layer - Rate-limit configuration.
Enums§
- KeyBy
- Strategy for picking the bucket key per request.
Traits§
- Rate
Limit Router Ext - Extension trait — apply a rate-limit layer to a router.