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§

header
The headers used when a RocketGovernor guarded path responds with TooManyRequests.

Structs§

LimitHeaderGen
Provides Fairing implementation which is attachable to Rocket-instance.
Quota
A rate-limiting quota.
ReqState
ReqState is the data struct to handle information about Quota and limits in the Request state.
RocketGovernor
Generic RocketGovernor implementation.

Enums§

LimitError
Errors for governed requests which implement Responder.
Method
Representation of HTTP methods.

Traits§

RocketGovernable
The RocketGovernable guard trait.

Functions§

rocket_governor_catcher
A default implementation for Rocket Catcher handling HTTP TooManyRequests responses.

Type Aliases§

NonZeroU32
A u32 that is known not to equal zero.