Skip to main content

ThrottleKey

Trait ThrottleKey 

Source
pub trait ThrottleKey:
    Send
    + Sync
    + 'static {
    // Required method
    fn throttle_key(
        ctx: &RequestContext,
    ) -> impl Future<Output = Result<String, Error>> + Send;
}
Expand description

Produces the key a request is rate-limited by (the “tracker”).

The default is the client IP (ByIp). Implement this on a unit type to key by something else — a user id, an API key, a tenant — reusing dependency injection inside, since it has the full RequestContext:

struct ByUser;
impl ThrottleKey for ByUser {
    async fn throttle_key(ctx: &RequestContext) -> tork::Result<String> {
        Ok(CurrentUser::from_request(ctx).await?.id.to_string())
    }
}

Required Methods§

Source

fn throttle_key( ctx: &RequestContext, ) -> impl Future<Output = Result<String, Error>> + Send

Returns the key for ctx.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§