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

@@ -16,12 +16,7 @@ import (
"gorm.io/gorm/clause"
)
// notificationActions 记录每个通知队列期望收到哪种 action。
//
// 分派依据必须是队列 + 消息体里的 action,不能是 routing key:重试链路
// 回源时会把 routing key 改写成队列名(`like.like` → `notification.like`),
// 按 routing key 判断会让第二次投递变成"未知事件"而被立即停泊,重试预算
// 白丢。queue 在整个生命周期里不变,所以它是可靠的。
// 分派必须用队列 + body.action:重试回源会把 routing key 改写成队列名。
var notificationActions = map[string]string{
rabbitmq.NotificationLikeQueue: "like",
rabbitmq.NotificationCommentQueue: "publish",
@@ -51,10 +46,7 @@ func (w *NotificationWorker) Run(ctx context.Context) error {
if w.queue == "" {
return errors.New("queue is required")
}
// 这里刻意不再 AutoMigrate:建表由 API 进程的 db.AutoMigrate 统一负责
// (internal/db/db.go 已包含 &notification.Notification{})。原来每个通知消费者
// 各跑一次 AutoMigrate,加唯一索引后三个 goroutine 会并发 CREATE INDEX
// 并撞上 1061;即使能靠重连自愈,也让"谁来建表"出现了两个答案。
// 建表由 API 进程的 db.AutoMigrate 统一负责;三个消费者并发建索引会撞 1061。
return rabbitmq.ConsumeLoop(ctx, w.ch, w.parker, w.queue, rabbitmq.MaxAttempts, w.process)
}
@@ -165,10 +157,7 @@ func (w *NotificationWorker) videoAuthorID(ctx context.Context, videoID uint) (u
}
func (w *NotificationWorker) persist(ctx context.Context, notif *notification.Notification, eventID string) error {
// 意图键取自事件体里的 event_id:P0-4 之后它就是"这一次用户动作"的
// 稳定标识(发布端用意图键填充)。取不到时留空——结果是不去重,
// 这是刻意的保守选择:多一条通知可以接受,把两条不同事件判成同一条
// 会让用户永久收不到其中一条。
// event_id 取不到时留空,此时该通知不参与去重。
notif.DedupeKey = nilIfEmpty(video.NotificationDedupeKey(notif.Type, eventID, notif.RecipientID))
res := w.db.WithContext(ctx).