From 0cba6fd6d583e0904b12dcd30209a368023bd7ea Mon Sep 17 00:00:00 2001 From: huang Date: Thu, 16 Apr 2026 18:33:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=BD=AE=E8=AF=A2?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E7=AB=9E=E6=80=81=E5=AF=BC=E8=87=B4=E6=B5=81?= =?UTF-8?q?=E9=87=8F=E5=A2=9E=E9=87=8F=E9=87=8D=E5=A4=8D=E8=AE=A1=E5=85=A8?= =?UTF-8?q?=E9=87=8F=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit getCardWithCache 在缓存 miss 时将 writeCardToCache 由异步协程改为同步调用。 原实现中协程可能在 updateCardCache 之后才被调度执行,将 last_gateway_reading_mb 覆盖回 DB 旧值(0),导致下次轮询 increment = gatewayFlowMB - 0 = 全量,触发套餐流量重复扣减。 Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- internal/task/polling_card_cache.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/task/polling_card_cache.go b/internal/task/polling_card_cache.go index 0017163..08e1dc5 100644 --- a/internal/task/polling_card_cache.go +++ b/internal/task/polling_card_cache.go @@ -25,7 +25,10 @@ func (b *PollingBase) getCardWithCache(ctx context.Context, cardID uint) (*model return nil, err } - go writeCardToCache(b, key, card) + // 同步写缓存,避免与后续 updateCardCache 产生竞态: + // 若异步协程在 updateCardCache 之后才执行,会将 last_gateway_reading_mb + // 覆盖回 DB 旧值(0),导致下次轮询增量重复计全量。 + writeCardToCache(b, key, card) return card, nil }