Files
VLoop/backend/internal/middleware/rabbitmq/likeMQ.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

77 lines
2.3 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 rabbitmq
import (
"context"
"errors"
"time"
)
// LikeMQ 通过连接管理器发布点赞事件。
// 它不再持有长生命周期 channel——连接断开后由管理器重建,发布自动恢复。
type LikeMQ struct {
mgr *RabbitMQ
}
// 拓扑常量导出:交换机名 / 队列名 / 绑定键是跨进程的"线缆契约",
// cmd/worker 与 http 层登记拓扑时必须复用同一份定义,不能再各自写字符串字面量。
const (
LikeExchange = "like.events"
LikeQueue = "like.events"
LikeBindingKey = "like.*"
LikeLikeRK = "like.like" // 点赞路由键
LikeUnlikeRK = "like.unlike" // 取消点赞路由键
)
type LikeEvent struct {
EventID string `json:"event_id"`
Action string `json:"action"`
UserID uint `json:"user_id"`
VideoID uint `json:"video_id"`
OccurredAt time.Time `json:"occurred_at"`
}
func NewLikeMQ(base *RabbitMQ) (*LikeMQ, error) {
if base == nil {
return nil, errors.New("rabbitmq base is nil")
}
// 拓扑交给管理器登记:每一代连接都会自动重放,不再只声明一次
base.RegisterTopology(Topology{Exchange: LikeExchange, Queue: LikeQueue, BindingKey: LikeBindingKey})
return &LikeMQ{mgr: base}, nil
}
// Like / Unlike 的 intentKey 语义见 PopularityMQ.Update:同一个键会同时
// 出现在点赞事件与热度事件的 event_id 里,让两条投递链路共享一次判重身份。
func (l *LikeMQ) Like(ctx context.Context, userID, videoID uint, intentKey string) error {
return l.publish(ctx, "like", LikeLikeRK, userID, videoID, intentKey)
}
func (l *LikeMQ) Unlike(ctx context.Context, userID, videoID uint, intentKey string) error {
return l.publish(ctx, "unlike", LikeUnlikeRK, userID, videoID, intentKey)
}
func (l *LikeMQ) publish(ctx context.Context, action, routingKey string, userID, videoID uint, intentKey string) error {
if l == nil || l.mgr == nil {
return errors.New("like mq is not initialized")
}
if userID == 0 || videoID == 0 {
return errors.New("userID and videoID are required")
}
id := intentKey
if id == "" {
var err error
id, err = newEventID(16)
if err != nil {
return err
}
}
event := LikeEvent{
EventID: id,
Action: action,
UserID: userID,
VideoID: videoID,
OccurredAt: time.Now(),
}
return l.mgr.PublishJSON(ctx, LikeExchange, routingKey, event)
}