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
分布式后端 trait
抽象分布式存储后端(如 Redis),提供原子计数器操作。
内存实现 InMemoryBackend 可用于单机测试和开发。
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>
原子递增并返回递增后的值
如果 key 不存在,创建并返回 1。 如果 key 存在且未过期,递增并返回新值。 如果 key 存在但已过期,重置为 1 并返回。
key:限流键window_secs:窗口大小(秒),仅在 key 新建或过期时设置 TTLwindow_start:当前窗口起始时间(Unix 秒)max_requests:窗口内最大请求数
返回 (count, reset_at_secs):
count:递增后的计数reset_at_secs:窗口重置时间(Unix 秒)
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".