[][src]Crate actix_governor

A middleware for actionable that provides rate-limiting backed by governor.

Features:

  • Simple to use
  • High performance
  • Robust yet flexible API
  • Actively maintained as part of the Triox-Project

How does it work?

Each governor middleware has a configuration that stores a quota. The quota specifies how many requests can be sent from an IP address before the middleware starts blocking further requests.

For example if the quota allowed ten requests a client could send a burst of ten requests in short time before the middleware starts blocking.

Once at least one element of the quota was used the elements of the quota will be replenished after a specified period.

For example if this period was 2 seconds and the quota was empty it would take 2 seconds to replenish one element of the quota. This means you could send one request every two seconds on average.

If there was a quota that allowed ten requests with the same period a client could again send a burst of ten requests and then had to wait two seconds before sending further requests or 20 seconds before the full quota would be replenished and he could send another burst.

Example

use actix_governor::{Governor, GovernorConfigBuilder};
use actix_web::{web, App, HttpServer, Responder};

async fn index() -> impl Responder {
    "Hello world!"
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    // Allow bursts with up to five requests per IP address
    // and replenishes one element every two seconds
    let governor_conf = GovernorConfigBuilder::default()
        .per_second(2)
        .burst_size(5)
        .finish()
        .unwrap();

    HttpServer::new(move || {
        App::new()
            // Enable Governor middleware
            .wrap(Governor::new(&governor_conf))
            // Route hello world service
            .route("/", web::get().to(index))
   })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

Configuration presets

Instead of using the configuration builder you can use predefined presets.

  • GovernorConfig::default(): The default configuration which is suitable for most services. Allows bursts with up to eight requests and replenishes one element after 500ms.

  • GovernorConfig::secure(): A default configuration for security related services. Allows bursts with up to two requests and replenishes one element after four seconds.

For example the secure configuration can be used as a short version of this code:

use actix_governor::GovernorConfigBuilder;

let config = GovernorConfigBuilder::default()
    .per_second(4)
    .burst_size(2)
    .finish()
    .unwrap();

Common pitfalls

Do not construct the same configuration multiple times, unless explicitly wanted! This will create an independent rate limiter for each configuration!

Instead pass the same configuration reference into Governor::new(), like it is described in the example.

Structs

Governor

Governor middleware factory.

GovernorConfig

Configuration for the Governor middleware.

GovernorConfigBuilder

Helper struct for building a configuration for the governor middleware

GovernorMiddleware