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:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"feedsystem_video_go/internal/middleware/rabbitmq"
|
||||
rediscache "feedsystem_video_go/internal/middleware/redis"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
@@ -55,24 +56,35 @@ func (s *LikeService) Like(ctx context.Context, like *Like) error {
|
||||
}
|
||||
|
||||
like.CreatedAt = time.Now()
|
||||
mysqlEnqueued := false
|
||||
redisEnqueued := false
|
||||
|
||||
// 一条意图键同时喂给"点赞事件"和"热度事件":两条消息若都重复投递,
|
||||
// 各自的消费端会用同一个键去重(点赞走唯一索引,热度走 redis 占位)。
|
||||
intentKey, err := NewIntentKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
likeEnqueued := false
|
||||
popEnqueued := false
|
||||
if s.likeMQ != nil {
|
||||
if err := s.likeMQ.Like(ctx, like.AccountID, like.VideoID); err == nil {
|
||||
mysqlEnqueued = true
|
||||
if err := s.likeMQ.Like(ctx, like.AccountID, like.VideoID, intentKey); err == nil {
|
||||
likeEnqueued = true
|
||||
}
|
||||
}
|
||||
if s.popularityMQ != nil {
|
||||
if err := s.popularityMQ.Update(ctx, like.VideoID, 1); err == nil {
|
||||
redisEnqueued = true
|
||||
if err := s.popularityMQ.Update(ctx, like.VideoID, 1, intentKey); err == nil {
|
||||
popEnqueued = true
|
||||
}
|
||||
}
|
||||
if mysqlEnqueued && redisEnqueued {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Fallback: direct MySQL write when like MQ publish fails.
|
||||
if !mysqlEnqueued {
|
||||
// 两条轨各自独立降级(P0-4 §6.10.4)。
|
||||
//
|
||||
// 这里【不能】写成 `if !likeEnqueued && !popEnqueued` 之外的任何"合并判断":
|
||||
// 旧代码是"两个都成功才 return,否则无条件进兜底",于是"点赞已入队 +
|
||||
// 热度发布失败"会再插一次 likes → 撞 idx_like_video_account 1062 →
|
||||
// 接口返回 500,而用户其实点赞成功了。热度归热度,点赞归点赞。
|
||||
if !likeEnqueued {
|
||||
// Fallback: direct MySQL write when like MQ publish fails.
|
||||
err := s.repo.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Select("id").First(&Video{}, like.VideoID).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -99,8 +111,11 @@ func (s *LikeService) Like(ctx context.Context, like *Like) error {
|
||||
}
|
||||
|
||||
// Fallback: direct Redis update when popularity MQ publish fails.
|
||||
if !redisEnqueued {
|
||||
UpdatePopularityCache(ctx, s.cache, like.VideoID, 1)
|
||||
// 热度是"尽力而为"的展示数据,写失败只降级记日志,不回滚已经成立的点赞。
|
||||
if !popEnqueued {
|
||||
if err := UpdatePopularityCache(ctx, s.cache, like.VideoID, 1, intentKey); err != nil {
|
||||
log.Printf("like: 热榜更新失败(已降级)video=%d: %v", like.VideoID, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -131,24 +146,27 @@ func (s *LikeService) Unlike(ctx context.Context, like *Like) error {
|
||||
return errors.New("user has not liked this video")
|
||||
}
|
||||
|
||||
mysqlEnqueued := false
|
||||
redisEnqueued := false
|
||||
intentKey, err := NewIntentKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
likeEnqueued := false
|
||||
popEnqueued := false
|
||||
if s.likeMQ != nil {
|
||||
if err := s.likeMQ.Unlike(ctx, like.AccountID, like.VideoID); err == nil {
|
||||
mysqlEnqueued = true
|
||||
if err := s.likeMQ.Unlike(ctx, like.AccountID, like.VideoID, intentKey); err == nil {
|
||||
likeEnqueued = true
|
||||
}
|
||||
}
|
||||
if s.popularityMQ != nil {
|
||||
if err := s.popularityMQ.Update(ctx, like.VideoID, -1); err == nil {
|
||||
redisEnqueued = true
|
||||
if err := s.popularityMQ.Update(ctx, like.VideoID, -1, intentKey); err == nil {
|
||||
popEnqueued = true
|
||||
}
|
||||
}
|
||||
if mysqlEnqueued && redisEnqueued {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Fallback: direct MySQL write when like MQ publish fails.
|
||||
if !mysqlEnqueued {
|
||||
// 与 Like 对称:两条轨各自降级,不做合并判断。
|
||||
if !likeEnqueued {
|
||||
// Fallback: direct MySQL write when like MQ publish fails.
|
||||
err := s.repo.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
del := tx.Where("video_id = ? AND account_id = ?", like.VideoID, like.AccountID).Delete(&Like{})
|
||||
if del.Error != nil {
|
||||
@@ -171,8 +189,10 @@ func (s *LikeService) Unlike(ctx context.Context, like *Like) error {
|
||||
}
|
||||
|
||||
// Fallback: direct Redis update when popularity MQ publish fails.
|
||||
if !redisEnqueued {
|
||||
UpdatePopularityCache(ctx, s.cache, like.VideoID, -1)
|
||||
if !popEnqueued {
|
||||
if err := UpdatePopularityCache(ctx, s.cache, like.VideoID, -1, intentKey); err != nil {
|
||||
log.Printf("unlike: 热榜更新失败(已降级)video=%d: %v", like.VideoID, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user