feat(worker): 五个消费者改用 ConsumeLoop 分派重试与停泊
每个 worker 的 Run 只保留参数校验。消费循环统一由 rabbitmq.ConsumeLoop 驱动。 因此重试、停泊、Ack 的规则只有一份实现。 坏消息一律标记 rabbitmq.Permanent。因此解析失败与未知 action 立即停泊。 未知 action 不再被静默 Ack 丢弃。 notification worker 改按队列与 body.action 分派。重试回源会把 routing key 改写成队列名。因此按 routing key 判断会让第二次投递立即停泊。 验证:go test -count=1 ./... 全绿
This commit is contained in:
@@ -4,97 +4,46 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"feedsystem_video_go/internal/middleware/rabbitmq"
|
||||
rediscache "feedsystem_video_go/internal/middleware/redis"
|
||||
"feedsystem_video_go/internal/video"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
)
|
||||
|
||||
type PopularityWorker struct {
|
||||
ch *amqp.Channel
|
||||
cache *rediscache.Client
|
||||
queue string
|
||||
ch *amqp.Channel
|
||||
parker rabbitmq.Parker
|
||||
cache *rediscache.Client
|
||||
queue string
|
||||
}
|
||||
|
||||
func NewPopularityWorker(ch *amqp.Channel, cache *rediscache.Client, queue string) *PopularityWorker {
|
||||
return &PopularityWorker{ch: ch, cache: cache, queue: queue}
|
||||
func NewPopularityWorker(ch *amqp.Channel, parker rabbitmq.Parker, cache *rediscache.Client, queue string) *PopularityWorker {
|
||||
return &PopularityWorker{ch: ch, parker: parker, cache: cache, queue: queue}
|
||||
}
|
||||
|
||||
func (w *PopularityWorker) Run(ctx context.Context) error {
|
||||
if w == nil || w.ch == nil || w.cache == nil {
|
||||
if w == nil || w.ch == nil {
|
||||
return errors.New("popularity worker is not initialized")
|
||||
}
|
||||
if w.queue == "" {
|
||||
return errors.New("queue is required")
|
||||
}
|
||||
|
||||
deliveries, err := w.ch.Consume(
|
||||
w.queue,
|
||||
"",
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case d, ok := <-deliveries:
|
||||
if !ok {
|
||||
return errors.New("deliveries channel closed")
|
||||
}
|
||||
w.handleDelivery(ctx, d)
|
||||
}
|
||||
}
|
||||
return rabbitmq.ConsumeLoop(ctx, w.ch, w.parker, w.queue, rabbitmq.MaxAttempts, w.process)
|
||||
}
|
||||
|
||||
func (w *PopularityWorker) handleDelivery(ctx context.Context, d amqp.Delivery) {
|
||||
const maxRetries = 3
|
||||
for i := 0; i <= maxRetries; i++ {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
_ = d.Nack(false, true)
|
||||
return
|
||||
default:
|
||||
}
|
||||
if err := w.process(ctx, d.Body); err != nil {
|
||||
if i >= maxRetries {
|
||||
log.Printf("popularity worker: 重试 %d 次后仍失败, 丢弃: %v", maxRetries, err)
|
||||
_ = d.Ack(false)
|
||||
return
|
||||
}
|
||||
wait := time.Duration(1<<uint(i)) * time.Second
|
||||
log.Printf("popularity worker: 处理失败, %v 后重试 (%d/%d): %v", wait, i+1, maxRetries, err)
|
||||
time.Sleep(wait)
|
||||
continue
|
||||
}
|
||||
_ = d.Ack(false)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (w *PopularityWorker) process(ctx context.Context, body []byte) error {
|
||||
func (w *PopularityWorker) process(ctx context.Context, d amqp.Delivery) error {
|
||||
var evt rabbitmq.PopularityEvent
|
||||
if err := json.Unmarshal(body, &evt); err != nil {
|
||||
// 坏消息重投多少次都是坏的。等消费层引入 Permanent 之后这里会改成
|
||||
// "立即停泊";当前重试循环不认识不可重试错误,保持返回 nil 才是
|
||||
// 与现状等价的最小改动(否则会白白阻塞队列 7 秒)。
|
||||
return nil
|
||||
if err := json.Unmarshal(d.Body, &evt); err != nil {
|
||||
return rabbitmq.Permanent(fmt.Errorf("popularity worker: decode event: %w", err))
|
||||
}
|
||||
if evt.VideoID == 0 || evt.Change == 0 {
|
||||
return nil
|
||||
return rabbitmq.Permanent(fmt.Errorf("popularity worker: invalid event video_id=%d change=%d", evt.VideoID, evt.Change))
|
||||
}
|
||||
// 关键:这里必须把错误上抛(P0-3 的 N2)。原来三条路径全返回 nil,
|
||||
// 于是 handleDelivery 里的 Nack/重试/sleep 全是死代码——Redis 短暂不可用
|
||||
// 就等于热度凭空丢失,且没有任何日志能证明发生过。
|
||||
// 于是重试分支全是死代码——Redis 短暂不可用就等于热度凭空丢失,
|
||||
// 且没有任何日志能证明发生过。
|
||||
return video.UpdatePopularityCache(ctx, w.cache, evt.VideoID, evt.Change, evt.EventID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user