pub struct FixedWindowRateLimiter { /* private fields */ }Expand description
固定窗口限流器
将时间划分为固定大小的窗口,每个 key 在每个窗口内有独立的计数器。
当计数器超过 max_requests 时拒绝请求。
§边界突刺
固定窗口算法存在边界突刺问题:如果窗口结束前 1 秒通过了 max_requests
个请求,新窗口开始后第 1 秒又通过了 max_requests 个请求,则 2 秒内
通过了 2 * max_requests 个请求。如需更平滑的限流,请使用
SlidingWindowRateLimiter 或 TokenBucketRateLimiter。
Implementations§
Source§impl FixedWindowRateLimiter
impl FixedWindowRateLimiter
Sourcepub fn new(max_requests: u64, window_size: Duration) -> Self
pub fn new(max_requests: u64, window_size: Duration) -> Self
创建固定窗口限流器
max_requests:每个窗口内允许的最大请求数window_size:窗口大小(如 60 秒)
Sourcepub fn with_max_keys(self, max_keys: usize) -> Self
pub fn with_max_keys(self, max_keys: usize) -> Self
配置最大 key 数量(OOM 防护)
pub fn max_requests(&self) -> u64
pub fn window_size(&self) -> Duration
pub fn max_keys(&self) -> usize
pub fn key_count(&self) -> usize
Trait Implementations§
Source§impl RateLimiter for FixedWindowRateLimiter
impl RateLimiter for FixedWindowRateLimiter
fn acquire(&self, key: &str) -> Result<RateLimitResult, RateLimitError>
fn try_acquire(&self, key: &str) -> Result<RateLimitResult, RateLimitError>
fn reset(&self, key: &str) -> Result<(), RateLimitError>
Auto Trait Implementations§
impl Freeze for FixedWindowRateLimiter
impl RefUnwindSafe for FixedWindowRateLimiter
impl Send for FixedWindowRateLimiter
impl Sync for FixedWindowRateLimiter
impl Unpin for FixedWindowRateLimiter
impl UnsafeUnpin for FixedWindowRateLimiter
impl UnwindSafe for FixedWindowRateLimiter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more