Repository navigation
fix(core): 取件码铸造 CAS + 通知去重原子认领(core#234 登记缺陷①②) - #241
Merged
Merged
Conversation
## 缺陷① SetPickupCode 无 CAS ⇒ 同享并发铸码出幽灵 无条件覆盖:并发铸造同一分享时两方都过预检、都 UPDATE 成功、后写覆盖 先写 ⇒ 两方都拿 set=true,先写方交付的码在 DB 真相源已不存在(KV/响应 vs DB 不一致)。 - dao:UPDATE 加 `AND pickup_code IS NULL` 条件,库里原子裁决胜负; - 调用方(GenerateCode):CAS 未中 ⇒ 幂等收敛到既有码直接返回(不换码 重试——行非 NULL 重铸必再撞;不写 KV——KV 归铸胜方,NX 同键会误判撞码 回滚别人的码)。 - 既有并发测试旧契约「50 方各拿不同码」正是幽灵码工厂的行为,改钉新契约 (全员成功 + 收敛同一码 + DB 对账)。 ## 缺陷② 通知去重读-判-写 ⇒ 风暴下 owner 收 N 条 旧三段(读 LastNotifiedAt → time.Since → 无条件写回)在并发取件时全部放行。 - dao 新增 ClaimNotifyWindow:条件 UPDATE(NULL 或窗口外)原子认领, RowsAffected 即胜负;认领出错不消耗窗口(下次取件自愈重试); - service:判断与写入合一,false ⇒ 跳过发送。 ## 测试(L5 + 负对照) - dao 并发(临时文件 SQLite 真并发):同享 8 方铸码恰 1 胜、通知窗口 8 方 恰 1 认领、窗口顺序语义三段; - share:窗口内 3 次取件恰 1 条通知、窗口过期恢复; - anonymous:已铸成分享幂等返回既有码; - 负对照见各文件头注释(抽掉条件 ⇒ 对应用例必红)。 验证:GOWORK=off go build/vet + go test -race ./... 全绿(70 包)。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
core#234 PR 描述「顺带查实但未修」的两条并发缺陷(本 PR 收口):
根因同构:判断与写入不是原子的——修复都是把裁决下沉为条件 UPDATE。
改动
缺陷①(dao/filecode_pickup.go + app/anonymous/anonymous.go)
SetPickupCode:UPDATE 加AND pickup_code IS NULL,库里原子裁决——恰一方RowsAffected>0;GenerateCode:CAS 未中 ⇒ 幂等收敛到既有码直接返回:缺陷②(dao/filecode_pickup.go + app/share/service.go)
ClaimNotifyWindow:条件 UPDATE(last_notified_at IS NULL OR < 窗口外)原子认领,false=窗口被占跳过;认领出错不消耗窗口(下次取件自愈);RecordViewerAndNotify:读-判-写三段合一,判断与写入不再有竞态窗口。测试(L5 + 负对照)
负对照写在各测试文件头(抽掉 CAS 条件 / 认领条件 ⇒ 对应用例必红)。
验证
GOWORK=off go build ./... && go vet ./...✅GOWORK=off go test -race ./...✅ 70 包全绿(CI 同口径)gofmt -l .空 ✅Refs core#234(登记缺陷①②收口)