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§
Sourcefn incr_and_get(
&self,
key: &str,
window_secs: u64,
window_start: i64,
max_requests: u64,
) -> Result<(u64, i64), RateLimitError>
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 keywindow_secs: window size (seconds); TTL is set only when the key is newly created or expiredwindow_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 incrementreset_at_secs: window reset time (Unix seconds)
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".