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) -> FixedWindowRateLimiter
pub fn new(max_requests: u64, window_size: Duration) -> FixedWindowRateLimiter
创建固定窗口限流器
max_requests:每个窗口内允许的最大请求数window_size:窗口大小(如 60 秒)
Sourcepub fn with_max_keys(self, max_keys: usize) -> FixedWindowRateLimiter
pub fn with_max_keys(self, max_keys: usize) -> FixedWindowRateLimiter
配置最大 key 数量(OOM 防护)
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more