[][src]Crate actix_governor

An middleware for actix-web that provides rate-limiting backed by governor.

Features:

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

How does it work?

Each governor middleware has a configuration that stores a quota. The quota specifies how many requests can be send from a 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 the 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 an 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
}

Structs

Governor

Governor middleware factory.

GovernorConfig

Configuration for the Governor middleware.

GovernorConfigBuilder

Helper struct for building a configuration for the governor middleware

GovernorMiddleware