[][src]Crate actix_governor

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


  • 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.


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

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

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()

    HttpServer::new(move || {
            // Enable Governor middleware
            // Route hello world service
            .route("/", web::get().to(index))

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()

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.



Governor middleware factory.


Configuration for the Governor middleware.


Helper struct for building a configuration for the governor middleware