Skip to main content

DistributedRateLimitBackend

Trait DistributedRateLimitBackend 

Source
pub trait DistributedRateLimitBackend: Send + Sync {
    // Required methods
    fn acquire<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
        cost: f64,
        params: RateLimitParams,
    ) -> Pin<Box<dyn Future<Output = Result<AcquireOutcome>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn available<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
        params: RateLimitParams,
    ) -> Pin<Box<dyn Future<Output = Result<f64>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn time_until_available<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
        cost: f64,
        params: RateLimitParams,
    ) -> Pin<Box<dyn Future<Output = Result<Duration>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn reset<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn backend_name(&self) -> &str;
}
Expand description

Shared token / counter store for distributed rate limiting.

Implementations must guarantee that acquire is atomic per key: the read, refill, and conditional decrement must happen without interleaving from other callers acting on the same key. This is what makes cluster-wide enforcement correct under concurrency.

A Redis-backed implementation can satisfy this by executing the equivalent Lua scripts (see crate::rate_limit::DistributedTokenBucketSpec and crate::rate_limit::DistributedSlidingWindowSpec) which Redis runs atomically.

Required Methods§

Source

fn acquire<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, cost: f64, params: RateLimitParams, ) -> Pin<Box<dyn Future<Output = Result<AcquireOutcome>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Atomically attempt to acquire cost permits for key.

The backend applies refill / windowing using params before deciding.

§Errors

Returns an error if the underlying store is unavailable.

Source

fn available<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, params: RateLimitParams, ) -> Pin<Box<dyn Future<Output = Result<f64>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Report the currently available permits for key (after refill).

§Errors

Returns an error if the underlying store is unavailable.

Source

fn time_until_available<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, cost: f64, params: RateLimitParams, ) -> Pin<Box<dyn Future<Output = Result<Duration>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Estimate the time until at least cost permits are available for key.

§Errors

Returns an error if the underlying store is unavailable.

Source

fn reset<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Clear all stored state for key.

§Errors

Returns an error if the underlying store is unavailable.

Source

fn backend_name(&self) -> &str

Backend name, for diagnostics.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§