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:
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"feedsystem_video_go/internal/middleware/rabbitmq"
|
||||
rediscache "feedsystem_video_go/internal/middleware/redis"
|
||||
"feedsystem_video_go/internal/video"
|
||||
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
@@ -19,11 +20,12 @@ type CommentWorker struct {
|
||||
parker rabbitmq.Parker
|
||||
comments video.CommentStore
|
||||
videos *video.VideoRepository
|
||||
cache *rediscache.Client
|
||||
queue string
|
||||
}
|
||||
|
||||
func NewCommentWorker(ch *amqp.Channel, parker rabbitmq.Parker, comments video.CommentStore, videos *video.VideoRepository, queue string) *CommentWorker {
|
||||
return &CommentWorker{ch: ch, parker: parker, comments: comments, videos: videos, queue: queue}
|
||||
func NewCommentWorker(ch *amqp.Channel, parker rabbitmq.Parker, comments video.CommentStore, videos *video.VideoRepository, cache *rediscache.Client, queue string) *CommentWorker {
|
||||
return &CommentWorker{ch: ch, parker: parker, comments: comments, videos: videos, cache: cache, queue: queue}
|
||||
}
|
||||
|
||||
func (w *CommentWorker) Run(ctx context.Context) error {
|
||||
@@ -71,6 +73,10 @@ func (w *CommentWorker) applyPublish(ctx context.Context, evt *rabbitmq.CommentE
|
||||
VideoID: evt.VideoID,
|
||||
AuthorID: evt.AuthorID,
|
||||
Content: strings.TrimSpace(evt.Content),
|
||||
// 行时间来自事件:桶必须落在评论发生的那一分钟。
|
||||
// 事件没带时间时保持零值,由 GORM 的 autoCreateTime 补当前时刻,
|
||||
// 并回填到这个字段,因此下面的落桶仍与行一致。
|
||||
CreatedAt: evt.OccurredAt,
|
||||
}
|
||||
// event_id 即意图键:API 已直写时这里会命中唯一索引。
|
||||
if evt.EventID != "" {
|
||||
@@ -85,6 +91,11 @@ func (w *CommentWorker) applyPublish(ctx context.Context, evt *rabbitmq.CommentE
|
||||
if !created {
|
||||
log.Printf("comment worker: dedupe=hit event_id=%s video_id=%d author_id=%d",
|
||||
evt.EventID, evt.VideoID, evt.AuthorID)
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := video.ApplyHotDelta(ctx, w.cache, c.VideoID, 1, c.CreatedAt); err != nil {
|
||||
log.Printf("comment worker: hot window update failed video=%d: %v", c.VideoID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -93,13 +104,18 @@ func (w *CommentWorker) applyDelete(ctx context.Context, evt *rabbitmq.CommentEv
|
||||
if evt == nil || evt.CommentID == 0 {
|
||||
return rabbitmq.Permanent(errors.New("comment worker: invalid delete event: comment_id is required"))
|
||||
}
|
||||
deleted, err := w.comments.DeleteByID(ctx, evt.CommentID)
|
||||
deleted, ok, err := w.comments.DeleteWithPopularity(ctx, evt.CommentID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !deleted {
|
||||
// 重复投递的删除是空操作,不是错误。
|
||||
if !ok {
|
||||
// 重复投递的删除是空操作,不是错误;热度也不能再减一次。
|
||||
log.Printf("comment worker: comment already deleted id=%d event_id=%s", evt.CommentID, evt.EventID)
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := video.ApplyHotDelta(ctx, w.cache, deleted.VideoID, -1, deleted.CreatedAt); err != nil {
|
||||
log.Printf("comment worker: hot window update failed comment_id=%d: %v", evt.CommentID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user