1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-23 23:51:06 +01:00
actix-extras/actix-limitation
dependabot[bot] 2ede588693
build(deps): update redis requirement from 0.26 to 0.27 (#463)
* build(deps): update redis requirement from 0.26 to 0.27

Updates the requirements on [redis](https://github.com/redis-rs/redis-rs) to permit the latest version.
- [Release notes](https://github.com/redis-rs/redis-rs/releases)
- [Commits](https://github.com/redis-rs/redis-rs/compare/redis-0.26.0...redis-0.27.2)

---
updated-dependencies:
- dependency-name: redis
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

* docs: update changelogs

* ci: fix doc

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Rob Ede <robjtede@icloud.com>
2024-10-11 13:10:39 +00:00
..
src build(deps): update derive_more to v1.0 (#458) 2024-08-18 14:21:56 +00:00
tests clippy 2023-01-07 01:04:16 +00:00
Cargo.toml build(deps): update redis requirement from 0.26 to 0.27 (#463) 2024-10-11 13:10:39 +00:00
CHANGES.md build(deps): update redis requirement from 0.26 to 0.27 (#463) 2024-10-11 13:10:39 +00: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 chore: fmt markdowns 2024-01-06 21:08:09 +00: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.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
}