同一条用户意图(同 author_id + request_id)在 comments 上只落一行: 唯一索引 uk_comments_author_key 是唯一的判重点,API 与 Worker 共用 同一个键,因此「MQ 假失败后兜底直写」与「消息稍后被消费」不再双写。 - comment_service: 引入 CommentStore/CommentPublisher/PopularityPublisher 三个最小接口;Publish 先解析意图键,再把同一个键喂给评论事件、热度事件 与兜底直写。只有评论事件没入队时才直写(闸 1),热度兜底独立门控。 - comment_repo: CreateIdempotentWithPopularity 在单事务内插入评论并按 created 门控 videos.popularity;CreateMentions 显式补 CreatedAt 并按 dedupe_key 去重;FindAccountIDs 一次 username IN ? 批量解析。 - commentworker: applyPublish 用 event_id 作幂等键并交给同一个事务, !created 记 dedupe=hit;applyDelete 改 DeleteByID,重复删除是空操作。 - comment_handler: 接收 request_id、响应回显,不返回评论 ID(MQ 路径上 评论此刻可能尚未落库)。 - 提及通知带上绑定到意图键的 dedupe_key,重复投递只写一条。 验证:go test ./... 全绿;-tags integration 下 3 个用例在真实 MySQL 上 通过(重复事件 1 行评论 + 热度只加 1、空 event_id 落 NULL 键、重复删除)
152 lines
3.5 KiB
Go
152 lines
3.5 KiB
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"feedsystem_video_go/internal/middleware/rabbitmq"
|
|
"feedsystem_video_go/internal/video"
|
|
"log"
|
|
"strings"
|
|
"time"
|
|
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|
)
|
|
|
|
type CommentWorker struct {
|
|
ch *amqp.Channel
|
|
comments video.CommentStore
|
|
videos *video.VideoRepository
|
|
queue string
|
|
}
|
|
|
|
func NewCommentWorker(ch *amqp.Channel, comments video.CommentStore, videos *video.VideoRepository, queue string) *CommentWorker {
|
|
return &CommentWorker{ch: ch, comments: comments, videos: videos, 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")
|
|
}
|
|
|
|
deliveries, err := w.ch.Consume(
|
|
w.queue,
|
|
"",
|
|
false,
|
|
false,
|
|
false,
|
|
false,
|
|
nil,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case d, ok := <-deliveries:
|
|
if !ok {
|
|
return errors.New("deliveries channel closed")
|
|
}
|
|
w.handleDelivery(ctx, d)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (w *CommentWorker) handleDelivery(ctx context.Context, d amqp.Delivery) {
|
|
const maxRetries = 3
|
|
for i := 0; i <= maxRetries; i++ {
|
|
select {
|
|
case <-ctx.Done():
|
|
_ = d.Nack(false, true)
|
|
return
|
|
default:
|
|
}
|
|
if err := w.process(ctx, d.Body); err != nil {
|
|
if i >= maxRetries {
|
|
log.Printf("comment worker: 重试 %d 次后仍失败, 丢弃: %v", maxRetries, err)
|
|
_ = d.Ack(false)
|
|
return
|
|
}
|
|
wait := time.Duration(1<<uint(i)) * time.Second
|
|
log.Printf("comment worker: 处理失败, %v 后重试 (%d/%d): %v", wait, i+1, maxRetries, err)
|
|
time.Sleep(wait)
|
|
continue
|
|
}
|
|
_ = d.Ack(false)
|
|
return
|
|
}
|
|
}
|
|
|
|
func (w *CommentWorker) process(ctx context.Context, body []byte) error {
|
|
var evt rabbitmq.CommentEvent
|
|
if err := json.Unmarshal(body, &evt); err != nil {
|
|
return nil
|
|
}
|
|
switch evt.Action {
|
|
case "publish":
|
|
return w.applyPublish(ctx, &evt)
|
|
case "delete":
|
|
return w.applyDelete(ctx, &evt)
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
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 nil
|
|
}
|
|
|
|
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),
|
|
}
|
|
// 事件里的 event_id 就是"这次用户意图"的键。API 侧若已经兜底直写过,
|
|
// 这次插入会命中唯一索引:(created=false) 说明重复投递,热度不能再加一次。
|
|
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
|
|
}
|
|
|
|
func (w *CommentWorker) applyDelete(ctx context.Context, evt *rabbitmq.CommentEvent) error {
|
|
if evt == nil || evt.CommentID == 0 {
|
|
return nil
|
|
}
|
|
deleted, err := w.comments.DeleteByID(ctx, evt.CommentID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !deleted {
|
|
// 重复投递的删除是空操作,不是错误。
|
|
log.Printf("comment worker: comment already deleted id=%d event_id=%s", evt.CommentID, evt.EventID)
|
|
}
|
|
return nil
|
|
}
|