Repository navigation
修复:上游报「积分余额不足」时把账号积分读数按 0 落库(#651) #652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: beta
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -188,6 +188,37 @@ pub fn balance_facts() -> HashMap<String, BalanceFact> { | |
|
|
||
| // ─── 写 ────────────────────────────────────────────────────── | ||
|
|
||
| /// 上游明确说「余额/积分不足」时,把账号余额读数**按 0 落一次**。 | ||
| /// | ||
| /// 为什么不信本地的余额查询:积分靠上游接口读,字段形状一变、或者把「钱包」 | ||
| /// 当成积分池,就会读出一个明显偏高的数字。限制器和低余额跳过都按这个数字 | ||
| /// 判断,于是账号一直显得「有钱」——每条请求都被派上去,再稳定吃一次 402 | ||
| /// `积分余额不足`。上游的 402 是比本地读数**更权威的事实**:这里把它直接落成 | ||
| /// 0,余额列跟着显示 0、低余额规则立刻拦下它,等下一次成功查询(或用户充值 | ||
| /// 后重新读到真余额)再自动恢复 —— 不需要人去手动改数。 | ||
| /// | ||
| /// 只覆盖余额(`totalLeft` / `available`),不动 `query_config` 与查询配置, | ||
| /// 所以下一次查询照常进行,读数一旦对得上就把 0 顶掉。 | ||
| pub fn force_zero_balance(account_id: &str, reason: &str) { | ||
| if account_id.is_empty() { return; } | ||
| let current = load(account_id); | ||
| let unit = current.usage.as_ref().and_then(|usage| usage.get("unit")).and_then(Value::as_str) | ||
| .filter(|unit| !unit.is_empty()).unwrap_or("积分"); | ||
| let usage = serde_json::json!({ | ||
| "unit": unit, | ||
| "available": 0.0, | ||
| "remaining": 0.0, | ||
| "totalLeft": 0.0, | ||
| "note": reason, | ||
| "forcedZeroAt": logging::now_ms(), | ||
| }); | ||
| let outcome = UsageOutcome { usage: Some(usage), error: None, code: None, diagnostics: None }; | ||
| // 失败不当错误:写不进去只是这次没落上 0,冷却与熔断仍然照常生效。 | ||
| if !write_result(account_id, &outcome, ¤t.query_config, "upstream-balance-zero") { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 让归零和正常查询共用同一个提交边界。 正常查询可能先把余额 10 写入数据库,但尚未更新内存。此时归零写入数据库和内存;随后正常查询继续发布 10。最终数据库为 0,限制器和 请在公共写入层按账号串行提交。例如,让正常查询和归零都在同一个账号锁内完成数据库写入及三份事实更新,而不是只锁新增函数。 按路径要求,这应从“更底层的数据源、公共组件或状态流转层面”解决。根据检索学习,也需要防止旧结果覆盖较新的修改。
|
||
| logging::verbose("[Usage]", &format!("账号 {account_id} 的余额按 0 落库失败(上游已报余额不足)")); | ||
| } | ||
| } | ||
|
|
||
| /// 记一笔「尝试开始」(心跳路径在**发请求之前**调用,见模块头)。 | ||
| /// 只推进时间戳,不动上一次的结果 —— 查询还没回来,旧读数仍是最新事实。 | ||
| pub fn mark_attempt(account_id: &str) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
归零时保留其他额度来源。
这里新建整份
usage,会删除原有quotaSources。write_result随后用它替换限制器事实。比如账号配置了“daily 剩余额度小于 1 时跳过”,原读数为 0;归零后,evaluate::read找不到 daily,规则反而不再命中。条件中保存的来源元数据不能补回实时读数。(raw.githubusercontent.com)请基于原记录更新余额字段,并保留其他额度来源。若存在账号主额度来源,也要同步其余额,避免两套读数冲突。没有历史记录时,再构造最小零余额记录。