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。热度是展示数据,失败只降级 记日志,不回滚已经成立的点赞。
This commit is contained in:
@@ -35,16 +35,26 @@ func NewPopularityMQ(base *RabbitMQ) (*PopularityMQ, error) {
|
||||
return &PopularityMQ{mgr: base}, nil
|
||||
}
|
||||
|
||||
func (p *PopularityMQ) Update(ctx context.Context, videoID uint, change int64) error {
|
||||
// Update 发布一条热度事件。
|
||||
//
|
||||
// intentKey 是"这次用户意图"的稳定标识,会写进 event_id:调用方(如点赞)
|
||||
// 用同一个键同时发布点赞事件与热度事件,于是两条消息无论重复投递多少次,
|
||||
// 消费端都能认出"这是同一次意图"(P0-4)。intentKey 为空时退化为随机 id
|
||||
// ——注意此时消费端无法去重,所以生产路径必须传键。
|
||||
func (p *PopularityMQ) Update(ctx context.Context, videoID uint, change int64, intentKey string) error {
|
||||
if p == nil || p.mgr == nil {
|
||||
return errors.New("popularity mq is not initialized")
|
||||
}
|
||||
if videoID == 0 || change == 0 {
|
||||
return errors.New("videoID and change are required")
|
||||
}
|
||||
id, err := newEventID(16)
|
||||
if err != nil {
|
||||
return err
|
||||
id := intentKey
|
||||
if id == "" {
|
||||
var err error
|
||||
id, err = newEventID(16)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
event := PopularityEvent{
|
||||
EventID: id,
|
||||
|
||||
Reference in New Issue
Block a user