From 7fc343689278de1c3f664a78776483cac443a390 Mon Sep 17 00:00:00 2001 From: huanghaosheng <386998068@qq.com> Date: Fri, 30 Oct 2026 13:13:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(failedevent):=20=E6=96=B0=E5=A2=9E=20faile?= =?UTF-8?q?d=5Fevents=20=E8=A1=A8=E4=B8=8E=E8=90=BD=E5=BA=93=E4=BB=93?= =?UTF-8?q?=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 停泊消息需要一个"查得到"的落地形态。此前死信链路只有声明没有消费者, 被拒绝的消息一旦 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 进程这一个。 --- backend/internal/db/db.go | 4 +++ backend/internal/failedevent/entity.go | 42 ++++++++++++++++++++++++++ backend/internal/failedevent/repo.go | 42 ++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 backend/internal/failedevent/entity.go create mode 100644 backend/internal/failedevent/repo.go diff --git a/backend/internal/db/db.go b/backend/internal/db/db.go index 34272db..1493438 100644 --- a/backend/internal/db/db.go +++ b/backend/internal/db/db.go @@ -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{}, ) } diff --git a/backend/internal/failedevent/entity.go b/backend/internal/failedevent/entity.go new file mode 100644 index 0000000..2dfb442 --- /dev/null +++ b/backend/internal/failedevent/entity.go @@ -0,0 +1,42 @@ +// Package failedevent 存放"处理不了的消息"的持久化记录。 +// +// 独立成包而不是放进 worker:db.go 已经 import worker(为了 Notification), +// 而 worker 又要 import failedevent(为了写库)。放进 worker 会形成 +// worker → failedevent → worker 的循环;独立包让依赖方向恒为 worker → failedevent。 +package failedevent + +import "time" + +// 停泊记录的处置状态。 +const ( + // StatusPending 表示这条失败事件还没有被人工/工具处理。 + StatusPending = "pending" + // StatusReplayed 表示已经重放过。 + StatusReplayed = "replayed" + // StatusDiscarded 表示确认不再重放。 + StatusDiscarded = "discarded" +) + +// FailedEvent 是一条停泊消息的落库记录。 +type FailedEvent struct { + ID uint `gorm:"primaryKey" json:"id"` + // SourceQueue + EventID 构成复合唯一键:同一意图键的评论事件与热度事件 + // 来自两个不同的源队列,只用 event_id 去重会把它们误判成同一条。 + SourceQueue string `gorm:"column:source_queue;type:varchar(100);not null;uniqueIndex:uk_failed_source_event,priority:1" json:"source_queue"` + EventID *string `gorm:"column:event_id;type:varchar(64);uniqueIndex:uk_failed_source_event,priority:2" json:"event_id"` + RoutingKey string `gorm:"column:routing_key;type:varchar(200)" json:"routing_key"` + Attempts int `gorm:"column:attempts;not null" json:"attempts"` + Body []byte `gorm:"column:body;type:mediumblob" json:"-"` + Headers string `gorm:"column:headers;type:json" json:"-"` + LastError string `gorm:"column:last_error;type:varchar(512)" json:"last_error"` + // Status 取值见本包的 StatusPending / StatusReplayed / StatusDiscarded。 + Status string `gorm:"column:status;type:varchar(20);default:'pending';index" json:"status"` + ReplayCount int `gorm:"column:replay_count;default:0" json:"replay_count"` + FirstFailedAt time.Time `gorm:"column:first_failed_at" json:"first_failed_at"` + CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` + UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` +} + +// TableName 固定表名:默认命名是 failed_events,但显式写出来可以避免 +// 有人把类型改名之后表名跟着漂移。 +func (FailedEvent) TableName() string { return "failed_events" } diff --git a/backend/internal/failedevent/repo.go b/backend/internal/failedevent/repo.go new file mode 100644 index 0000000..5937720 --- /dev/null +++ b/backend/internal/failedevent/repo.go @@ -0,0 +1,42 @@ +package failedevent + +import ( + "context" + "errors" + + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +// Repository 是 failed_events 的存储层。 +type Repository struct { + db *gorm.DB +} + +// NewRepository 构造仓储。 +func NewRepository(db *gorm.DB) *Repository { + return &Repository{db: db} +} + +// Save 写入一条停泊记录,返回它是否是本次新插入的。 +// +// 去重键是 (source_queue, event_id):重复投递同一条停泊消息(停泊成功但 Ack 前崩溃、 +// 或运维重放)只会留下一条记录。EventID 为 NULL 的消息不参与复合唯一键 +// ——MySQL 唯一索引里 NULL 不参与比较,多个 NULL 可以共存,这是有意的: +// 没有意图键的坏消息本来就没有身份,去重无从谈起。 +func (r *Repository) Save(ctx context.Context, e *FailedEvent) (bool, error) { + if r == nil || r.db == nil { + return false, errors.New("failedevent: repository is not initialized") + } + if e == nil { + return false, errors.New("failedevent: entity is nil") + } + if e.EventID == nil { + return true, r.db.WithContext(ctx).Create(e).Error + } + res := r.db.WithContext(ctx).Clauses(clause.OnConflict{DoNothing: true}).Create(e) + if res.Error != nil { + return false, res.Error + } + return res.RowsAffected > 0, nil +}