#rate-limiting #actix-web-middleware #middleware #actix-web #actix-middleware

limitation-actix-middleware

An Actix web middleware for rate limiting requests using a fixed window counter keyed on a header

2 releases

0.1.1 Oct 20, 2019
0.1.0 Oct 20, 2019

#7 in #rate-limiter

22 downloads per month
Used in limitation-proxy

MPL-2.0 license

73KB
494 lines

limitation-actix-middleware

CI CI Status
Latest Version Latest version
Documentation Documentation
Crate Downloads Crate downloads
License Crate license

Table of Contents

An Actix web middleware for rate limiting requests using a fixed window counter keyed on a header.

Usage

Add limitation-actix-middleware to your Cargo.toml:

[dependencies]
limitation-actix-middleware = "0.1.1"

Quick Example

The RateLimiter middleware is the primary type which is intended to be inserted in an Actix web app's middleware chain. The middleware requires 2 Data types to be present:

  1. A HeaderName which is the header to use as the rate limiter key
  2. A Limiter which performs the rate limiting and manages persistence
use actix_web::{http::header::HeaderName, web, App, HttpResponse};
use limitation_actix_middleware::{Limiter, RateLimiter};

// Choose a header to use for rate limit tracking
let header = web::Data::new(HeaderName::from_static("authorization"));
// Build a `Limiter` which will be used by the middleware
let limiter = web::Data::new(Limiter::build("redis://127.0.0.1/").finish()?);

let app = App::new()
    // Register the header as application data
    .register_data(header.clone())
    // Register the Limiter as application data
    .register_data(limiter.clone())
    // Insert the RateLimter middleware
    .wrap(RateLimiter)
    .service(
        web::resource("/test")
            .route(web::get().to(|| HttpResponse::Ok()))
            .route(web::head().to(|| HttpResponse::MethodNotAllowed()))
    );

Examples

This crate ships with an example program called catchall which can be run from the sources with:

$ cargo run --example catchall

CI Status

Build (master branch)

Operating System Stable Rust Nightly Rust MSRV
FreeBSD FreeBSD Stable Build Status FreeBSD Nightly Build Status FreeBSD Oldest Build Status
Linux Linux Stable Build Status Linux Nightly Build Status Linux Oldest Build Status
macOS macOS Stable Build Status macOS Nightly Build Status macOS Oldest Build Status
Windows Windows Stable Build Status Windows Nightly Build Status Windows Oldest Build Status

Test (master branch)

Operating System Stable Rust Nightly Rust MSRV
FreeBSD FreeBSD Stable Test Status FreeBSD Nightly Test Status FreeBSD Oldest Test Status
Linux Linux Stable Test Status Linux Nightly Test Status Linux Oldest Test Status
macOS macOS Stable Test Status macOS Nightly Test Status macOS Oldest Test Status
Windows Windows Stable Test Status Windows Nightly Test Status Windows Oldest Test Status

Check (master branch)

Status
Lint Lint Status
Format Format Status

Code of Conduct

This project adheres to the Contributor Covenant code of conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to fnichol@nichol.ca.

Issues

If you have any problems with or questions about this project, please contact us through a GitHub issue.

Contributing

You are invited to contribute to new features, fixes, or updates, large or small; we are always thrilled to receive pull requests, and do our best to process them as fast as we can.

Before you start to code, we recommend discussing your plans through a GitHub issue, especially for more ambitious contributions. This gives other contributors a chance to point you in the right direction, give you feedback on your design, and help you find out if someone else is working on the same thing.

Release History

See the changelog for a full release history.

Authors

Created and maintained by Fletcher Nichol (fnichol@nichol.ca).

License

Licensed under the Mozilla Public License Version 2.0 (LICENSE.txt).

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the MPL-2.0 license, shall be licensed as above, without any additional terms or conditions.

Dependencies

~25MB
~543K SLoC