Rate limiting using a fixed window counter for arbitrary keys, backed by Redis for actix-web. This project is based on https://github.com/fnichol/limitation.
toml
[dependencies]
actix-limitation = "0.1.0"
actix-web = "2.0.0"
actix-rt = "1.0.0"
Code:
```rust use actixweb::{get, web, App, HttpServer, Responder}; use actixlimitation::{Limiter, RateLimiter}; use std::time::Duration;
async fn index(info: web::Path<(u32, String)>) -> impl Responder { format!("Hello {}! id:{}", info.1, info.0) }
async fn main() -> std::io::Result<()> { let limiter = web::Data::new( Limiter::build("redis://127.0.0.1") .cookiename("session-id") .sessionkey("rate-api-id") .limit(5000) .period(Duration::from_secs(3600)) // 60 minutes .finish() .expect("Can't build actix-limiter"), );
HttpServer::new(|| {
App::new()
.wrap(RateLimiter)
.app_data(limiter)
.service(index)
})
.bind("127.0.0.1:8080")?
.run()
.await
} ```