pub struct PolicyState {
pub velocity_stamps: Vec<u64>,
pub category_spent_cents: BTreeMap<String, u64>,
}Expand description
每委托的策略运行时状态:随 commit 演化、随回放重建。
只在对应策略维度启用时才记录对应数据(未启用速率不记时刻,未设上限的类目 不记账),因此缺省策略下本结构恒空、零额外内存。时刻全量保留、不做剪枝: 与账本同阶(每笔成功放行一个 u64,审计本身也在按行增长),剪枝会破坏 「访问器 = 全量成功时刻」的直读语义。
Fields§
§velocity_stamps: Vec<u64>成功放行时刻(仅启用速率时记录;按时间升序)。
category_spent_cents: BTreeMap<String, u64>类目累计消费(仅设了上限的类目记账)。
Implementations§
Source§impl PolicyState
impl PolicyState
Sourcepub fn in_window_count(&self, now: u64, window_secs: u64) -> usize
pub fn in_window_count(&self, now: u64, window_secs: u64) -> usize
now 时刻窗口内的成功放行笔数。
一笔在 t 时刻放行,当 now - t < window_secs 时计入;恰在
t + window_secs 时刻不再计入(半开,与过期语义同形)。
Sourcepub fn record_velocity_stamp(&mut self, now: u64)
pub fn record_velocity_stamp(&mut self, now: u64)
记录一笔成功放行的时刻(仅当该委托启用了速率限制才调用——未启用
速率的委托不产生任何窗口时刻,velocity_stamps 恒空)。
Sourcepub fn record_category_spend(&mut self, category: &str, amount_cents: u64)
pub fn record_category_spend(&mut self, category: &str, amount_cents: u64)
记一笔类目消费(仅当该类目设了上限才调用——未设上限的类目没有 「类目预算」可言,不记账)。
§前提
amount_cents 的溢出必须由调用方先经 checked_add 判过(闸在阶段 5
里先验溢出再 commit);本方法不重复判,溢出 panic 会在回放与实时两侧
同位触发,不破坏「实时态 == 回放态」。
Trait Implementations§
Source§impl Clone for PolicyState
impl Clone for PolicyState
Source§fn clone(&self) -> PolicyState
fn clone(&self) -> PolicyState
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for PolicyState
impl Debug for PolicyState
Source§impl Default for PolicyState
impl Default for PolicyState
Source§fn default() -> PolicyState
fn default() -> PolicyState
Returns the “default value” for a type. Read more
Source§impl<'de> Deserialize<'de> for PolicyState
impl<'de> Deserialize<'de> for PolicyState
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl Eq for PolicyState
Source§impl PartialEq for PolicyState
impl PartialEq for PolicyState
Source§impl Serialize for PolicyState
impl Serialize for PolicyState
impl StructuralPartialEq for PolicyState
Auto Trait Implementations§
impl Freeze for PolicyState
impl RefUnwindSafe for PolicyState
impl Send for PolicyState
impl Sync for PolicyState
impl Unpin for PolicyState
impl UnsafeUnpin for PolicyState
impl UnwindSafe for PolicyState
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