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
impl Debug for MemoryThrottleStore
Source§impl Default for MemoryThrottleStore
impl Default for MemoryThrottleStore
Source§impl ThrottleStore for MemoryThrottleStore
impl ThrottleStore for MemoryThrottleStore
Source§fn record_failure(
&self,
key: &str,
now: u64,
window_secs: u64,
) -> Result<u32, StoreError>
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>
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>
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>
fn ban(&self, key: &str, until: u64) -> Result<(), StoreError>
写入封禁截止时刻。只能延长不能缩短:已生效的封禁遇上更早的
until
应被忽略(时钟回拨时 now + ban_secs 可能反而更小)。Source§fn clear_failures(&self, key: &str) -> Result<(), StoreError>
fn clear_failures(&self, key: &str) -> Result<(), StoreError>
只清空失败计数,保留封禁。 Read more
Source§fn purge_expired(&self, now: u64) -> Result<usize, StoreError>
fn purge_expired(&self, now: u64) -> Result<usize, StoreError>
清除已过期状态,返回清除条数。
Auto Trait Implementations§
impl !Freeze for MemoryThrottleStore
impl RefUnwindSafe for MemoryThrottleStore
impl Send for MemoryThrottleStore
impl Sync for MemoryThrottleStore
impl Unpin for MemoryThrottleStore
impl UnsafeUnpin for MemoryThrottleStore
impl UnwindSafe for MemoryThrottleStore
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