点赞与评论的计数只由业务事务写。ApplyLike、ApplyUnlike、CreateIdempotentWithPopularity 与 DeleteWithPopularity 各自在单事务内完成插行与计数。 热榜增量改为提交后的 best-effort 写。ApplyHotDelta 按行的 created_at 落分钟桶,写失败只记日志,DB 计数仍然正确。 删除 PopularityMQ 与 PopularityWorker。同一次互动因此只有一个写者,不再有两个门闩各自放行。 删除 hot:applied 占位键。判重改由 likes 的唯一索引与 uk_comments_author_key 负责。 DeleteWithPopularity 先读行再删行。评论删除因此回退热度,重复投递不再多减一次。 likes 与 comments 各加 (created_at, video_id) 覆盖索引,服务热榜窗口查询。 验证:go build -buildvcs=false ./... 通过。go test -buildvcs=false -count=1 ./... 全绿。
148 lines
3.7 KiB
Go
148 lines
3.7 KiB
Go
package video
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"feedsystem_video_go/internal/middleware/rabbitmq"
|
||
rediscache "feedsystem_video_go/internal/middleware/redis"
|
||
"log"
|
||
"time"
|
||
)
|
||
|
||
type LikeService struct {
|
||
repo *LikeRepository
|
||
VideoRepo *VideoRepository
|
||
cache *rediscache.Client
|
||
likeMQ *rabbitmq.LikeMQ
|
||
}
|
||
|
||
func NewLikeService(repo *LikeRepository, videoRepo *VideoRepository, cache *rediscache.Client, likeMQ *rabbitmq.LikeMQ) *LikeService {
|
||
return &LikeService{repo: repo, VideoRepo: videoRepo, cache: cache, likeMQ: likeMQ}
|
||
}
|
||
|
||
func (s *LikeService) Like(ctx context.Context, like *Like) error {
|
||
if like == nil {
|
||
return errors.New("like is nil")
|
||
}
|
||
if like.VideoID == 0 || like.AccountID == 0 {
|
||
return errors.New("video_id and account_id are required")
|
||
}
|
||
|
||
if s.VideoRepo != nil {
|
||
ok, err := s.VideoRepo.IsExist(ctx, like.VideoID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if !ok {
|
||
return errors.New("video not found")
|
||
}
|
||
}
|
||
|
||
isLiked, err := s.repo.IsLiked(ctx, like.VideoID, like.AccountID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if isLiked {
|
||
return errors.New("user has liked this video")
|
||
}
|
||
|
||
like.CreatedAt = time.Now()
|
||
|
||
// 意图键只用于日志关联(LikeEvent.EventID),不再参与去重:
|
||
// 判重由 likes 的唯一索引在事务里做,重复投递不会重复计数。
|
||
intentKey, err := NewIntentKey()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
enqueued := false
|
||
if s.likeMQ != nil {
|
||
if err := s.likeMQ.Like(ctx, like.AccountID, like.VideoID, intentKey); err != nil {
|
||
log.Printf("like: enqueue like failed, fallback to direct write: %v", err)
|
||
} else {
|
||
enqueued = true
|
||
}
|
||
}
|
||
if enqueued {
|
||
// 入队成功即返回:落库与热榜都由 worker 在同一条事务路径上完成。
|
||
return nil
|
||
}
|
||
|
||
created, err := s.repo.ApplyLike(ctx, like)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if !created {
|
||
return errors.New("user has liked this video")
|
||
}
|
||
if err := ApplyHotDelta(ctx, s.cache, like.VideoID, 1, like.CreatedAt); err != nil {
|
||
log.Printf("like: 热榜更新失败(已降级)video=%d: %v", like.VideoID, err)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (s *LikeService) Unlike(ctx context.Context, like *Like) error {
|
||
if like == nil {
|
||
return errors.New("like is nil")
|
||
}
|
||
if like.VideoID == 0 || like.AccountID == 0 {
|
||
return errors.New("video_id and account_id are required")
|
||
}
|
||
|
||
if s.VideoRepo != nil {
|
||
ok, err := s.VideoRepo.IsExist(ctx, like.VideoID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if !ok {
|
||
return errors.New("video not found")
|
||
}
|
||
}
|
||
|
||
isLiked, err := s.repo.IsLiked(ctx, like.VideoID, like.AccountID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if !isLiked {
|
||
return errors.New("user has not liked this video")
|
||
}
|
||
|
||
intentKey, err := NewIntentKey()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
enqueued := false
|
||
if s.likeMQ != nil {
|
||
if err := s.likeMQ.Unlike(ctx, like.AccountID, like.VideoID, intentKey); err != nil {
|
||
log.Printf("unlike: enqueue unlike failed, fallback to direct write: %v", err)
|
||
} else {
|
||
enqueued = true
|
||
}
|
||
}
|
||
if enqueued {
|
||
return nil
|
||
}
|
||
|
||
row, deleted, err := s.repo.ApplyUnlike(ctx, like.VideoID, like.AccountID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if !deleted {
|
||
return errors.New("user has not liked this video")
|
||
}
|
||
// 减回点赞那一分钟:热榜是窗口,不能按此刻的钟回退。
|
||
if err := ApplyHotDelta(ctx, s.cache, like.VideoID, -1, row.CreatedAt); err != nil {
|
||
log.Printf("unlike: 热榜更新失败(已降级)video=%d: %v", like.VideoID, err)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (s *LikeService) IsLiked(ctx context.Context, videoID, accountID uint) (bool, error) {
|
||
return s.repo.IsLiked(ctx, videoID, accountID)
|
||
}
|
||
|
||
func (s *LikeService) ListLikedVideos(ctx context.Context, accountID uint) ([]Video, error) {
|
||
return s.repo.ListLikedVideos(ctx, accountID)
|
||
}
|