DaydreamCoding
f7f5e33830
feat(quota): user×platform 配额 DB 写聚合 flusher
Redis 同步权威 + DB 镜像,不在进程内维护 delta:
- 写入点 HasUserPlatformQuotaLimit 守卫:无 limit 跳过 Redis 写与持久化
- 累加 usage 的 Lua 在 flusher_enabled 时 SADD 脏集 billing:upq:dirty
- UserPlatformQuotaUsageFlusher 定时 SPOP 脏集 → 批量 HGETALL 读当前窗口 usage 快照
→ BatchSnapshotUsage 绝对值 UPSERT 覆盖 DB(去 SELECT FOR UPDATE 行锁)
→ 失败 SADD 回 / FK(23503)整批丢弃
- flusher 单批 clamp 到 ≤6000,保证一次 flush 只生成一条 UPSERT(单事务原子)
- flusher_enabled 默认 false(降级=旧异步直写 DB)
效果:DB 写连接从 O(QPS) 收敛到 O(副本)。
循环依赖:service 层独立 Snapshot/FK 类型,repository adapter 转换 + %w 映射 FK error。
admin reset/upsert 后失效 cache(脏残留被 flusher 当 MISS 跳过)。
健壮性与可观测性:
- flusher_enabled=false 时 Start 不注册定时器;flush_interval_ms 非法回退 2s
- Readd 回填失败单独计 dirty_lost(不再误记 dirty_readd)并 ALERT;脏集 Readd 补兜底 TTL
- 单 tick 达 max batches 上限仍有积压时记 log
- admin 失效 cache 失败升级为 ALERT(提示 enforcement 可能延迟至 sentinel TTL)
- BatchGet 单条命令失败 / usage 字段损坏均记 log,避免静默以 0 覆写 DB
三态 go vet + 单测/集成测全绿。
已知取舍(默认 flusher_enabled=false 不触发):
- FK 整批丢弃牵连同批正常 key(活跃 key 靠下次 SADD+绝对值快照自愈;Redis 仍权威)
- admin reset/upsert 直写 DB 与 flusher 异步刷存在覆盖竞态:flusher 持旧快照在途时可能覆盖
admin 刚写值(limit 列不受影响;usage 有 preflight windowExpired 兜底;低频)。彻底消除需 version OCC。
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-05-29 17:19:15 +08:00
..
2026-05-08 11:36:09 +08:00
2026-05-11 11:43:44 +08:00
2026-04-09 18:14:28 +08:00
2026-05-08 03:44:04 +08:00
2026-05-18 19:01:33 +08:00
2026-05-26 19:59:12 +08:00
2026-04-27 17:32:34 +08:00
2026-02-28 20:38:35 +08:00
2026-05-26 19:46:08 +08:00
2026-03-04 15:22:46 +08:00
2026-05-27 18:00:45 +08:00
2026-04-28 11:15:09 +08:00
2026-05-27 18:00:45 +08:00
2026-05-03 20:33:13 +08:00
2026-04-09 18:14:28 +08:00
2026-04-09 18:14:28 +08:00
2026-02-11 01:23:21 +08:00
2026-04-27 16:47:44 +08:00
2026-04-27 16:47:44 +08:00
2026-03-16 20:22:10 +08:00
2026-02-28 20:38:35 +08:00
2026-05-19 20:32:32 +08:00
2026-05-19 20:32:32 +08:00
2026-05-19 22:05:43 +08:00
2026-05-19 22:05:43 +08:00
2026-05-26 13:58:02 +08:00
2026-03-11 16:46:58 +08:00
2026-03-17 19:26:30 +08:00
2026-03-17 19:26:30 +08:00
2026-05-19 15:25:34 +08:00
2026-03-17 19:26:11 +08:00
2026-03-17 19:26:11 +08:00
2026-02-28 15:01:20 +08:00
2026-02-28 15:05:54 +08:00
2026-02-10 11:42:39 +08:00
2026-02-24 20:04:05 +08:00
2026-05-27 18:00:45 +08:00
2026-03-04 15:22:46 +08:00
2026-03-04 13:45:49 +08:00
2026-02-23 12:45:37 +08:00
2026-02-23 12:45:37 +08:00
2026-04-05 17:11:01 +08:00
2026-03-10 11:29:31 +08:00
2026-02-12 14:20:14 +08:00
2026-05-19 19:37:41 +08:00
2026-02-12 16:27:29 +08:00
2026-02-12 16:27:29 +08:00
2026-03-04 13:45:49 +08:00
2026-02-12 16:25:44 +08:00
2026-02-12 16:27:29 +08:00
2026-02-28 15:01:20 +08:00
2026-04-21 12:41:27 +08:00
2026-04-09 18:14:28 +08:00
2026-04-09 18:14:28 +08:00
2026-04-09 18:14:28 +08:00
2026-04-09 18:14:28 +08:00
2026-04-09 18:14:28 +08:00
2026-05-19 15:53:28 +08:00
2026-05-20 16:08:41 +08:00
2026-03-08 06:59:53 +08:00
2026-05-19 15:27:47 +08:00
2026-05-19 15:27:47 +08:00
2026-05-26 10:49:20 +08:00
2026-05-27 23:55:34 +08:00
2026-03-11 16:46:58 +08:00
2026-03-11 16:46:58 +08:00
2026-03-18 09:23:19 +08:00
2026-05-28 17:27:01 +08:00
2026-05-28 17:27:01 +08:00
2026-03-27 14:33:05 +08:00
2026-02-28 15:01:20 +08:00
2026-04-09 19:25:08 +08:00
2026-04-09 18:14:28 +08:00
2026-04-09 18:14:28 +08:00
2026-03-04 13:45:49 +08:00
2026-05-26 10:49:20 +08:00
2026-05-29 17:19:15 +08:00
2026-05-26 10:49:20 +08:00
2026-05-26 10:49:20 +08:00