style(backend): 清理幂等写入路径的注释

注释只保留一行级约束。
长段理由移入设计文档。
验证:go test -count=1 ./... 全绿。
This commit is contained in:
2026-10-31 00:03:53 +08:00
parent c0ab69118a
commit c210e75996
13 changed files with 50 additions and 239 deletions

View File

@@ -13,26 +13,18 @@ import (
rediscache "feedsystem_video_go/internal/middleware/redis"
)
// CommentPublisher 只暴露评论服务用得到的两个动作。
//
// 用接口而不是具体类型,是为了让"发布失败"成为可注入的场景:P0-4 的全部
// 分支都取决于"这条消息到底发出去了没有",而这恰恰是真实 broker 最难稳定
// 复现的状态。
//
// 注意:nil 指针装进接口后 != nil 依然为 true,所以真实实现必须在 nil
// 接收者上返回 error(commentMQ.go 现状已如此)。
// CommentPublisher 只暴露评论服务用到的两个动作,便于在测试里注入发布失败。
// 真实实现必须在 nil 接收者上返回 error:nil 装进接口后 != nil 仍为 true。
type CommentPublisher interface {
Publish(ctx context.Context, evt rabbitmq.CommentEvent) error
Delete(ctx context.Context, commentID uint, intentKey string) error
}
// PopularityPublisher 是热度事件的最小接口。
type PopularityPublisher interface {
Update(ctx context.Context, videoID uint, change int64, intentKey string) error
}
// videoExister 只暴露 CommentService 需要的那一个方法。
// *VideoRepository 天然满足它,因此 router.go 的构造代码不需要改。
// videoExister 由 *VideoRepository 满足,因此 router.go 无需改动。
type videoExister interface {
IsExist(ctx context.Context, id uint) (bool, error)
}
@@ -55,17 +47,7 @@ func NewCommentService(store CommentStore, videos videoExister, cache *rediscach
}
}
// Publish 把一次"用户想发一条评论"的意图变成一次写入(P0-4 §6.5.2)。
//
// 关键在于:MQ 路径与兜底路径共用同一个意图键,而"判重"发生在唯一索引上,
// 不是在内存里。因此
//
// - 消息发布成功 → 由 worker 写库,API 不写(否则正常路径会稳定双写);
// - 消息发布失败 → API 直写,键仍是同一个,worker 稍后到达时命中唯一索引;
// - 客户端重试 → 同一个键,第二次什么都不做,但返回成功。
//
// 发布失败不代表"没送达"(短超时、channel 抖动都可能发生在 broker 已收下
// 之后),这正是旧代码的 D1:把 error 当成"未送达",于是必然双写。
// Publish 用同一个意图键编排两条路径:MQ 成功则不直写,失败则直写。
func (s *CommentService) Publish(ctx context.Context, comment *Comment) error {
if comment == nil {
return errors.New("comment is nil")
@@ -110,8 +92,7 @@ func (s *CommentService) Publish(ctx context.Context, comment *Comment) error {
log.Printf("%s enqueue popularity failed, fallback to hot cache: %v", span, err)
}
// —— 闸 1:只有评论事件没入队时才直写。
// 入队成功时不写:让 worker 去写,避免"入队成功但 API 又写一遍"的必然双写。
// 入队成功时不直写,避免正常路径上稳定双写。
if !commentEnqueued {
created, err := s.store.CreateIdempotentWithPopularity(ctx, comment, 1)
if err != nil {
@@ -120,8 +101,7 @@ func (s *CommentService) Publish(ctx context.Context, comment *Comment) error {
log.Printf("%s fallback write dedupe=%s", span, dedupeLabel(created))
}
// —— 热度兜底:与评论事件彼此独立(两条事件是两次独立发布)。
// 热榜是"近似正确"的派生数据:失败只降级,不影响"评论已发布"的语义。
// 热度与评论是两次独立发布,失败只降级。
if !popularityEnqueued {
if err := UpdatePopularityCache(ctx, s.cache, comment.VideoID, 1, key); err != nil {
log.Printf("%s hot cache update failed (degraded): %v", span, err)
@@ -143,8 +123,6 @@ func (s *CommentService) Delete(ctx context.Context, commentID uint, accountID u
if comment.AuthorID != accountID {
return apierror.ErrUnauthorized
}
// 删除事件的 event_id 也用意图键:同一次删除被重复投递时,worker 侧
// 按 id 删除并检查受影响行数,第二次是空操作。
key, keyErr := NewIntentKey()
if keyErr != nil {
key = ""
@@ -168,10 +146,8 @@ func (s *CommentService) GetAll(ctx context.Context, videoID uint) ([]Comment, e
var mentionRegex = regexp.MustCompile(`@(\w+)`)
// notifyMentions 写入 @ 提及通知。
//
// 三次往返收敛成两次(一次批量查用户名、一次批量插),并且每条通知都带
// 绑定到意图键的去重键:同一条评论被重复投递时,通知也只有一条。
// notifyMentions 批量解析用户名并写入带去重键的提及通知。
// 去重键必须含收件人:同一次评论可能通知多个人。
func (s *CommentService) notifyMentions(ctx context.Context, comment *Comment, intentKey string) {
matches := mentionRegex.FindAllStringSubmatch(comment.Content, -1)
if len(matches) == 0 {
@@ -208,7 +184,7 @@ func (s *CommentService) notifyMentions(ctx context.Context, comment *Comment, i
continue
}
if id == comment.AuthorID {
continue // 不给自己发通知
continue
}
mentions = append(mentions, Mention{
RecipientID: id,
@@ -222,15 +198,12 @@ func (s *CommentService) notifyMentions(ctx context.Context, comment *Comment, i
return
}
// 通知是派生副作用:失败不改变"评论已发布",但必须留痕。
if err := s.store.CreateMentions(ctx, mentions); err != nil {
// 通知是派生副作用:失败不改变"评论已发布"的结论(保持现状语义),
// 但必须留痕,否则"用户说没收到通知"无从排查。
log.Printf("intent_key=%s stage=mention.create err=%v", intentKey, err)
}
}
// logIntent 返回一段可 grep 的公共前缀:一次意图的所有日志都带同一个 key,
// 因此"用户说没看到评论"可以用 grep intent_key=<K> 串起 API 与 Worker 两侧。
func logIntent(stage, key string, authorID, videoID uint) string {
return fmt.Sprintf("intent_key=%s stage=%s author_id=%d video_id=%d", key, stage, authorID, videoID)
}