Files
VLoop/backend/internal/worker/popularityworker.go
yiyiis 589116b141 fix: Worker 进程独立 Channel + 进程内指数退避重试
- 每个 Worker 消费者使用独立 AMQP Channel,互不影响
- 用 runWorkerWithRetry 包装每个 Worker,断开自动重连
- 用进程内指数退避重试替代失效的 Nack+DLX 重试机制
- 拓扑声明改用临时 Channel,声明完即关闭
2026-05-23 09:23:14 +08:00

96 lines
2.0 KiB
Go

package worker
import (
"context"
"encoding/json"
"errors"
"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
}
func NewPopularityWorker(ch *amqp.Channel, cache *rediscache.Client, queue string) *PopularityWorker {
return &PopularityWorker{ch: ch, cache: cache, queue: queue}
}
func (w *PopularityWorker) Run(ctx context.Context) error {
if w == nil || w.ch == nil || w.cache == 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)
}
}
}
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 {
var evt rabbitmq.PopularityEvent
if err := json.Unmarshal(body, &evt); err != nil {
return nil
}
if evt.VideoID == 0 || evt.Change == 0 {
return nil
}
video.UpdatePopularityCache(ctx, w.cache, evt.VideoID, evt.Change)
return nil
}