Skip to main content

DistributedBackend

Trait DistributedBackend 

Source
pub trait DistributedBackend: Send + Sync {
    // Required methods
    fn incr_and_get(
        &self,
        key: &str,
        window_secs: u64,
        window_start: i64,
        max_requests: u64,
    ) -> Result<(u64, i64), RateLimitError>;
    fn get(&self, key: &str) -> Result<Option<(u64, i64)>, RateLimitError>;
    fn reset_key(&self, key: &str) -> Result<(), RateLimitError>;
}
Expand description

Distributed backend trait

Abstracts a distributed storage backend (such as Redis) and provides atomic counter operations. The in-memory implementation InMemoryBackend can be used for single-machine testing and development.

Required Methods§

Source

fn incr_and_get( &self, key: &str, window_secs: u64, window_start: i64, max_requests: u64, ) -> Result<(u64, i64), RateLimitError>

Atomically increments and returns the value after increment

If the key does not exist, creates it and returns 1. If the key exists and has not expired, increments and returns the new value. If the key exists but has expired, resets to 1 and returns.

  • key: rate limit key
  • window_secs: window size (seconds); TTL is set only when the key is newly created or expired
  • window_start: current window start time (Unix seconds)
  • max_requests: maximum number of requests within the window

Returns (count, reset_at_secs):

  • count: the count after increment
  • reset_at_secs: window reset time (Unix seconds)
Source

fn get(&self, key: &str) -> Result<Option<(u64, i64)>, RateLimitError>

Returns the current count (without incrementing)

Source

fn reset_key(&self, key: &str) -> Result<(), RateLimitError>

Resets the key (deletes it)

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§