diff --git a/backend/internal/middleware/rabbitmq/commentMQ.go b/backend/internal/middleware/rabbitmq/commentMQ.go index d8d82ff..c9a9b08 100644 --- a/backend/internal/middleware/rabbitmq/commentMQ.go +++ b/backend/internal/middleware/rabbitmq/commentMQ.go @@ -4,12 +4,11 @@ import ( "context" "errors" "time" - - amqp "github.com/rabbitmq/amqp091-go" ) +// CommentMQ 通过连接管理器发布评论事件(不再持有长生命周期 channel)。 type CommentMQ struct { - ch *amqp.Channel + mgr *RabbitMQ } const ( @@ -36,15 +35,8 @@ func NewCommentMQ(base *RabbitMQ) (*CommentMQ, error) { if base == nil { return nil, errors.New("rabbitmq base is nil") } - ch, err := base.NewChannel() - if err != nil { - return nil, err - } - if err := DeclareTopic(ch, commentExchange, commentQueue, commentBindingKey); err != nil { - ch.Close() - return nil, err - } - return &CommentMQ{ch: ch}, nil + base.RegisterTopology(Topology{Exchange: commentExchange, Queue: commentQueue, BindingKey: commentBindingKey}) + return &CommentMQ{mgr: base}, nil } func (c *CommentMQ) Publish(ctx context.Context, username string, videoID, authorID uint, content string) error { @@ -63,7 +55,7 @@ func (c *CommentMQ) Delete(ctx context.Context, commentID uint) error { } func (c *CommentMQ) publish(ctx context.Context, action, routingKey string, evt CommentEvent) error { - if c == nil || c.ch == nil { + if c == nil || c.mgr == nil { return errors.New("comment mq is not initialized") } id, err := newEventID(16) @@ -73,5 +65,5 @@ func (c *CommentMQ) publish(ctx context.Context, action, routingKey string, evt evt.EventID = id evt.Action = action evt.OccurredAt = time.Now().UTC() - return PublishJSON(ctx, c.ch, commentExchange, routingKey, evt) + return c.mgr.PublishJSON(ctx, commentExchange, routingKey, evt) } diff --git a/backend/internal/middleware/rabbitmq/likeMQ.go b/backend/internal/middleware/rabbitmq/likeMQ.go index 62e6328..8df330a 100644 --- a/backend/internal/middleware/rabbitmq/likeMQ.go +++ b/backend/internal/middleware/rabbitmq/likeMQ.go @@ -4,12 +4,12 @@ import ( "context" "errors" "time" - - amqp "github.com/rabbitmq/amqp091-go" ) +// LikeMQ 通过连接管理器发布点赞事件。 +// 它不再持有长生命周期 channel——连接断开后由管理器重建,发布自动恢复。 type LikeMQ struct { - ch *amqp.Channel + mgr *RabbitMQ } const ( @@ -33,15 +33,9 @@ func NewLikeMQ(base *RabbitMQ) (*LikeMQ, error) { if base == nil { return nil, errors.New("rabbitmq base is nil") } - ch, err := base.NewChannel() - if err != nil { - return nil, err - } - if err := DeclareTopic(ch, likeExchange, likeQueue, likeBindingKey); err != nil { - ch.Close() - return nil, err - } - return &LikeMQ{ch: ch}, nil + // 拓扑交给管理器登记:每一代连接都会自动重放,不再只声明一次 + base.RegisterTopology(Topology{Exchange: likeExchange, Queue: likeQueue, BindingKey: likeBindingKey}) + return &LikeMQ{mgr: base}, nil } func (l *LikeMQ) Like(ctx context.Context, userID, videoID uint) error { @@ -53,7 +47,7 @@ func (l *LikeMQ) Unlike(ctx context.Context, userID, videoID uint) error { } func (l *LikeMQ) publish(ctx context.Context, action, routingKey string, userID, videoID uint) error { - if l == nil || l.ch == nil { + if l == nil || l.mgr == nil { return errors.New("like mq is not initialized") } if userID == 0 || videoID == 0 { @@ -70,5 +64,5 @@ func (l *LikeMQ) publish(ctx context.Context, action, routingKey string, userID, VideoID: videoID, OccurredAt: time.Now(), } - return PublishJSON(ctx, l.ch, likeExchange, routingKey, event) + return l.mgr.PublishJSON(ctx, likeExchange, routingKey, event) } diff --git a/backend/internal/middleware/rabbitmq/popularityMQ.go b/backend/internal/middleware/rabbitmq/popularityMQ.go index e829c78..73e1c8f 100644 --- a/backend/internal/middleware/rabbitmq/popularityMQ.go +++ b/backend/internal/middleware/rabbitmq/popularityMQ.go @@ -4,12 +4,11 @@ import ( "context" "errors" "time" - - amqp "github.com/rabbitmq/amqp091-go" ) +// PopularityMQ 通过连接管理器发布热度事件(不再持有长生命周期 channel)。 type PopularityMQ struct { - ch *amqp.Channel + mgr *RabbitMQ } const ( @@ -31,19 +30,12 @@ func NewPopularityMQ(base *RabbitMQ) (*PopularityMQ, error) { if base == nil { return nil, errors.New("rabbitmq base is nil") } - ch, err := base.NewChannel() - if err != nil { - return nil, err - } - if err := DeclareTopic(ch, popularityExchange, popularityQueue, popularityBindingKey); err != nil { - ch.Close() - return nil, err - } - return &PopularityMQ{ch: ch}, 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.ch == nil { + if p == nil || p.mgr == nil { return errors.New("popularity mq is not initialized") } if videoID == 0 || change == 0 { @@ -59,5 +51,5 @@ func (p *PopularityMQ) Update(ctx context.Context, videoID uint, change int64) e Change: change, OccurredAt: time.Now().UTC(), } - return PublishJSON(ctx, p.ch, popularityExchange, popularityUpdateRK, event) + return p.mgr.PublishJSON(ctx, popularityExchange, popularityUpdateRK, event) } diff --git a/backend/internal/middleware/rabbitmq/rabbitMQ.go b/backend/internal/middleware/rabbitmq/rabbitMQ.go index c3676a9..7f10c91 100644 --- a/backend/internal/middleware/rabbitmq/rabbitMQ.go +++ b/backend/internal/middleware/rabbitmq/rabbitMQ.go @@ -557,25 +557,6 @@ func (r *RabbitMQ) invalidatePublisherChannel(ch *amqp.Channel) { _ = ch.Close() } -// PublishJSON 在调用方自备的 channel 上发布一条 JSON 消息。 -// -// 【临时兼容函数】本次提交只迁移了消费侧与接线,5 个 publisher 仍各自持有 -// 长生命周期 channel 并调用本函数;下一次提交把它们改为经 -// (*RabbitMQ).PublishJSON 发布后,本函数即被删除,不留死代码。 -func PublishJSON(ctx context.Context, ch *amqp.Channel, exchange string, routingKey string, payload any) error { - if ch == nil { - return errors.New("channel is not initialized") - } - if exchange == "" || routingKey == "" { - return errors.New("exchange and routingKey are required") - } - pub, err := buildPublishing(payload) - if err != nil { - return err - } - return ch.PublishWithContext(ctx, exchange, routingKey, false, false, pub) -} - func buildPublishing(payload any) (amqp.Publishing, error) { b, err := json.Marshal(payload) if err != nil { diff --git a/backend/internal/middleware/rabbitmq/socialMQ.go b/backend/internal/middleware/rabbitmq/socialMQ.go index b8df788..763afcf 100644 --- a/backend/internal/middleware/rabbitmq/socialMQ.go +++ b/backend/internal/middleware/rabbitmq/socialMQ.go @@ -4,12 +4,11 @@ import ( "context" "errors" "time" - - amqp "github.com/rabbitmq/amqp091-go" ) +// SocialMQ 通过连接管理器发布关注事件(不再持有长生命周期 channel)。 type SocialMQ struct { - ch *amqp.Channel + mgr *RabbitMQ } const ( @@ -33,15 +32,8 @@ func NewSocialMQ(base *RabbitMQ) (*SocialMQ, error) { if base == nil { return nil, errors.New("rabbitmq base is nil") } - ch, err := base.NewChannel() - if err != nil { - return nil, err - } - if err := DeclareTopic(ch, socialExchange, socialQueue, socialBindingKey); err != nil { - ch.Close() - return nil, err - } - return &SocialMQ{ch: ch}, nil + base.RegisterTopology(Topology{Exchange: socialExchange, Queue: socialQueue, BindingKey: socialBindingKey}) + return &SocialMQ{mgr: base}, nil } func (s *SocialMQ) Follow(ctx context.Context, followerID, vloggerID uint) error { @@ -53,7 +45,7 @@ func (s *SocialMQ) UnFollow(ctx context.Context, followerID, vloggerID uint) err } func (s *SocialMQ) publish(ctx context.Context, action, routingKey string, followerID, vloggerID uint) error { - if s == nil || s.ch == nil { + if s == nil || s.mgr == nil { return errors.New("social mq is not initialized") } if followerID == 0 || vloggerID == 0 { @@ -70,5 +62,5 @@ func (s *SocialMQ) publish(ctx context.Context, action, routingKey string, follo VloggerID: vloggerID, OccurredAt: time.Now().UTC(), } - return PublishJSON(ctx, s.ch, socialExchange, routingKey, evt) + return s.mgr.PublishJSON(ctx, socialExchange, routingKey, evt) } diff --git a/backend/internal/middleware/rabbitmq/timelineMQ.go b/backend/internal/middleware/rabbitmq/timelineMQ.go index f25320a..ec99aab 100644 --- a/backend/internal/middleware/rabbitmq/timelineMQ.go +++ b/backend/internal/middleware/rabbitmq/timelineMQ.go @@ -4,12 +4,11 @@ import ( "context" "errors" "time" - - amqp "github.com/rabbitmq/amqp091-go" ) +// TimelineMQ 通过连接管理器发布时间线事件(不再持有长生命周期 channel)。 type TimelineMQ struct { - ch *amqp.Channel + mgr *RabbitMQ } const ( @@ -30,19 +29,12 @@ func NewTimelineMQ(base *RabbitMQ) (*TimelineMQ, error) { if base == nil { return nil, errors.New("rabbitmq base is nil") } - ch, err := base.NewChannel() - if err != nil { - return nil, err - } - if err := DeclareTopic(ch, timelineExchange, timelineQueue, timelineBindingKey); err != nil { - ch.Close() - return nil, err - } - return &TimelineMQ{ch: ch}, nil + base.RegisterTopology(Topology{Exchange: timelineExchange, Queue: timelineQueue, BindingKey: timelineBindingKey}) + return &TimelineMQ{mgr: base}, nil } func (t *TimelineMQ) PublishVideo(ctx context.Context, videoID uint, createTime time.Time) error { - if t == nil || t.ch == nil { + if t == nil || t.mgr == nil { return errors.New("timeline mq is not initialized") } if videoID == 0 { @@ -58,5 +50,5 @@ func (t *TimelineMQ) PublishVideo(ctx context.Context, videoID uint, createTime CreateTime: createTime.UnixMilli(), OccurredAt: time.Now(), } - return PublishJSON(ctx, t.ch, timelineExchange, timelinePublishRK, timeline) + return t.mgr.PublishJSON(ctx, timelineExchange, timelinePublishRK, timeline) }