同一条用户意图(同 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 键、重复删除)
72 lines
2.1 KiB
Go
72 lines
2.1 KiB
Go
package rabbitmq
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"time"
|
||
)
|
||
|
||
// CommentMQ 通过连接管理器发布评论事件(不再持有长生命周期 channel)。
|
||
type CommentMQ struct {
|
||
mgr *RabbitMQ
|
||
}
|
||
|
||
// 拓扑常量导出:见 likeMQ.go 中的说明。
|
||
const (
|
||
CommentExchange = "comment.events"
|
||
CommentQueue = "comment.events"
|
||
CommentBindingKey = "comment.*"
|
||
|
||
CommentPublishRK = "comment.publish"
|
||
CommentDeleteRK = "comment.delete"
|
||
)
|
||
|
||
type CommentEvent struct {
|
||
EventID string `json:"event_id"`
|
||
Action string `json:"action"`
|
||
CommentID uint `json:"comment_id,omitempty"`
|
||
Username string `json:"username,omitempty"`
|
||
VideoID uint `json:"video_id,omitempty"`
|
||
AuthorID uint `json:"author_id,omitempty"`
|
||
Content string `json:"content,omitempty"`
|
||
OccurredAt time.Time `json:"occurred_at"`
|
||
}
|
||
|
||
func NewCommentMQ(base *RabbitMQ) (*CommentMQ, error) {
|
||
if base == nil {
|
||
return nil, errors.New("rabbitmq base is nil")
|
||
}
|
||
base.RegisterTopology(Topology{Exchange: CommentExchange, Queue: CommentQueue, BindingKey: CommentBindingKey})
|
||
return &CommentMQ{mgr: base}, nil
|
||
}
|
||
|
||
// Publish 直接接收完整事件:event_id 由调用方(服务层)生成,因为它是
|
||
// "这次用户意图"的标识,必须与兜底直写用的是同一个值(P0-4 §6.8)。
|
||
// 只有 EventID 为空时才在这里补一个随机 id(老调用方/测试路径)。
|
||
func (c *CommentMQ) Publish(ctx context.Context, evt CommentEvent) error {
|
||
return c.publish(ctx, "publish", CommentPublishRK, evt)
|
||
}
|
||
|
||
func (c *CommentMQ) Delete(ctx context.Context, commentID uint, intentKey string) error {
|
||
return c.publish(ctx, "delete", CommentDeleteRK, CommentEvent{
|
||
CommentID: commentID,
|
||
EventID: intentKey,
|
||
})
|
||
}
|
||
|
||
func (c *CommentMQ) publish(ctx context.Context, action, routingKey string, evt CommentEvent) error {
|
||
if c == nil || c.mgr == nil {
|
||
return errors.New("comment mq is not initialized")
|
||
}
|
||
if evt.EventID == "" {
|
||
id, err := newEventID(16)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
evt.EventID = id
|
||
}
|
||
evt.Action = action
|
||
evt.OccurredAt = time.Now().UTC()
|
||
return c.mgr.PublishJSON(ctx, CommentExchange, routingKey, evt)
|
||
}
|