注释搬家到 docs/design/message-failure-handling.md。 删除复述代码的注释、历史缺陷说明与批次编号。保留的类型断言、索引语义、声明顺序等约束已在文档中展开。 重写 dlx.go、topology.go、consumer.go 三个文件,其余文件只删注释。 唯一非注释改动是 topology.go 里两处 ExchangeDeclare 由多行压成单行,语义不变。 验证:go test -count=1 ./... 全绿。
40 lines
985 B
Go
40 lines
985 B
Go
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);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")
|
||
}
|
||
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
|
||
}
|