refactor(rabbitmq): 5 个 publisher 改经连接管理器发布
LikeMQ / CommentMQ / SocialMQ / PopularityMQ / TimelineMQ 原先各自在构造时取一个 *amqp.Channel 并终身持有,连接断开后发布持续失败,而 API 进程没有任何重建逻辑 (即报告 P1-13):业务数据因 DB 直写降级仍然正确,但通知、热度、时间线全部静默 停更,且不会自愈——只修消费者会得到一个"消费者活着、消息却不再产生"的半自愈系统。 改为:各 publisher 只持 *RabbitMQ,构造时用 RegisterTopology 登记拓扑(不再 "构造时声明一次"),发布统一走 mgr.PublishJSON;管理器内部为每一代连接复用一个 发布 channel,发布失败即丢弃缓存、随下一代重建。amqp091-go 的 Publish 内部持锁, 多 goroutine 复用同一 channel 是安全的(只是串行化)。 刻意不做"ErrClosed 时自动重发":那会在 P0-4 的双写窗口之上再叠加一次不确定性。 发布失败如实返回错误,交由既有的 DB 直写降级路径,幂等应交给事件层(event_id)。 各 NewXxxMQ 的签名保持不变,router.go 的 5 个构造点无需改动。同时删除上一个提交 为兼容尚未迁移的 publisher 而临时保留的包级 PublishJSON。
This commit is contained in:
@@ -4,12 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
amqp "github.com/rabbitmq/amqp091-go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// CommentMQ 通过连接管理器发布评论事件(不再持有长生命周期 channel)。
|
||||||
type CommentMQ struct {
|
type CommentMQ struct {
|
||||||
ch *amqp.Channel
|
mgr *RabbitMQ
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -36,15 +35,8 @@ func NewCommentMQ(base *RabbitMQ) (*CommentMQ, error) {
|
|||||||
if base == nil {
|
if base == nil {
|
||||||
return nil, errors.New("rabbitmq base is nil")
|
return nil, errors.New("rabbitmq base is nil")
|
||||||
}
|
}
|
||||||
ch, err := base.NewChannel()
|
base.RegisterTopology(Topology{Exchange: commentExchange, Queue: commentQueue, BindingKey: commentBindingKey})
|
||||||
if err != nil {
|
return &CommentMQ{mgr: base}, nil
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := DeclareTopic(ch, commentExchange, commentQueue, commentBindingKey); err != nil {
|
|
||||||
ch.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &CommentMQ{ch: ch}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CommentMQ) Publish(ctx context.Context, username string, videoID, authorID uint, content string) error {
|
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 {
|
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")
|
return errors.New("comment mq is not initialized")
|
||||||
}
|
}
|
||||||
id, err := newEventID(16)
|
id, err := newEventID(16)
|
||||||
@@ -73,5 +65,5 @@ func (c *CommentMQ) publish(ctx context.Context, action, routingKey string, evt
|
|||||||
evt.EventID = id
|
evt.EventID = id
|
||||||
evt.Action = action
|
evt.Action = action
|
||||||
evt.OccurredAt = time.Now().UTC()
|
evt.OccurredAt = time.Now().UTC()
|
||||||
return PublishJSON(ctx, c.ch, commentExchange, routingKey, evt)
|
return c.mgr.PublishJSON(ctx, commentExchange, routingKey, evt)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
amqp "github.com/rabbitmq/amqp091-go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// LikeMQ 通过连接管理器发布点赞事件。
|
||||||
|
// 它不再持有长生命周期 channel——连接断开后由管理器重建,发布自动恢复。
|
||||||
type LikeMQ struct {
|
type LikeMQ struct {
|
||||||
ch *amqp.Channel
|
mgr *RabbitMQ
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -33,15 +33,9 @@ func NewLikeMQ(base *RabbitMQ) (*LikeMQ, error) {
|
|||||||
if base == nil {
|
if base == nil {
|
||||||
return nil, errors.New("rabbitmq base is nil")
|
return nil, errors.New("rabbitmq base is nil")
|
||||||
}
|
}
|
||||||
ch, err := base.NewChannel()
|
// 拓扑交给管理器登记:每一代连接都会自动重放,不再只声明一次
|
||||||
if err != nil {
|
base.RegisterTopology(Topology{Exchange: likeExchange, Queue: likeQueue, BindingKey: likeBindingKey})
|
||||||
return nil, err
|
return &LikeMQ{mgr: base}, nil
|
||||||
}
|
|
||||||
if err := DeclareTopic(ch, likeExchange, likeQueue, likeBindingKey); err != nil {
|
|
||||||
ch.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &LikeMQ{ch: ch}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LikeMQ) Like(ctx context.Context, userID, videoID uint) error {
|
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 {
|
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")
|
return errors.New("like mq is not initialized")
|
||||||
}
|
}
|
||||||
if userID == 0 || videoID == 0 {
|
if userID == 0 || videoID == 0 {
|
||||||
@@ -70,5 +64,5 @@ func (l *LikeMQ) publish(ctx context.Context, action, routingKey string, userID,
|
|||||||
VideoID: videoID,
|
VideoID: videoID,
|
||||||
OccurredAt: time.Now(),
|
OccurredAt: time.Now(),
|
||||||
}
|
}
|
||||||
return PublishJSON(ctx, l.ch, likeExchange, routingKey, event)
|
return l.mgr.PublishJSON(ctx, likeExchange, routingKey, event)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
amqp "github.com/rabbitmq/amqp091-go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// PopularityMQ 通过连接管理器发布热度事件(不再持有长生命周期 channel)。
|
||||||
type PopularityMQ struct {
|
type PopularityMQ struct {
|
||||||
ch *amqp.Channel
|
mgr *RabbitMQ
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -31,19 +30,12 @@ func NewPopularityMQ(base *RabbitMQ) (*PopularityMQ, error) {
|
|||||||
if base == nil {
|
if base == nil {
|
||||||
return nil, errors.New("rabbitmq base is nil")
|
return nil, errors.New("rabbitmq base is nil")
|
||||||
}
|
}
|
||||||
ch, err := base.NewChannel()
|
base.RegisterTopology(Topology{Exchange: popularityExchange, Queue: popularityQueue, BindingKey: popularityBindingKey})
|
||||||
if err != nil {
|
return &PopularityMQ{mgr: base}, nil
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := DeclareTopic(ch, popularityExchange, popularityQueue, popularityBindingKey); err != nil {
|
|
||||||
ch.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &PopularityMQ{ch: ch}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PopularityMQ) Update(ctx context.Context, videoID uint, change int64) error {
|
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")
|
return errors.New("popularity mq is not initialized")
|
||||||
}
|
}
|
||||||
if videoID == 0 || change == 0 {
|
if videoID == 0 || change == 0 {
|
||||||
@@ -59,5 +51,5 @@ func (p *PopularityMQ) Update(ctx context.Context, videoID uint, change int64) e
|
|||||||
Change: change,
|
Change: change,
|
||||||
OccurredAt: time.Now().UTC(),
|
OccurredAt: time.Now().UTC(),
|
||||||
}
|
}
|
||||||
return PublishJSON(ctx, p.ch, popularityExchange, popularityUpdateRK, event)
|
return p.mgr.PublishJSON(ctx, popularityExchange, popularityUpdateRK, event)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -557,25 +557,6 @@ func (r *RabbitMQ) invalidatePublisherChannel(ch *amqp.Channel) {
|
|||||||
_ = ch.Close()
|
_ = 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) {
|
func buildPublishing(payload any) (amqp.Publishing, error) {
|
||||||
b, err := json.Marshal(payload)
|
b, err := json.Marshal(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -4,12 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
amqp "github.com/rabbitmq/amqp091-go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// SocialMQ 通过连接管理器发布关注事件(不再持有长生命周期 channel)。
|
||||||
type SocialMQ struct {
|
type SocialMQ struct {
|
||||||
ch *amqp.Channel
|
mgr *RabbitMQ
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -33,15 +32,8 @@ func NewSocialMQ(base *RabbitMQ) (*SocialMQ, error) {
|
|||||||
if base == nil {
|
if base == nil {
|
||||||
return nil, errors.New("rabbitmq base is nil")
|
return nil, errors.New("rabbitmq base is nil")
|
||||||
}
|
}
|
||||||
ch, err := base.NewChannel()
|
base.RegisterTopology(Topology{Exchange: socialExchange, Queue: socialQueue, BindingKey: socialBindingKey})
|
||||||
if err != nil {
|
return &SocialMQ{mgr: base}, nil
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := DeclareTopic(ch, socialExchange, socialQueue, socialBindingKey); err != nil {
|
|
||||||
ch.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &SocialMQ{ch: ch}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SocialMQ) Follow(ctx context.Context, followerID, vloggerID uint) error {
|
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 {
|
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")
|
return errors.New("social mq is not initialized")
|
||||||
}
|
}
|
||||||
if followerID == 0 || vloggerID == 0 {
|
if followerID == 0 || vloggerID == 0 {
|
||||||
@@ -70,5 +62,5 @@ func (s *SocialMQ) publish(ctx context.Context, action, routingKey string, follo
|
|||||||
VloggerID: vloggerID,
|
VloggerID: vloggerID,
|
||||||
OccurredAt: time.Now().UTC(),
|
OccurredAt: time.Now().UTC(),
|
||||||
}
|
}
|
||||||
return PublishJSON(ctx, s.ch, socialExchange, routingKey, evt)
|
return s.mgr.PublishJSON(ctx, socialExchange, routingKey, evt)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
amqp "github.com/rabbitmq/amqp091-go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TimelineMQ 通过连接管理器发布时间线事件(不再持有长生命周期 channel)。
|
||||||
type TimelineMQ struct {
|
type TimelineMQ struct {
|
||||||
ch *amqp.Channel
|
mgr *RabbitMQ
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -30,19 +29,12 @@ func NewTimelineMQ(base *RabbitMQ) (*TimelineMQ, error) {
|
|||||||
if base == nil {
|
if base == nil {
|
||||||
return nil, errors.New("rabbitmq base is nil")
|
return nil, errors.New("rabbitmq base is nil")
|
||||||
}
|
}
|
||||||
ch, err := base.NewChannel()
|
base.RegisterTopology(Topology{Exchange: timelineExchange, Queue: timelineQueue, BindingKey: timelineBindingKey})
|
||||||
if err != nil {
|
return &TimelineMQ{mgr: base}, nil
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := DeclareTopic(ch, timelineExchange, timelineQueue, timelineBindingKey); err != nil {
|
|
||||||
ch.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &TimelineMQ{ch: ch}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TimelineMQ) PublishVideo(ctx context.Context, videoID uint, createTime time.Time) error {
|
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")
|
return errors.New("timeline mq is not initialized")
|
||||||
}
|
}
|
||||||
if videoID == 0 {
|
if videoID == 0 {
|
||||||
@@ -58,5 +50,5 @@ func (t *TimelineMQ) PublishVideo(ctx context.Context, videoID uint, createTime
|
|||||||
CreateTime: createTime.UnixMilli(),
|
CreateTime: createTime.UnixMilli(),
|
||||||
OccurredAt: time.Now(),
|
OccurredAt: time.Now(),
|
||||||
}
|
}
|
||||||
return PublishJSON(ctx, t.ch, timelineExchange, timelinePublishRK, timeline)
|
return t.mgr.PublishJSON(ctx, timelineExchange, timelinePublishRK, timeline)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user