pub struct RuntimeCounters { /* private fields */ }Implementations§
Source§impl RuntimeCounters
impl RuntimeCounters
pub fn new() -> Self
Sourcepub fn check_limits<'a>(
&'a self,
key_id: &str,
limits: &Limits,
ctx: &CheckCtx,
now: DateTime<Utc>,
commit_rate: bool,
) -> Result<LimitGuard<'a>, LimitOutcome>
pub fn check_limits<'a>( &'a self, key_id: &str, limits: &Limits, ctx: &CheckCtx, now: DateTime<Utc>, commit_rate: bool, ) -> Result<LimitGuard<'a>, LimitOutcome>
v1.4.106 codex 0538 F3 (P2): accepted-quota architecture entry.
跑全部限额检查 (whitelist + per-order cap + rate + daily peek), 但不
累加 daily counter — 返 LimitGuard 让 caller 在 backend 成功后
显式 commit_daily().
失败 → drop(guard) 不写 daily counter, 配额自然归还 (与 legacy
check_and_commit 在失败前就累加的 attempted-quota 行为相反).
rate 在本函数仍 commit (rate 是节流不是 quota, 失败重试也算消耗).
参数:
commit_rate: true = 跑 rate window 累加 (auth middleware 入口); false = 跳过 rate (handler 层重入, 等同check_full_skip_rate).
Sourcepub fn check_and_commit(
&self,
key_id: &str,
limits: &Limits,
ctx: &CheckCtx,
now: DateTime<Utc>,
) -> LimitOutcome
pub fn check_and_commit( &self, key_id: &str, limits: &Limits, ctx: &CheckCtx, now: DateTime<Utc>, ) -> LimitOutcome
执行全部限额检查;通过则(若提供 order_value)累加日计数 + 记录速率窗口时间戳
检查顺序:市场 → 品种 → 方向 → 时间窗 → 单笔 → 速率 → 日累计。 前面的便宜检查先跑;日累计放最后是因为它有副作用(累加), 前面 reject 就不该动计数器。
v1.4.106 codex 0538 F3: 此 wrapper 保持 v1.4.105 attempted-quota
行为 (legacy compat). 新代码应迁移到 Self::check_limits +
LimitGuard::commit_daily 走 accepted-quota 模型.
Sourcepub fn check_full_skip_rate(
&self,
key_id: &str,
limits: &Limits,
ctx: &CheckCtx,
now: DateTime<Utc>,
) -> LimitOutcome
pub fn check_full_skip_rate( &self, key_id: &str, limits: &Limits, ctx: &CheckCtx, now: DateTime<Utc>, ) -> LimitOutcome
handler 层细粒度检查:跑 market / symbol / trd_side / hours / per_order / daily 全套,但跳过 rate —— rate 已经在 auth 中间件层(v1.0) commit 过了,handler 再 commit 一次会让 rate 窗口计 2 次。
典型用法:REST /api/order 路由 / gRPC request(2202) 这种 handler
已经知道完整下单参数(market/symbol/value/side),调用方先在 middleware
跑 rate+hours 全局闸门(check_and_commit with empty CheckCtx),过了
再在 handler 里跑这个方法做细粒度检查。
注意:daily 计数器会累加 —— 这是必须的,因为 rate 不能算“额度“, daily 才是真实金额额度。