点赞与评论的计数只由业务事务写。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 ./... 全绿。
122 lines
3.8 KiB
Go
122 lines
3.8 KiB
Go
package worker
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"log"
|
||
"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"
|
||
)
|
||
|
||
type CommentWorker struct {
|
||
ch *amqp.Channel
|
||
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, 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 {
|
||
if w == nil || w.ch == nil || w.comments == nil || w.videos == nil {
|
||
return errors.New("comment worker is not initialized")
|
||
}
|
||
if w.queue == "" {
|
||
return errors.New("queue is required")
|
||
}
|
||
return rabbitmq.ConsumeLoop(ctx, w.ch, w.parker, w.queue, rabbitmq.MaxAttempts, w.process)
|
||
}
|
||
|
||
func (w *CommentWorker) process(ctx context.Context, d amqp.Delivery) error {
|
||
var evt rabbitmq.CommentEvent
|
||
if err := json.Unmarshal(d.Body, &evt); err != nil {
|
||
return rabbitmq.Permanent(fmt.Errorf("comment worker: decode event: %w", err))
|
||
}
|
||
|
||
switch evt.Action {
|
||
case "publish":
|
||
return w.applyPublish(ctx, &evt)
|
||
case "delete":
|
||
return w.applyDelete(ctx, &evt)
|
||
default:
|
||
return rabbitmq.Permanent(fmt.Errorf("comment worker: unknown action %q", evt.Action))
|
||
}
|
||
}
|
||
|
||
func (w *CommentWorker) applyPublish(ctx context.Context, evt *rabbitmq.CommentEvent) error {
|
||
if evt == nil || evt.VideoID == 0 || evt.AuthorID == 0 || strings.TrimSpace(evt.Content) == "" {
|
||
return rabbitmq.Permanent(fmt.Errorf("comment worker: invalid publish event video_id=%d author_id=%d", evt.VideoID, evt.AuthorID))
|
||
}
|
||
|
||
ok, err := w.videos.IsExist(ctx, evt.VideoID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if !ok {
|
||
// 视频已删除:处理完了,不是失败。
|
||
return nil
|
||
}
|
||
|
||
c := &video.Comment{
|
||
Username: strings.TrimSpace(evt.Username),
|
||
VideoID: evt.VideoID,
|
||
AuthorID: evt.AuthorID,
|
||
Content: strings.TrimSpace(evt.Content),
|
||
// 行时间来自事件:桶必须落在评论发生的那一分钟。
|
||
// 事件没带时间时保持零值,由 GORM 的 autoCreateTime 补当前时刻,
|
||
// 并回填到这个字段,因此下面的落桶仍与行一致。
|
||
CreatedAt: evt.OccurredAt,
|
||
}
|
||
// event_id 即意图键:API 已直写时这里会命中唯一索引。
|
||
if evt.EventID != "" {
|
||
key := evt.EventID
|
||
c.IdempotencyKey = &key
|
||
}
|
||
|
||
created, err := w.comments.CreateIdempotentWithPopularity(ctx, c, 1)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
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
|
||
}
|
||
|
||
func (w *CommentWorker) applyDelete(ctx context.Context, evt *rabbitmq.CommentEvent) error {
|
||
if evt == nil || evt.CommentID == 0 {
|
||
return rabbitmq.Permanent(errors.New("comment worker: invalid delete event: comment_id is required"))
|
||
}
|
||
deleted, ok, err := w.comments.DeleteWithPopularity(ctx, evt.CommentID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
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
|
||
}
|