feat(failedevent): 新增 failed_events 表与落库仓储

停泊消息需要一个"查得到"的落地形态。此前死信链路只有声明没有消费者,
被拒绝的消息一旦 Ack 就再也无法举证。

表设计的两个要点:

- 复合唯一键 (source_queue, event_id) 而不是只钉 event_id。同一个意图可以
  出现在多个队列里(比如 like.events 与 notification.like 由同一次点赞产生),
  只按 event_id 去重会把第二个队列的失败记录误当成重复丢掉。
- event_id 必须可空(*string):解析不出意图键的坏消息没有 id,NULL 允许
  多行共存,空字符串则会互相撞键。

落库是"只增不改"的:Save 在 event_id 非空时用 OnConflict{DoNothing},
并以 RowsAffected 判断是否真的插入,重复投递因此是幂等的。

包独立成 internal/failedevent 而不是放进 internal/worker:
db.go 已经 import worker(AutoMigrate 需要 Notification),若表定义也放
worker,worker → failedevent → worker 就成环。

db.go 的 AutoMigrate 追加该表,建表入口仍然只有 API 进程这一个。
This commit is contained in:
2026-10-30 13:13:29 +08:00
parent e0a21fab78
commit 7fc3436892
3 changed files with 88 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ package db
import (
"feedsystem_video_go/internal/account"
"feedsystem_video_go/internal/config"
"feedsystem_video_go/internal/failedevent"
"feedsystem_video_go/internal/message"
"feedsystem_video_go/internal/social"
"feedsystem_video_go/internal/video"
@@ -30,6 +31,9 @@ func AutoMigrate(db *gorm.DB) error {
&account.Account{}, &video.Video{}, &video.Like{}, &video.Comment{},
&social.Social{}, &video.OutboxMsg{}, &video.Tag{}, &video.VideoTag{},
&message.Message{}, &worker.Notification{},
// 停泊消息的落库表(P0-3)。它的写入方是 Worker 的 FailedEventWorker,
// 但建表仍由 API 进程的 AutoMigrate 统一负责——建表入口只有一个。
&failedevent.FailedEvent{},
)
}