| Crates.io | actix-web-ratelimit |
| lib.rs | actix-web-ratelimit |
| version | 0.1.1 |
| created_at | 2025-06-18 18:27:22.154515+00 |
| updated_at | 2025-06-20 08:27:31.60361+00 |
| description | A simple and highly customizable rate limiter for actix-web 4 |
| homepage | |
| repository | https://github.com/bigyao25/actix-web-ratelimit |
| max_upload_size | |
| id | 1717472 |
| size | 94,016 |
A simple and highly customizable rate limiting middleware for actix-web 4.
Add this to your Cargo.toml:
[dependencies]
actix-web-ratelimit = "0.1"
# Or, for Redis support
actix-web-ratelimit = { version = "0.1", features = ["redis"] }
// Configure rate limiting: allow 3 requests per 10-second window
let config = RateLimitConfig::default().max_requests(3).window_secs(10);
// Create in-memory store for tracking request timestamps
let store = Arc::new(MemoryStore::new());
HttpServer::new(move || {
App::new()
// create and register the rate limit middleware
.wrap(RateLimit::new(config.clone(), store.clone()))
.route("/", web::get().to(index))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
let store = Arc::new(MemoryStore::new());
let config = RateLimitConfig::default()
.max_requests(3)
.window_secs(10)
// Extract client identifier from req. It is IP (realip_remote_addr) by default.
.id(|req| {
req.headers()
.get("X-Client-Id")
.and_then(|h| h.to_str().ok())
.unwrap_or("anonymous")
.to_string()
})
// Custom handler for rate limit exceeded. It returns a 429 response by default.
.exceeded(|id, config, _req| {
HttpResponse::TooManyRequests().body(format!(
"429 caused: client-id: {}, limit: {}req/{:?}",
id, config.max_requests, config.window_secs
))
});
HttpServer::new(move || {
App::new()
.wrap(RateLimit::new(config.clone(), store.clone()))
.route("/", web::get().to(index))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
first set feature redis enable:
actix-web-ratelimit = { version = "0.1", features = [ "redis" ] }
then you can use it:
let store = Arc::new(
RedisStore::new("redis://127.0.0.1/0")
.expect("Failed to connect to Redis")
// Custom prefix for Redis keys
.with_prefix("myapp:ratelimit:"),
);
let config = RateLimitConfig::default().max_requests(3).window_secs(10);
HttpServer::new(move || {
App::new()
.wrap(RateLimit::new(config.clone(), store.clone()))
.route("/", web::get().to(index))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
| Method | Description | Default |
|---|---|---|
max_requests(usize) |
Maximum requests per window | 10 |
window_secs(u64) |
Time window in seconds | 100 |
id(fn) |
Client identification function | IP address |
exceeded(fn) |
Rate limit exceeded handler | 429 response |
redis feature)This middleware uses a sliding window algorithm:
Run the example:
cargo run --example simple
Then test the rate limiting:
# This should work
curl http://localhost:8080/
# Exceed rate limit by making many requests
for i in {1..5}; do echo "$(curl -s http://localhost:8080)\r"; done
redis: Enables Redis storage backend supportThis project is licensed under the MIT License.