style(backend): 清理消息失败处理路径的注释
注释搬家到 docs/design/message-failure-handling.md。 删除复述代码的注释、历史缺陷说明与批次编号。保留的类型断言、索引语义、声明顺序等约束已在文档中展开。 重写 dlx.go、topology.go、consumer.go 三个文件,其余文件只删注释。 唯一非注释改动是 topology.go 里两处 ExchangeDeclare 由多行压成单行,语义不变。 验证:go test -count=1 ./... 全绿。
This commit is contained in:
@@ -1,15 +1,11 @@
|
||||
// Package failedevent 存放"处理不了的消息"的持久化记录。
|
||||
//
|
||||
// 独立成包而不是放进 worker:db.go 已经 import worker(为了 Notification),
|
||||
// 而 worker 又要 import failedevent(为了写库)。放进 worker 会形成
|
||||
// worker → failedevent → worker 的循环;独立包让依赖方向恒为 worker → failedevent。
|
||||
package failedevent
|
||||
|
||||
import "time"
|
||||
|
||||
// 停泊记录的处置状态。
|
||||
const (
|
||||
// StatusPending 表示这条失败事件还没有被人工/工具处理。
|
||||
// StatusPending 表示这条失败事件还没有被处理。
|
||||
StatusPending = "pending"
|
||||
// StatusReplayed 表示已经重放过。
|
||||
StatusReplayed = "replayed"
|
||||
@@ -20,8 +16,7 @@ const (
|
||||
// FailedEvent 是一条停泊消息的落库记录。
|
||||
type FailedEvent struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
// SourceQueue + EventID 构成复合唯一键:同一意图键的评论事件与热度事件
|
||||
// 来自两个不同的源队列,只用 event_id 去重会把它们误判成同一条。
|
||||
// SourceQueue + EventID 是复合唯一键:同一意图键的评论事件与热度事件来自不同源队列。
|
||||
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"`
|
||||
@@ -29,7 +24,7 @@ type FailedEvent struct {
|
||||
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"`
|
||||
@@ -37,6 +32,5 @@ type FailedEvent struct {
|
||||
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
|
||||
}
|
||||
|
||||
// TableName 固定表名:默认命名是 failed_events,但显式写出来可以避免
|
||||
// 有人把类型改名之后表名跟着漂移。
|
||||
// TableName 固定表名,避免类型改名后表名跟着漂移。
|
||||
func (FailedEvent) TableName() string { return "failed_events" }
|
||||
|
||||
@@ -18,12 +18,9 @@ func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
// Save 写入一条停泊记录,返回它是否是本次新插入的。
|
||||
// Save 写入一条停泊记录,返回是否是本次新插入的。
|
||||
//
|
||||
// 去重键是 (source_queue, event_id):重复投递同一条停泊消息(停泊成功但 Ack 前崩溃、
|
||||
// 或运维重放)只会留下一条记录。EventID 为 NULL 的消息不参与复合唯一键
|
||||
// ——MySQL 唯一索引里 NULL 不参与比较,多个 NULL 可以共存,这是有意的:
|
||||
// 没有意图键的坏消息本来就没有身份,去重无从谈起。
|
||||
// 去重键是 (source_queue, event_id);EventID 为 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")
|
||||
|
||||
Reference in New Issue
Block a user