Crates.io | actix-limitation |
lib.rs | actix-limitation |
version | 0.5.1 |
source | src |
created_at | 2020-04-04 19:25:19.443588 |
updated_at | 2023-09-16 02:37:50.229103 |
description | Rate limiter using a fixed window counter for arbitrary keys, backed by Redis for Actix Web |
homepage | |
repository | https://github.com/actix/actix-extras.git |
max_upload_size | |
id | 226367 |
size | 39,832 |
Rate limiter using a fixed window counter for arbitrary keys, backed by Redis for Actix Web.
Originally based on https://github.com/fnichol/limitation.
[dependencies]
actix-web = "4"
actix-limitation = "0.5"
use actix_limitation::{Limiter, RateLimiter};
use actix_session::SessionExt as _;
use actix_web::{dev::ServiceRequest, get, web, App, HttpServer, Responder};
use std::{sync::Arc, time::Duration};
#[get("/{id}/{name}")]
async fn index(info: web::Path<(u32, String)>) -> impl Responder {
format!("Hello {}! id:{}", info.1, info.0)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let limiter = web::Data::new(
Limiter::builder("redis://127.0.0.1")
.key_by(|req: &ServiceRequest| {
req.get_session()
.get(&"session-id")
.unwrap_or_else(|_| req.cookie(&"rate-api-id").map(|c| c.to_string()))
})
.limit(5000)
.period(Duration::from_secs(3600)) // 60 minutes
.build()
.unwrap(),
);
HttpServer::new(move || {
App::new()
.wrap(RateLimiter::default())
.app_data(limiter.clone())
.service(index)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}