From 49fc17c4451ca4eb27116993a5e094b9f67b2cc3 Mon Sep 17 00:00:00 2001 From: huanghaosheng <386998068@qq.com> Date: Fri, 30 Oct 2026 15:20:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(worker):=20=E4=BA=94=E4=B8=AA=E6=B6=88?= =?UTF-8?q?=E8=B4=B9=E8=80=85=E6=94=B9=E7=94=A8=20ConsumeLoop=20=E5=88=86?= =?UTF-8?q?=E6=B4=BE=E9=87=8D=E8=AF=95=E4=B8=8E=E5=81=9C=E6=B3=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 每个 worker 的 Run 只保留参数校验。消费循环统一由 rabbitmq.ConsumeLoop 驱动。 因此重试、停泊、Ack 的规则只有一份实现。 坏消息一律标记 rabbitmq.Permanent。因此解析失败与未知 action 立即停泊。 未知 action 不再被静默 Ack 丢弃。 notification worker 改按队列与 body.action 分派。重试回源会把 routing key 改写成队列名。因此按 routing key 判断会让第二次投递立即停泊。 验证:go test -count=1 ./... 全绿 --- backend/cmd/worker/main.go | 8 +- backend/internal/http/router.go | 2 +- backend/internal/worker/commentworker.go | 77 ++---- .../worker/commentworker_integration_test.go | 6 +- backend/internal/worker/likeworker.go | 76 ++---- backend/internal/worker/notificationworker.go | 232 ++++++++++-------- .../notificationworker_integration_test.go | 31 ++- .../worker/notificationworker_test.go | 66 +++++ backend/internal/worker/popularityworker.go | 83 ++----- backend/internal/worker/socialworker.go | 78 ++---- 10 files changed, 290 insertions(+), 369 deletions(-) create mode 100644 backend/internal/worker/notificationworker_test.go diff --git a/backend/cmd/worker/main.go b/backend/cmd/worker/main.go index 8bc4cac..c261716 100644 --- a/backend/cmd/worker/main.go +++ b/backend/cmd/worker/main.go @@ -153,17 +153,17 @@ func main() { return worker.NewFailedEventWorker(ch, failedEventRepo, mqrabbit.ParkedQueue).Run(ctx) }) go mqrabbit.RunConsumer(ctx, rmqMgr, "SocialWorker", 50, func(ch *amqp.Channel) error { - return worker.NewSocialWorker(ch, socialRepo, mqrabbit.SocialQueue).Run(ctx) + return worker.NewSocialWorker(ch, rmqMgr, socialRepo, mqrabbit.SocialQueue).Run(ctx) }) go mqrabbit.RunConsumer(ctx, rmqMgr, "LikeWorker", 50, func(ch *amqp.Channel) error { - return worker.NewLikeWorker(ch, likeRepo, videoRepo, mqrabbit.LikeQueue).Run(ctx) + return worker.NewLikeWorker(ch, rmqMgr, likeRepo, videoRepo, mqrabbit.LikeQueue).Run(ctx) }) go mqrabbit.RunConsumer(ctx, rmqMgr, "CommentWorker", 50, func(ch *amqp.Channel) error { - return worker.NewCommentWorker(ch, commentRepo, videoRepo, mqrabbit.CommentQueue).Run(ctx) + return worker.NewCommentWorker(ch, rmqMgr, commentRepo, videoRepo, mqrabbit.CommentQueue).Run(ctx) }) if cache != nil { go mqrabbit.RunConsumer(ctx, rmqMgr, "PopularityWorker", 50, func(ch *amqp.Channel) error { - return worker.NewPopularityWorker(ch, cache, mqrabbit.PopularityQueue).Run(ctx) + return worker.NewPopularityWorker(ch, rmqMgr, cache, mqrabbit.PopularityQueue).Run(ctx) }) } diff --git a/backend/internal/http/router.go b/backend/internal/http/router.go index d8350c7..0eec74c 100644 --- a/backend/internal/http/router.go +++ b/backend/internal/http/router.go @@ -246,7 +246,7 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *g queue := q go rabbitmq.RunConsumer(context.Background(), rmq, "notification-"+queue, 10, func(ch *amqp.Channel) error { - return worker.NewNotificationWorker(ch, db, queue, sseHub).Run(context.Background()) + return worker.NewNotificationWorker(ch, rmq, db, queue, sseHub).Run(context.Background()) }) } } else { diff --git a/backend/internal/worker/commentworker.go b/backend/internal/worker/commentworker.go index b7e3651..ab3c382 100644 --- a/backend/internal/worker/commentworker.go +++ b/backend/internal/worker/commentworker.go @@ -4,24 +4,26 @@ import ( "context" "encoding/json" "errors" - "feedsystem_video_go/internal/middleware/rabbitmq" - "feedsystem_video_go/internal/video" + "fmt" "log" "strings" - "time" + + "feedsystem_video_go/internal/middleware/rabbitmq" + "feedsystem_video_go/internal/video" amqp "github.com/rabbitmq/amqp091-go" ) type CommentWorker struct { ch *amqp.Channel + parker rabbitmq.Parker comments video.CommentStore videos *video.VideoRepository queue string } -func NewCommentWorker(ch *amqp.Channel, comments video.CommentStore, videos *video.VideoRepository, queue string) *CommentWorker { - return &CommentWorker{ch: ch, comments: comments, videos: videos, queue: queue} +func NewCommentWorker(ch *amqp.Channel, parker rabbitmq.Parker, comments video.CommentStore, videos *video.VideoRepository, queue string) *CommentWorker { + return &CommentWorker{ch: ch, parker: parker, comments: comments, videos: videos, queue: queue} } func (w *CommentWorker) Run(ctx context.Context) error { @@ -31,76 +33,28 @@ func (w *CommentWorker) 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 *CommentWorker) 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("comment worker: 重试 %d 次后仍失败, 丢弃: %v", maxRetries, err) - _ = d.Ack(false) - return - } - wait := time.Duration(1<= maxRetries { - log.Printf("like worker: 重试 %d 次后仍失败, 丢弃: %v", maxRetries, err) - _ = d.Ack(false) - return - } - wait := time.Duration(1<= maxRetries { - log.Printf("notification worker: 重试 %d 次后仍失败, 丢弃: %v", maxRetries, err) - _ = d.Ack(false) - return - } - wait := time.Duration(1<= maxRetries { - log.Printf("popularity worker: 重试 %d 次后仍失败, 丢弃: %v", maxRetries, err) - _ = d.Ack(false) - return - } - wait := time.Duration(1<= maxRetries { - log.Printf("social worker: 重试 %d 次后仍失败, 丢弃: %v", maxRetries, err) - _ = d.Ack(false) - return - } - wait := time.Duration(1<