Skip to main content

LimitGuard

Struct LimitGuard 

Source
pub struct LimitGuard<'a> { /* private fields */ }
Expand description

v1.4.106 codex 0538 F3 (P2): LimitGuard architecture — accepted-quota 模型而非 attempted-quota.

问题 (F3 root cause): 旧版 check_and_commit / check_full_skip_rate 在限额检查通过的瞬间直接累加 daily counter, 即便后续 backend 调用失败 退单也不会回滚 — 用户的 daily quota 被 “attempted” 单消耗 = 攻击者发 N 个 高金额无效单可耗尽合法用户 daily quota.

修法 Option B (本次实装): 引入 LimitGuard RAII pattern:

  1. RuntimeCounters::check_limits(...) — 全部检查 (含 rate + per-order cap + daily peek), 但不写 daily counter → 返 Result<LimitGuard, LimitOutcome>.
  2. caller 拿 LimitGuard, 调 backend, 成功后guard.commit_daily() 才把 daily counter 实际写入.
  3. 失败 → drop(guard) 不写 counter, 配额自然归还.
  4. rate window 在 check_limits 已 commit (rate 是请求节流不是 quota 退单不该回收 rate budget — 攻击者重试也算 rate 消耗).

legacy compat: check_and_commit / check_full_skip_rate 内部仍 调用 check_limits 链路, 但 wrapper 立即 commit daily (保留 v1.4.105 行为). 新调用方 (v1.4.106+ trade handler) 应迁移到 check_limits + 显式 commit.

Implementations§

Source§

impl<'a> LimitGuard<'a>

Source

pub fn commit_daily(self) -> Result<(), LimitOutcome>

把 pending daily delta 写入 counter (accepted-quota commit).

通常 caller 在 backend 调用 成功 后调; backend 失败时 drop(guard) 不写, daily quota 不消耗.

重要: commit_daily 是 idempotent — 同一 guard 多次调只写一次 (pending 取走后 set None). 多 caller 共享一个 guard 时安全.

返回 Ok(()) if commit 成功 (或 no-op); Err(LimitOutcome) if commit 时 cap 被超 — 不应发生 (peek 已校验过), 但并发场景下两 guard 同时 commit 可能撞 cap, 此时第二个 commit 拿 ThroughputReject.

Source

pub fn has_pending_daily(&self) -> bool

是否有 pending daily delta (false = no-op commit, e.g. mutation_no_exposure)

Trait Implementations§

Source§

impl<'a> Debug for LimitGuard<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for LimitGuard<'a>

§

impl<'a> !RefUnwindSafe for LimitGuard<'a>

§

impl<'a> Send for LimitGuard<'a>

§

impl<'a> Sync for LimitGuard<'a>

§

impl<'a> Unpin for LimitGuard<'a>

§

impl<'a> UnsafeUnpin for LimitGuard<'a>

§

impl<'a> !UnwindSafe for LimitGuard<'a>

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more