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:
@@ -79,21 +79,21 @@ func main() {
|
||||
})
|
||||
defer db.CloseDB(sqlDB)
|
||||
|
||||
// 连接 Redis(用于流行度更新)
|
||||
// 连接 Redis(用于热榜窗口增量)
|
||||
cache, err := rediscache.NewFromEnv(&cfg.Redis)
|
||||
if err != nil {
|
||||
log.Printf("Redis config error (popularity worker disabled): %v", err)
|
||||
log.Printf("Redis config error (hot window disabled): %v", err)
|
||||
cache = nil
|
||||
} else {
|
||||
pingCtx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := cache.Ping(pingCtx); err != nil {
|
||||
log.Printf("Redis not available (popularity worker disabled): %v", err)
|
||||
log.Printf("Redis not available (hot window disabled): %v", err)
|
||||
_ = cache.Close()
|
||||
cache = nil
|
||||
} else {
|
||||
defer cache.Close()
|
||||
log.Printf("Redis connected (popularity worker enabled)")
|
||||
log.Printf("Redis connected (hot window enabled)")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,11 +110,6 @@ func main() {
|
||||
mqrabbit.Topology{Exchange: mqrabbit.LikeExchange, Queue: mqrabbit.LikeQueue, BindingKey: mqrabbit.LikeBindingKey},
|
||||
mqrabbit.Topology{Exchange: mqrabbit.CommentExchange, Queue: mqrabbit.CommentQueue, BindingKey: mqrabbit.CommentBindingKey},
|
||||
)
|
||||
if cache != nil {
|
||||
rmqMgr.RegisterTopology(
|
||||
mqrabbit.Topology{Exchange: mqrabbit.PopularityExchange, Queue: mqrabbit.PopularityQueue, BindingKey: mqrabbit.PopularityBindingKey},
|
||||
)
|
||||
}
|
||||
|
||||
// 准备 repo
|
||||
socialRepo := social.NewSocialRepository(sqlDB)
|
||||
@@ -147,16 +142,11 @@ func main() {
|
||||
return worker.NewSocialWorker(ch, rmqMgr, socialRepo, mqrabbit.SocialQueue).Run(ctx)
|
||||
})
|
||||
go mqrabbit.RunConsumer(ctx, rmqMgr, "LikeWorker", 50, func(ch *amqp.Channel) error {
|
||||
return worker.NewLikeWorker(ch, rmqMgr, likeRepo, videoRepo, mqrabbit.LikeQueue).Run(ctx)
|
||||
return worker.NewLikeWorker(ch, rmqMgr, likeRepo, videoRepo, cache, mqrabbit.LikeQueue).Run(ctx)
|
||||
})
|
||||
go mqrabbit.RunConsumer(ctx, rmqMgr, "CommentWorker", 50, func(ch *amqp.Channel) error {
|
||||
return worker.NewCommentWorker(ch, rmqMgr, commentRepo, videoRepo, mqrabbit.CommentQueue).Run(ctx)
|
||||
return worker.NewCommentWorker(ch, rmqMgr, commentRepo, videoRepo, cache, mqrabbit.CommentQueue).Run(ctx)
|
||||
})
|
||||
if cache != nil {
|
||||
go mqrabbit.RunConsumer(ctx, rmqMgr, "PopularityWorker", 50, func(ch *amqp.Channel) error {
|
||||
return worker.NewPopularityWorker(ch, rmqMgr, cache, mqrabbit.PopularityQueue).Run(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
// 等待退出信号
|
||||
<-ctx.Done()
|
||||
|
||||
Reference in New Issue
Block a user