Skip to main content

RateLimitCounter

Trait RateLimitCounter 

Source
pub trait RateLimitCounter: Send + Sync {
    // Required methods
    fn increment<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
        amount: u64,
        window_secs: u64,
    ) -> Pin<Box<dyn Future<Output = Result<u64, StorageError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn current<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<u64, StorageError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn reset<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<(), StorageError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
}
Expand description

Atomic counters keyed by an arbitrary string, used for rate limiting.

The defining operation is increment: it must apply the read-modify-write atomically so two concurrent callers can never observe or commit the same pre-increment value. Counters are scoped to a fixed-length window; window_secs lets the backend bucket and expire counts without the caller tracking wall-clock time.

§Example

use aa_core::storage::{RateLimitCounter, Result};
use async_trait::async_trait;

/// A counter that always reports a single hit (a stand-in for a real backend).
struct AlwaysOne;

#[async_trait]
impl RateLimitCounter for AlwaysOne {
    async fn increment(&self, _key: &str, _amount: u64, _window_secs: u64) -> Result<u64> {
        Ok(1)
    }

    async fn current(&self, _key: &str) -> Result<u64> {
        Ok(1)
    }

    async fn reset(&self, _key: &str) -> Result<()> {
        Ok(())
    }
}

Required Methods§

Source

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

Atomically add amount to the counter for key within the window of length window_secs, returning the new total for the current window.

The read-modify-write is atomic with respect to concurrent callers.

Source

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

Return the current total for key without modifying it.

Returns 0 for a key that has never been incremented (or whose window has expired).

Source

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

Reset the counter for key back to zero.

Idempotent: resetting an absent key succeeds.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§