Skip to main content

RateLimitStore

Trait RateLimitStore 

Source
pub trait RateLimitStore: Send + Sync {
    // Required methods
    fn check_and_increment<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        scope: &'life1 RateLimitScope,
        config: &'life2 RateLimitConfig,
    ) -> Pin<Box<dyn Future<Output = Result<RateLimitResult, RateLimitError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn check<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        scope: &'life1 RateLimitScope,
        config: &'life2 RateLimitConfig,
    ) -> Pin<Box<dyn Future<Output = Result<RateLimitResult, RateLimitError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn reset<'life0, 'life1, 'async_trait>(
        &'life0 self,
        scope: &'life1 RateLimitScope,
    ) -> Pin<Box<dyn Future<Output = Result<(), RateLimitError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn apply_penalty<'life0, 'life1, 'async_trait>(
        &'life0 self,
        scope: &'life1 RateLimitScope,
        penalty_secs: u64,
    ) -> Pin<Box<dyn Future<Output = Result<(), RateLimitError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn is_penalized<'life0, 'life1, 'async_trait>(
        &'life0 self,
        scope: &'life1 RateLimitScope,
    ) -> Pin<Box<dyn Future<Output = Result<bool, RateLimitError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}
Expand description

Trait for rate limiting storage

Implementations should use efficient counters with TTL where possible. Redis is ideal for this, but in-memory and SQL implementations are also provided.

Required Methods§

Source

fn check_and_increment<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, scope: &'life1 RateLimitScope, config: &'life2 RateLimitConfig, ) -> Pin<Box<dyn Future<Output = Result<RateLimitResult, RateLimitError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Check if an operation is allowed under the rate limit and increment counter

This is an atomic “check and increment” operation:

  • If under the limit: increment and return allowed=true
  • If at or over the limit: return allowed=false
  • Automatically handles window expiration
Source

fn check<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, scope: &'life1 RateLimitScope, config: &'life2 RateLimitConfig, ) -> Pin<Box<dyn Future<Output = Result<RateLimitResult, RateLimitError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Get current rate limit status without incrementing

Source

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

Reset rate limit for a scope (useful for admin overrides)

Source

fn apply_penalty<'life0, 'life1, 'async_trait>( &'life0 self, scope: &'life1 RateLimitScope, penalty_secs: u64, ) -> Pin<Box<dyn Future<Output = Result<(), RateLimitError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Apply a penalty (temporary ban) for a scope

Source

fn is_penalized<'life0, 'life1, 'async_trait>( &'life0 self, scope: &'life1 RateLimitScope, ) -> Pin<Box<dyn Future<Output = Result<bool, RateLimitError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Check if a scope is currently in penalty period

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§