Skip to content

ChorusOne/actix-extensible-rate-limit

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Actix Extensible Rate Limit

Build status crates.io docs.rs

An attempt at a more flexible rate limiting middleware for actix-web

Allows for:

  • Deriving a custom rate limit key from the request context.
  • Using dynamic rate limits and intervals determined by the request context.
  • Using custom backends (store & algorithm)
  • Setting a custom 429 response.
  • Transforming the response headers based on rate limit results (e.g x-ratelimit-remaining).
  • Rolling back rate limit counts based on response codes.

Provided Backends

Backend Algorithm Store
InMemoryBackend Fixed Window Dashmap
RedisBackend Fixed Window Redis

Getting Started

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    // A backend is responsible for storing rate limit data, and choosing whether to allow/deny requests
    let backend = InMemoryBackend::builder().build();
    HttpServer::new(move || {
        // Assign a limit of 5 requests per minute per client ip address
        let input = SimpleInputFunctionBuilder::new(Duration::from_secs(60), 5)
            .real_ip_key()
            .build();
        let middleware = RateLimiter::builder(backend.clone(), input)
            .add_headers()
            .build();
        App::new().wrap(middleware)
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

About

Flexible rate limiting middleware for actix-web

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 99.6%
  • Makefile 0.4%