2025-12-30 01:25:30 +08:00
|
|
|
|
package rabbitmq
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-10-14 14:04:14 +08:00
|
|
|
|
// PopularityMQ 通过连接管理器发布热度事件(不再持有长生命周期 channel)。
|
2025-12-30 01:25:30 +08:00
|
|
|
|
type PopularityMQ struct {
|
2026-10-14 14:04:14 +08:00
|
|
|
|
mgr *RabbitMQ
|
2025-12-30 01:25:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-28 21:54:35 +08:00
|
|
|
|
// 拓扑常量导出:见 likeMQ.go 中的说明。
|
2025-12-30 01:25:30 +08:00
|
|
|
|
const (
|
2026-10-28 21:54:35 +08:00
|
|
|
|
PopularityExchange = "video.popularity.events"
|
|
|
|
|
|
PopularityQueue = "video.popularity.events"
|
|
|
|
|
|
PopularityBindingKey = "video.popularity.*"
|
2025-12-30 01:25:30 +08:00
|
|
|
|
|
2026-10-28 21:54:35 +08:00
|
|
|
|
PopularityUpdateRK = "video.popularity.update"
|
2025-12-30 01:25:30 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type PopularityEvent struct {
|
|
|
|
|
|
EventID string `json:"event_id"`
|
|
|
|
|
|
VideoID uint `json:"video_id"`
|
|
|
|
|
|
Change int64 `json:"change"`
|
|
|
|
|
|
OccurredAt time.Time `json:"occurred_at"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewPopularityMQ(base *RabbitMQ) (*PopularityMQ, error) {
|
|
|
|
|
|
if base == nil {
|
|
|
|
|
|
return nil, errors.New("rabbitmq base is nil")
|
|
|
|
|
|
}
|
2026-10-28 21:54:35 +08:00
|
|
|
|
base.RegisterTopology(Topology{Exchange: PopularityExchange, Queue: PopularityQueue, BindingKey: PopularityBindingKey})
|
2026-10-14 14:04:14 +08:00
|
|
|
|
return &PopularityMQ{mgr: base}, nil
|
2025-12-30 01:25:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *PopularityMQ) Update(ctx context.Context, videoID uint, change int64) error {
|
2026-10-14 14:04:14 +08:00
|
|
|
|
if p == nil || p.mgr == nil {
|
2025-12-30 01:25:30 +08:00
|
|
|
|
return errors.New("popularity mq is not initialized")
|
|
|
|
|
|
}
|
|
|
|
|
|
if videoID == 0 || change == 0 {
|
|
|
|
|
|
return errors.New("videoID and change are required")
|
|
|
|
|
|
}
|
|
|
|
|
|
id, err := newEventID(16)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
event := PopularityEvent{
|
|
|
|
|
|
EventID: id,
|
|
|
|
|
|
VideoID: videoID,
|
|
|
|
|
|
Change: change,
|
|
|
|
|
|
OccurredAt: time.Now().UTC(),
|
|
|
|
|
|
}
|
2026-10-28 21:54:35 +08:00
|
|
|
|
return p.mgr.PublishJSON(ctx, PopularityExchange, PopularityUpdateRK, event)
|
2025-12-30 01:25:30 +08:00
|
|
|
|
}
|