Crate rocket_governor

source ·
Expand description

§rocket-governor - rate-limiting implementation for Rocket web framework

Provides the rocket guard implementing rate-limiting (based on governor).

Declare a struct and use it with the generic RocketGovernor guard.
This requires to implement trait RocketGovernable for your struct.

§Example

use rocket::{catchers, get, http::Status, launch, routes};
use rocket_governor::{rocket_governor_catcher, Method, Quota, RocketGovernable, RocketGovernor};

pub struct RateLimitGuard;

impl<'r> RocketGovernable<'r> for RateLimitGuard {
    fn quota(_method: Method, _route_name: &str) -> Quota {
        Quota::per_second(Self::nonzero(1u32))
    }
}

#[get("/")]
fn route_example(_limitguard: RocketGovernor<RateLimitGuard>) -> Status {
    Status::Ok
}

#[launch]
fn launch_rocket() -> _ {
    rocket::build()
        .mount("/", routes![route_example])
        .register("/", catchers![rocket_governor_catcher])
}

See rocket-governor Github project for more information.

§Features

§Optional feature limit_info

There is the optional feature limit_info which enables reporting about rate limits in HTTP headers of requests.

The implementation is based on headers of https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-ratelimit-headers. The feature provides a default implementation of a Rocket fairing which need to be used to get the HTTP headers set.

See API documentation for LimitHeaderGen.

For usage depend on it in Cargo.toml

[dependencies]
rocket-governor = { version = "...", features = ["limit_info"] }

§Optional feature logger

There is the optional feature logger which enables some logging output.

For usage depend on it in Cargo.toml

[dependencies]
rocket-governor = { version = "...", features = ["logger"] }

Modules§

Structs§

Enums§

Traits§

Functions§

Type Aliases§

  • An integer that is known not to equal zero.