背景叙述与历史取舍已写进 `docs/design/hot-ranking.md`,从源码删除。 只保留不变式与安全约束:`clientFoundRows` 禁令、唯一索引判重、 成员补零的排序约定、快照用 `RENAMENX` 只建一次、`EXPIRE` 失败即删键。 `video_service.go` 的 `NewVideoService` 注释原本复述已删方法的来历,一并收敛。 `zset.go` 的 `ZUnionStore` 与 `RenameNX` 注释各压到四行以内。 验证:go build ./... 通过。 验证:go vet ./... 通过。 验证:go test -count=1 ./... 全绿(本机无 cgo,未跑 -race)。 验证:go test -tags integration -p 1 -count=1 ./internal/feed/ ./internal/video/ ./internal/worker/ 全绿。
147 lines
3.6 KiB
Go
147 lines
3.6 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()
|
||
|
||
// 意图键只用于日志关联,不参与去重:判重由唯一索引在事务里做。
|
||
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)
|
||
}
|