style(backend): 清理幂等写入路径的注释
注释只保留一行级约束。 长段理由移入设计文档。 验证:go test -count=1 ./... 全绿。
This commit is contained in:
@@ -57,8 +57,7 @@ func (s *LikeService) Like(ctx context.Context, like *Like) error {
|
||||
|
||||
like.CreatedAt = time.Now()
|
||||
|
||||
// 一条意图键同时喂给"点赞事件"和"热度事件":两条消息若都重复投递,
|
||||
// 各自的消费端会用同一个键去重(点赞走唯一索引,热度走 redis 占位)。
|
||||
// 同一个键喂给点赞事件与热度事件,两条消费端各自去重。
|
||||
intentKey, err := NewIntentKey()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -77,14 +76,9 @@ func (s *LikeService) Like(ctx context.Context, like *Like) error {
|
||||
}
|
||||
}
|
||||
|
||||
// 两条轨各自独立降级(P0-4 §6.10.4)。
|
||||
//
|
||||
// 这里【不能】写成 `if !likeEnqueued && !popEnqueued` 之外的任何"合并判断":
|
||||
// 旧代码是"两个都成功才 return,否则无条件进兜底",于是"点赞已入队 +
|
||||
// 热度发布失败"会再插一次 likes → 撞 idx_like_video_account 1062 →
|
||||
// 接口返回 500,而用户其实点赞成功了。热度归热度,点赞归点赞。
|
||||
// 两条轨各自独立降级:合并判断会在"点赞已入队、热度失败"时再插一次 likes,
|
||||
// 撞 idx_like_video_account 并返回 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) {
|
||||
@@ -110,8 +104,7 @@ func (s *LikeService) Like(ctx context.Context, like *Like) error {
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: direct Redis update when popularity MQ publish fails.
|
||||
// 热度是"尽力而为"的展示数据,写失败只降级记日志,不回滚已经成立的点赞。
|
||||
// 热度是展示数据:写失败只降级记日志,不回滚已经成立的点赞。
|
||||
if !popEnqueued {
|
||||
if err := UpdatePopularityCache(ctx, s.cache, like.VideoID, 1, intentKey); err != nil {
|
||||
log.Printf("like: 热榜更新失败(已降级)video=%d: %v", like.VideoID, err)
|
||||
@@ -166,7 +159,6 @@ func (s *LikeService) Unlike(ctx context.Context, like *Like) error {
|
||||
|
||||
// 与 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 {
|
||||
@@ -188,7 +180,6 @@ func (s *LikeService) Unlike(ctx context.Context, like *Like) error {
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: direct Redis update when popularity MQ publish fails.
|
||||
if !popEnqueued {
|
||||
if err := UpdatePopularityCache(ctx, s.cache, like.VideoID, -1, intentKey); err != nil {
|
||||
log.Printf("unlike: 热榜更新失败(已降级)video=%d: %v", like.VideoID, err)
|
||||
|
||||
Reference in New Issue
Block a user