Files
VLoop/backend/internal/worker/popularityworker.go
huanghaosheng 90a7feb6d3 feat(video): 热度写入单轨化,点赞兜底不再重复插入
热度有两个写入者(PopularityWorker 与兜底直写)和三份实现,其中
VideoService.UpdatePopularity 是死代码且漏删 video:entity 缓存。这里
收敛成一条轨,并让"加过分"这件事可以判重。

- popularity_cache: UpdatePopularityCache 改成 5 参数并返回 error,
  用 redis SETNX 占位 hot:applied:<意图键> 实现幂等;ZINCRBY 失败必须
  撤销占位再上抛(占位留在失败路径上会让增量被永久跳过);三次 Del 是
  best-effort,不受 opCtx 约束。返回 error 同时修掉 P0-3 的 N2——
  原来三条路径全返回 nil,消费端的 Nack/重试/sleep 全是死代码。
- popularityMQ.Update / likeMQ.Like/Unlike 增加 intentKey 入参,写进
  event_id,让点赞事件与热度事件共享一次判重身份。
- like_service: 兜底拆成两个独立的 if(!likeEnqueued 写 DB、
  !popEnqueued 写热榜)。旧代码"两个都成功才 return"会在"点赞已入队 +
  热度发布失败"时再插一次 likes,撞唯一索引返回 500,而点赞其实成功了。
- 删除 VideoService.UpdatePopularity 及其 popularityMQ 字段与构造参数
  (router.go 是唯一调用点)。

行为变更:点赞在热度发布失败时不再返回 500。热度是展示数据,失败只降级
记日志,不回滚已经成立的点赞。
2026-10-30 13:25:48 +08:00

101 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 {
// 坏消息重投多少次都是坏的。等消费层引入 Permanent 之后这里会改成
// "立即停泊";当前重试循环不认识不可重试错误,保持返回 nil 才是
// 与现状等价的最小改动(否则会白白阻塞队列 7 秒)。
return nil
}
if evt.VideoID == 0 || evt.Change == 0 {
return nil
}
// 关键:这里必须把错误上抛(P0-3 的 N2)。原来三条路径全返回 nil,
// 于是 handleDelivery 里的 Nack/重试/sleep 全是死代码——Redis 短暂不可用
// 就等于热度凭空丢失,且没有任何日志能证明发生过。
return video.UpdatePopularityCache(ctx, w.cache, evt.VideoID, evt.Change, evt.EventID)
}