A Tower service and layer that provides a rate-limiting backend by governor. Based heavily on the work done for actix-governor. Works with Axum, Hyper, Tonic, and anything else based on Tower!

# Features:

async fn hello() -> &'static str { "Hello world" }

[tokio::main]

async fn main() { // Configure tracing if desired // construct a subscriber that prints formatted traces to stdout let subscriber = tracingsubscriber::FmtSubscriber::new(); // use that subscriber to process traces emitted after this point tracing::subscriber::setglobal_default(subscriber).unwrap();

// Allow bursts with up to five requests per IP address
// and replenishes one element every two seconds
// We Box it because Axum 0.6 requires all Layers to be Clone
// and thus we need a static reference to it
let governor_conf = Box::new(
    GovernorConfigBuilder::default()
        .per_second(2)
        .burst_size(5)
        .finish()
        .unwrap(),
);

// build our application with a route
let app = Router::new()
    // `GET /` goes to `root`
    .route("/", get(hello))
    .layer(
        ServiceBuilder::new()
            // this middleware goes above `GovernorLayer` because it will receive
            // errors returned by `GovernorLayer`
            .layer(HandleErrorLayer::new(|e: BoxError| async move {
                display_error(e)
            }))
            .layer(GovernorLayer {
                // We can leak this because it is created once and then
                config: Box::leak(governor_conf),
            }),
    );

// run our app with hyper
// `axum::Server` is a re-export of `hyper::Server`
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
    .serve(app.into_make_service_with_connect_info::<SocketAddr>())
    .await
    .unwrap();

} ```

# Configuration presets

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