package rabbitmq import ( "context" "errors" "time" ) // PopularityMQ 通过连接管理器发布热度事件(不再持有长生命周期 channel)。 type PopularityMQ struct { mgr *RabbitMQ } // 拓扑常量导出:见 likeMQ.go 中的说明。 const ( PopularityExchange = "video.popularity.events" PopularityQueue = "video.popularity.events" PopularityBindingKey = "video.popularity.*" PopularityUpdateRK = "video.popularity.update" ) 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") } base.RegisterTopology(Topology{Exchange: PopularityExchange, Queue: PopularityQueue, BindingKey: PopularityBindingKey}) return &PopularityMQ{mgr: base}, nil } func (p *PopularityMQ) Update(ctx context.Context, videoID uint, change int64) error { if p == nil || p.mgr == nil { 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(), } return p.mgr.PublishJSON(ctx, PopularityExchange, PopularityUpdateRK, event) }