pub trait Backend<I: 'static = SimpleInput>: Clone {
    type Output;
    type RollbackToken;
    type Error;

    // Required methods
    fn request(
        &self,
        input: I
    ) -> impl Future<Output = Result<(Decision, Self::Output, Self::RollbackToken), Self::Error>>;
    fn rollback(
        &self,
        token: Self::RollbackToken
    ) -> impl Future<Output = Result<(), Self::Error>>;
}
Expand description

Describes an implementation of a rate limiting store and algorithm.

A Backend is required to implement Clone, usually this means wrapping your data store within an Arc, although many connection pools already do so internally; there is no need to wrap it twice.

Required Associated Types§

Required Methods§

source

fn request( &self, input: I ) -> impl Future<Output = Result<(Decision, Self::Output, Self::RollbackToken), Self::Error>>

Process an incoming request.

The input could include such things as a rate limit key, and the rate limit policy to be applied.

Returns a boolean of whether to allow or deny the request, arbitrary output that can be used to transform the allowed and denied responses, and a token to allow the rate limit counter to be rolled back in certain conditions.

source

fn rollback( &self, token: Self::RollbackToken ) -> impl Future<Output = Result<(), Self::Error>>

Under certain conditions we may not want to rollback the request operation.

E.g. We may want to exclude 5xx errors from counting against a user’s rate limit, we can only exclude them after having already allowed the request through the rate limiter in the first place, so we must therefore deduct from the rate limit counter afterwards.

Note that if this function fails there is not much the RateLimiter can do about it, given that the request has already been allowed.

Arguments

Object Safety§

This trait is not object safe.

Implementors§