Skip to main content

MemoryThrottleStore

Struct MemoryThrottleStore 

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

内存后端。无后台线程,且只有写路径会清理:record_failure 顺手 retain 掉滑出窗口的失败,failure_count / is_banned 这类读路径不碰状态。

于是有两条边界:

  • 每个 key 的 failures 向量有界(每次 record_failure 都 retain,上界是 窗口内的失败数);
  • map 本身的条目数无上限 —— key 只增不减,只有 purge_expired(和 reset)会移除条目。

长期运行的进程必须按定时器调 purge_expired(间隔取 window_secs 量级即可), 否则 key 基数的增长会一直吃内存。

Implementations§

Trait Implementations§

Source§

impl Debug for MemoryThrottleStore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MemoryThrottleStore

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl ThrottleStore for MemoryThrottleStore

Source§

fn record_failure( &self, key: &str, now: u64, window_secs: u64, ) -> Result<u32, StoreError>

记录一次失败,返回窗口内的累计失败数(含本次)。 实现须自行滑动窗口:只计 now - window_secs 之后的失败。
Source§

fn failure_count( &self, key: &str, now: u64, window_secs: u64, ) -> Result<u32, StoreError>

只读查询窗口内失败数,不写入任何状态。 Read more
Source§

fn is_banned(&self, key: &str, now: u64) -> Result<Option<u64>, StoreError>

该 key 是否处于封禁中;是则返回解封时刻。 Read more
Source§

fn ban(&self, key: &str, until: u64) -> Result<(), StoreError>

写入封禁截止时刻。只能延长不能缩短:已生效的封禁遇上更早的 until 应被忽略(时钟回拨时 now + ban_secs 可能反而更小)。
Source§

fn clear_failures(&self, key: &str) -> Result<(), StoreError>

只清空失败计数,保留封禁。 Read more
Source§

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

清零该 key 的失败计数与封禁。
Source§

fn purge_expired(&self, now: u64) -> Result<usize, StoreError>

清除已过期状态,返回清除条数。

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.