1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-27 17:22:57 +01:00
actix-extras/actix-limitation
Rob Ede 7c3c9357e0
fix expose all headers (#273)
* fix expose all headers

* update changelog
2022-08-07 20:56:33 +01:00
..
src make RateLimiter non-exhaustive 2022-07-31 03:03:43 +01:00
tests limitation clean up (#232) 2022-03-20 00:40:34 +00:00
Cargo.toml prepare actix-limitation release 0.3.0 2022-07-11 02:05:40 +01:00
CHANGES.md fix expose all headers (#273) 2022-08-07 20:56:33 +01:00
LICENSE-APACHE limitation clean up (#232) 2022-03-20 00:40:34 +00:00
LICENSE-MIT limitation clean up (#232) 2022-03-20 00:40:34 +00:00
README.md update readme 2022-07-11 02:15:47 +01:00

actix-limitation

Rate limiter using a fixed window counter for arbitrary keys, backed by Redis for Actix Web.
Originally based on https://github.com/fnichol/limitation.

crates.io Documentation Apache 2.0 or MIT licensed Dependency Status

Examples

[dependencies]
actix-web = "4"
actix-limitation = "0.3"
use std::time::Duration;
use actix_web::{get, web, App, HttpServer, Responder};
use actix_limitation::{Limiter, RateLimiter};

#[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::build("redis://127.0.0.1")
            .cookie_name("session-id".to_owned())
            .session_key("rate-api-id".to_owned())
            .limit(5000)
            .period(Duration::from_secs(3600)) // 60 minutes
            .finish()
            .expect("Can't build actix-limiter"),
    );

    HttpServer::new(move || {
        App::new()
            .wrap(RateLimiter)
            .app_data(limiter.clone())
            .service(index)
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}