refactor(video): 计数改由业务事务维护并删除热度第二条写路径
点赞与评论的计数只由业务事务写。ApplyLike、ApplyUnlike、CreateIdempotentWithPopularity 与 DeleteWithPopularity 各自在单事务内完成插行与计数。 热榜增量改为提交后的 best-effort 写。ApplyHotDelta 按行的 created_at 落分钟桶,写失败只记日志,DB 计数仍然正确。 删除 PopularityMQ 与 PopularityWorker。同一次互动因此只有一个写者,不再有两个门闩各自放行。 删除 hot:applied 占位键。判重改由 likes 的唯一索引与 uk_comments_author_key 负责。 DeleteWithPopularity 先读行再删行。评论删除因此回退热度,重复投递不再多减一次。 likes 与 comments 各加 (created_at, video_id) 覆盖索引,服务热榜窗口查询。 验证:go build -buildvcs=false ./... 通过。go test -buildvcs=false -count=1 ./... 全绿。
This commit is contained in:
@@ -25,12 +25,6 @@ func (failingCommentPublisher) Delete(context.Context, uint, string) error {
|
||||
return errors.New("mq publish failed")
|
||||
}
|
||||
|
||||
type failingPopularityPublisher struct{}
|
||||
|
||||
func (failingPopularityPublisher) Update(context.Context, uint, int64, string) error {
|
||||
return errors.New("mq publish failed")
|
||||
}
|
||||
|
||||
// P0-4 的原始缺陷是"兜底直写一次 + 消费者再写一次",结果是两条评论、双倍热度。
|
||||
// 这个用例把两条路径接起来跑:先让服务层兜底写入,再让同一个意图键的事件到达
|
||||
// worker。断言收敛到一行评论、热度恰为 1。
|
||||
@@ -45,10 +39,8 @@ func TestP0_4_FallbackAndWorkerConvergeToOneComment(t *testing.T) {
|
||||
|
||||
commentRepo := video.NewCommentRepository(db)
|
||||
videoRepo := video.NewVideoRepository(db)
|
||||
svc := video.NewCommentService(
|
||||
commentRepo, videoRepo, nil,
|
||||
failingCommentPublisher{}, failingPopularityPublisher{},
|
||||
)
|
||||
// cache 传 nil:热榜是 best-effort 派生数据,本用例只断言 DB 侧的收敛。
|
||||
svc := video.NewCommentService(commentRepo, videoRepo, nil, failingCommentPublisher{})
|
||||
|
||||
intentKey := "converge-intent-0001"
|
||||
comment := &video.Comment{
|
||||
@@ -66,7 +58,7 @@ func TestP0_4_FallbackAndWorkerConvergeToOneComment(t *testing.T) {
|
||||
}
|
||||
|
||||
// 消息其实已到 broker,稍后由消费者处理同一个意图键。
|
||||
w := NewCommentWorker(nil, nil, commentRepo, videoRepo, rabbitmq.CommentQueue)
|
||||
w := NewCommentWorker(nil, nil, commentRepo, videoRepo, nil, rabbitmq.CommentQueue)
|
||||
evt := &rabbitmq.CommentEvent{
|
||||
EventID: intentKey,
|
||||
Action: "publish",
|
||||
|
||||
Reference in New Issue
Block a user