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:
2026-10-30 15:20:15 +08:00
parent 03d4301020
commit 49fc17c445
10 changed files with 290 additions and 369 deletions

View File

@@ -4,23 +4,24 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"feedsystem_video_go/internal/middleware/rabbitmq"
"feedsystem_video_go/internal/social"
"log"
"time"
"github.com/go-sql-driver/mysql"
amqp "github.com/rabbitmq/amqp091-go"
)
type SocialWorker struct {
ch *amqp.Channel
repo *social.SocialRepository
queue string
ch *amqp.Channel
parker rabbitmq.Parker
repo *social.SocialRepository
queue string
}
func NewSocialWorker(ch *amqp.Channel, repo *social.SocialRepository, queue string) *SocialWorker {
return &SocialWorker{ch: ch, repo: repo, queue: queue}
func NewSocialWorker(ch *amqp.Channel, parker rabbitmq.Parker, repo *social.SocialRepository, queue string) *SocialWorker {
return &SocialWorker{ch: ch, parker: parker, repo: repo, queue: queue}
}
func (w *SocialWorker) Run(ctx context.Context) error {
@@ -30,66 +31,16 @@ func (w *SocialWorker) Run(ctx context.Context) error {
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 *SocialWorker) 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("social worker: 重试 %d 次后仍失败, 丢弃: %v", maxRetries, err)
_ = d.Ack(false)
return
}
wait := time.Duration(1<<uint(i)) * time.Second
log.Printf("social worker: 处理失败, %v 后重试 (%d/%d): %v", wait, i+1, maxRetries, err)
time.Sleep(wait)
continue
}
_ = d.Ack(false)
return
}
}
func (w *SocialWorker) process(ctx context.Context, body []byte) error {
func (w *SocialWorker) process(ctx context.Context, d amqp.Delivery) error {
var evt rabbitmq.SocialEvent
if err := json.Unmarshal(body, &evt); err != nil {
// 解析事件失败,直接丢弃
return nil
if err := json.Unmarshal(d.Body, &evt); err != nil {
return rabbitmq.Permanent(fmt.Errorf("social worker: decode event: %w", err))
}
if evt.FollowerID == 0 || evt.VloggerID == 0 {
return nil
return rabbitmq.Permanent(fmt.Errorf("social worker: invalid event follower_id=%d vlogger_id=%d", evt.FollowerID, evt.VloggerID))
}
switch evt.Action {
@@ -101,6 +52,7 @@ func (w *SocialWorker) process(ctx context.Context, body []byte) error {
if err == nil {
return nil
}
// 重复投递:关注关系已在,唯一索引挡下第二条。这是成功语义。
var mysqlErr *mysql.MySQLError
if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 {
return nil
@@ -112,6 +64,6 @@ func (w *SocialWorker) process(ctx context.Context, body []byte) error {
VloggerID: evt.VloggerID,
})
default:
return nil
return rabbitmq.Permanent(fmt.Errorf("social worker: unknown action %q", evt.Action))
}
}