Skip to main content

FixedWindowRateLimiter

Struct FixedWindowRateLimiter 

Source
pub struct FixedWindowRateLimiter { /* private fields */ }
Expand description

固定窗口限流器

将时间划分为固定大小的窗口,每个 key 在每个窗口内有独立的计数器。 当计数器超过 max_requests 时拒绝请求。

§边界突刺

固定窗口算法存在边界突刺问题:如果窗口结束前 1 秒通过了 max_requests 个请求,新窗口开始后第 1 秒又通过了 max_requests 个请求,则 2 秒内 通过了 2 * max_requests 个请求。如需更平滑的限流,请使用 SlidingWindowRateLimiterTokenBucketRateLimiter

Implementations§

Source§

impl FixedWindowRateLimiter

Source

pub fn new(max_requests: u64, window_size: Duration) -> Self

创建固定窗口限流器

  • max_requests:每个窗口内允许的最大请求数
  • window_size:窗口大小(如 60 秒)
Source

pub fn with_max_keys(self, max_keys: usize) -> Self

配置最大 key 数量(OOM 防护)

Trait Implementations§

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.