Skip to content

A rate limiter using a fixed window counter for arbitrary keys, backed by Redis for actix-web.

Notifications You must be signed in to change notification settings

0xmad/actix-limitation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Actix Limitation

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.

Example

[dependencies]
actix-limitation = "0.1.3"
actix-web = "2.0.0"
actix-rt = "1.1.1"

Code:

use actix_web::{get, web, App, HttpServer, Responder};
use actix_limitation::{Limiter, RateLimiter};
use std::time::Duration;

#[get("/{id}/{name}/index.html")]
async fn index(info: web::Path<(u32, String)>) -> impl Responder {
    format!("Hello {}! id:{}", info.1, info.0)
}

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    let limiter = web::Data::new(
        Limiter::build("redis://127.0.0.1")
            .cookie_name("session-id")
            .session_key("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
}

About

A rate limiter using a fixed window counter for arbitrary keys, backed by Redis for actix-web.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages