2025-12-30 01:23:54 +08:00
|
|
|
|
package rabbitmq
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-10-14 14:04:14 +08:00
|
|
|
|
// LikeMQ 通过连接管理器发布点赞事件。
|
|
|
|
|
|
// 它不再持有长生命周期 channel——连接断开后由管理器重建,发布自动恢复。
|
2025-12-30 01:23:54 +08:00
|
|
|
|
type LikeMQ struct {
|
2026-10-14 14:04:14 +08:00
|
|
|
|
mgr *RabbitMQ
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-28 21:54:35 +08:00
|
|
|
|
// 拓扑常量导出:交换机名 / 队列名 / 绑定键是跨进程的"线缆契约",
|
|
|
|
|
|
// cmd/worker 与 http 层登记拓扑时必须复用同一份定义,不能再各自写字符串字面量。
|
2025-12-30 01:23:54 +08:00
|
|
|
|
const (
|
2026-10-28 21:54:35 +08:00
|
|
|
|
LikeExchange = "like.events"
|
|
|
|
|
|
LikeQueue = "like.events"
|
|
|
|
|
|
LikeBindingKey = "like.*"
|
2025-12-30 01:23:54 +08:00
|
|
|
|
|
2026-10-28 21:54:35 +08:00
|
|
|
|
LikeLikeRK = "like.like" // 点赞路由键
|
|
|
|
|
|
LikeUnlikeRK = "like.unlike" // 取消点赞路由键
|
2025-12-30 01:23:54 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type LikeEvent struct {
|
|
|
|
|
|
EventID string `json:"event_id"`
|
|
|
|
|
|
Action string `json:"action"`
|
|
|
|
|
|
UserID uint `json:"user_id"`
|
|
|
|
|
|
VideoID uint `json:"video_id"`
|
|
|
|
|
|
OccurredAt time.Time `json:"occurred_at"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewLikeMQ(base *RabbitMQ) (*LikeMQ, error) {
|
|
|
|
|
|
if base == nil {
|
|
|
|
|
|
return nil, errors.New("rabbitmq base is nil")
|
|
|
|
|
|
}
|
2026-10-14 14:04:14 +08:00
|
|
|
|
// 拓扑交给管理器登记:每一代连接都会自动重放,不再只声明一次
|
2026-10-28 21:54:35 +08:00
|
|
|
|
base.RegisterTopology(Topology{Exchange: LikeExchange, Queue: LikeQueue, BindingKey: LikeBindingKey})
|
2026-10-14 14:04:14 +08:00
|
|
|
|
return &LikeMQ{mgr: base}, nil
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 13:25:48 +08:00
|
|
|
|
// Like / Unlike 的 intentKey 语义见 PopularityMQ.Update:同一个键会同时
|
|
|
|
|
|
// 出现在点赞事件与热度事件的 event_id 里,让两条投递链路共享一次判重身份。
|
|
|
|
|
|
func (l *LikeMQ) Like(ctx context.Context, userID, videoID uint, intentKey string) error {
|
|
|
|
|
|
return l.publish(ctx, "like", LikeLikeRK, userID, videoID, intentKey)
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 13:25:48 +08:00
|
|
|
|
func (l *LikeMQ) Unlike(ctx context.Context, userID, videoID uint, intentKey string) error {
|
|
|
|
|
|
return l.publish(ctx, "unlike", LikeUnlikeRK, userID, videoID, intentKey)
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 13:25:48 +08:00
|
|
|
|
func (l *LikeMQ) publish(ctx context.Context, action, routingKey string, userID, videoID uint, intentKey string) error {
|
2026-10-14 14:04:14 +08:00
|
|
|
|
if l == nil || l.mgr == nil {
|
2025-12-30 01:23:54 +08:00
|
|
|
|
return errors.New("like mq is not initialized")
|
|
|
|
|
|
}
|
|
|
|
|
|
if userID == 0 || videoID == 0 {
|
|
|
|
|
|
return errors.New("userID and videoID are required")
|
|
|
|
|
|
}
|
2026-10-30 13:25:48 +08:00
|
|
|
|
id := intentKey
|
|
|
|
|
|
if id == "" {
|
|
|
|
|
|
var err error
|
|
|
|
|
|
id, err = newEventID(16)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
event := LikeEvent{
|
|
|
|
|
|
EventID: id,
|
|
|
|
|
|
Action: action,
|
|
|
|
|
|
UserID: userID,
|
|
|
|
|
|
VideoID: videoID,
|
|
|
|
|
|
OccurredAt: time.Now(),
|
|
|
|
|
|
}
|
2026-10-28 21:54:35 +08:00
|
|
|
|
return l.mgr.PublishJSON(ctx, LikeExchange, routingKey, event)
|
2025-12-30 01:23:54 +08:00
|
|
|
|
}
|