[][src]Crate limitation_actix_middleware

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.0"

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

Structs

Builder

A builder for a Limiter.

Limiter

A rate limiter using a fixed window counter, backed by Redis.

RateLimiter

Middleware for rate limiting requests using a fixed window counter keyed on a HeaaderName.

Status

A report for a given key containing the limit status.

Enums

Error

Error type for this crate.