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:
@@ -14,7 +14,6 @@ import (
|
||||
)
|
||||
|
||||
// failedEventSaver 是这个 worker 对存储的唯一依赖。
|
||||
// 抽成接口是为了用假实现验证"落库失败必须 requeue"这条契约(无需 MySQL)。
|
||||
type failedEventSaver interface {
|
||||
Save(ctx context.Context, e *failedevent.FailedEvent) (bool, error)
|
||||
}
|
||||
@@ -26,8 +25,7 @@ type FailedEventWorker struct {
|
||||
queue string
|
||||
}
|
||||
|
||||
// failedEventRetryBackoff 是"落库失败后重投"之前的退避。
|
||||
// 变量而非常量:单测要把它调到 0 以免为了等 2 秒而拖慢整个套件。
|
||||
// failedEventRetryBackoff 是落库失败后重投之前的退避,变量以便单测调小。
|
||||
var failedEventRetryBackoff = 2 * time.Second
|
||||
|
||||
// NewFailedEventWorker 构造停泊消费者。
|
||||
@@ -35,11 +33,7 @@ func NewFailedEventWorker(ch *amqp.Channel, repo failedEventSaver, queue string)
|
||||
return &FailedEventWorker{ch: ch, repo: repo, queue: queue}
|
||||
}
|
||||
|
||||
// Run 消费停泊队列直到 ctx 取消。
|
||||
//
|
||||
// 它自己【不】走 rabbitmq.ConsumeLoop:ConsumeLoop 的失败动作是"停泊",
|
||||
// 而这个 worker 的职责就是停泊落库——让它停泊自己会形成递归
|
||||
// (停泊队列消费失败 → 停泊 → 停泊队列 → …)。见方案 §6.9.3。
|
||||
// Run 消费停泊队列直到 ctx 取消;刻意不走 ConsumeLoop,否则停泊失败会递归停泊自己。
|
||||
func (w *FailedEventWorker) Run(ctx context.Context) error {
|
||||
if w == nil || w.ch == nil {
|
||||
return fmt.Errorf("failedevent worker: channel is not initialized")
|
||||
@@ -61,13 +55,11 @@ func (w *FailedEventWorker) Run(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
// persist 是这个 worker 的"最后一道防线":它自己不再停泊。
|
||||
// 落库失败 → requeue + 退避,不 Ack、不停泊(把消息停泊到一个不存在的消费者没有意义)。
|
||||
// persist 把停泊消息落库:失败则退避后 requeue,不 Ack、也不停泊自己。
|
||||
func (w *FailedEventWorker) persist(ctx context.Context, d amqp.Delivery) {
|
||||
info := rabbitmq.ParseParkedInfo(d)
|
||||
headers, err := json.Marshal(d.Headers)
|
||||
if err != nil {
|
||||
// 序列化失败不该阻止落库:Body 才是重放所需的主体,headers 只是附注。
|
||||
headers = []byte("{}")
|
||||
}
|
||||
record := &failedevent.FailedEvent{
|
||||
@@ -104,8 +96,7 @@ func nilIfEmpty(s string) *string {
|
||||
return &s
|
||||
}
|
||||
|
||||
// sleepCtx 是 worker 包自己的实现:rabbitmq.sleepCtx 未导出,不能跨包复用。
|
||||
// 返回 false 表示 ctx 已取消(调用方应尽快退出,不要再处理下一条消息)。
|
||||
// sleepCtx 返回 false 表示 ctx 已取消。
|
||||
func sleepCtx(ctx context.Context, d time.Duration) bool {
|
||||
t := time.NewTimer(d)
|
||||
defer t.Stop()
|
||||
|
||||
@@ -42,8 +42,6 @@ func (w *PopularityWorker) process(ctx context.Context, d amqp.Delivery) error {
|
||||
if evt.VideoID == 0 || evt.Change == 0 {
|
||||
return rabbitmq.Permanent(fmt.Errorf("popularity worker: invalid event video_id=%d change=%d", evt.VideoID, evt.Change))
|
||||
}
|
||||
// 关键:这里必须把错误上抛(P0-3 的 N2)。原来三条路径全返回 nil,
|
||||
// 于是重试分支全是死代码——Redis 短暂不可用就等于热度凭空丢失,
|
||||
// 且没有任何日志能证明发生过。
|
||||
// 必须把错误上抛,否则重试分支是死代码。
|
||||
return video.UpdatePopularityCache(ctx, w.cache, evt.VideoID, evt.Change, evt.EventID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user