Skip to main content

SlidingWindowLogLimiter

Struct SlidingWindowLogLimiter 

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

滑动窗口日志算法(Sliding Window Log)

与滑动窗口计数器不同,日志算法记录每个请求的精确时间戳, 提供更精确的限流(无边界突刺),但内存占用更高。

Implementations§

Source§

impl SlidingWindowLogLimiter

Source

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

创建滑动窗口日志限流器

Source

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

配置最大 key 数量

Source

pub fn max_requests(&self) -> u64

最大请求数

Source

pub fn window_size(&self) -> Duration

窗口大小

Source

pub fn key_count(&self) -> usize

当前 key 数量

Source

pub fn current_count(&self, key: &str) -> usize

当前窗口内请求数

Source

pub fn acquire(&self, key: &str) -> Result<RateLimitResult, RateLimitError>

尝试获取一个请求

Source

pub fn reset(&self, key: &str) -> Result<(), RateLimitError>

重置 key

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.