refactor(rabbitmq): 拓扑常量改为导出,worker/router 复用同一份定义
Some checks failed
CI/CD / Deploy (push) Failing after 5s

原先交换机名 / 队列名 / 绑定键存在三份副本:rabbitmq 包内的未导出常量、cmd/worker/main.go 的本地常量块、internal/http/router.go 的字符串字面量。任一处改名都会让其余两处静默失联——消费一个没人发布的队列,或发布到一个没人监听的交换机,编译与运行都不报错。

- 5 个 MQ 文件的拓扑常量改为导出(仅首字母大写,字符串值一字未改)
- 新增 notification.go,补上 3 个此前只以字面量存在的通知队列常量
- cmd/worker/main.go 删除 12 个本地常量,改用 mqrabbit.*
- router.go 的 13 处字面量改用 rabbitmq.*
- notificationworker.go 的路由键比较改用常量,与发布端同源
- 新增 constants_test.go 锁定 26 个 wire 名称,改名必须配套迁移

附带:outbox 轮询失败现在单独打日志,不再与没有待发消息共用一条静默分支;rabbitMQ.go 合并一处注释换行。
This commit is contained in:
2026-10-28 21:54:35 +08:00
parent 9b8a116852
commit 5f30f264f7
12 changed files with 149 additions and 77 deletions

View File

@@ -11,13 +11,14 @@ type CommentMQ struct {
mgr *RabbitMQ
}
// 拓扑常量导出:见 likeMQ.go 中的说明。
const (
commentExchange = "comment.events"
commentQueue = "comment.events"
commentBindingKey = "comment.*"
CommentExchange = "comment.events"
CommentQueue = "comment.events"
CommentBindingKey = "comment.*"
commentPublishRK = "comment.publish"
commentDeleteRK = "comment.delete"
CommentPublishRK = "comment.publish"
CommentDeleteRK = "comment.delete"
)
type CommentEvent struct {
@@ -35,12 +36,12 @@ func NewCommentMQ(base *RabbitMQ) (*CommentMQ, error) {
if base == nil {
return nil, errors.New("rabbitmq base is nil")
}
base.RegisterTopology(Topology{Exchange: commentExchange, Queue: commentQueue, BindingKey: commentBindingKey})
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 {
return c.publish(ctx, "publish", commentPublishRK, CommentEvent{
return c.publish(ctx, "publish", CommentPublishRK, CommentEvent{
Username: username,
VideoID: videoID,
AuthorID: authorID,
@@ -49,7 +50,7 @@ func (c *CommentMQ) Publish(ctx context.Context, username string, videoID, autho
}
func (c *CommentMQ) Delete(ctx context.Context, commentID uint) error {
return c.publish(ctx, "delete", commentDeleteRK, CommentEvent{
return c.publish(ctx, "delete", CommentDeleteRK, CommentEvent{
CommentID: commentID,
})
}
@@ -65,5 +66,5 @@ func (c *CommentMQ) publish(ctx context.Context, action, routingKey string, evt
evt.EventID = id
evt.Action = action
evt.OccurredAt = time.Now().UTC()
return c.mgr.PublishJSON(ctx, commentExchange, routingKey, evt)
return c.mgr.PublishJSON(ctx, CommentExchange, routingKey, evt)
}

View File

@@ -0,0 +1,54 @@
package rabbitmq
import "testing"
// TestTopologyConstants_WireNamesAreStable 锁定 broker 侧的"线缆名称"。
//
// 这些字符串是运行中集群里的持久实体名:改名等于新开一套交换机/队列,
// 旧队列里的消息与消费者会静默失联。导出给 cmd/worker 与 http 层复用之后,
// 重命名变得更容易,所以用一条测试把值钉住——改名必须是一次有意的、
// 带迁移的动作,而不是顺手"整理命名"。
func TestTopologyConstants_WireNamesAreStable(t *testing.T) {
cases := []struct {
name string
got string
want string
}{
{"LikeExchange", LikeExchange, "like.events"},
{"LikeQueue", LikeQueue, "like.events"},
{"LikeBindingKey", LikeBindingKey, "like.*"},
{"LikeLikeRK", LikeLikeRK, "like.like"},
{"LikeUnlikeRK", LikeUnlikeRK, "like.unlike"},
{"CommentExchange", CommentExchange, "comment.events"},
{"CommentQueue", CommentQueue, "comment.events"},
{"CommentBindingKey", CommentBindingKey, "comment.*"},
{"CommentPublishRK", CommentPublishRK, "comment.publish"},
{"CommentDeleteRK", CommentDeleteRK, "comment.delete"},
{"SocialExchange", SocialExchange, "social.events"},
{"SocialQueue", SocialQueue, "social.events"},
{"SocialBindingKey", SocialBindingKey, "social.*"},
{"SocialFollowRK", SocialFollowRK, "social.follow"},
{"SocialUnfollowRK", SocialUnfollowRK, "social.unfollow"},
{"PopularityExchange", PopularityExchange, "video.popularity.events"},
{"PopularityQueue", PopularityQueue, "video.popularity.events"},
{"PopularityBindingKey", PopularityBindingKey, "video.popularity.*"},
{"PopularityUpdateRK", PopularityUpdateRK, "video.popularity.update"},
{"TimelineExchange", TimelineExchange, "video.timeline.events"},
{"TimelineQueue", TimelineQueue, "video.timeline.update.queue"},
{"TimelineBindingKey", TimelineBindingKey, "video.timeline.*"},
{"TimelinePublishRK", TimelinePublishRK, "video.timeline.publish"},
{"NotificationLikeQueue", NotificationLikeQueue, "notification.like"},
{"NotificationCommentQueue", NotificationCommentQueue, "notification.comment"},
{"NotificationSocialQueue", NotificationSocialQueue, "notification.social"},
}
for _, c := range cases {
if c.got != c.want {
t.Errorf("%s = %q, want %q(broker 线缆契约被改动,需配套迁移)", c.name, c.got, c.want)
}
}
}

View File

@@ -12,13 +12,15 @@ type LikeMQ struct {
mgr *RabbitMQ
}
// 拓扑常量导出:交换机名 / 队列名 / 绑定键是跨进程的"线缆契约",
// cmd/worker 与 http 层登记拓扑时必须复用同一份定义,不能再各自写字符串字面量。
const (
likeExchange = "like.events"
likeQueue = "like.events"
likeBindingKey = "like.*"
LikeExchange = "like.events"
LikeQueue = "like.events"
LikeBindingKey = "like.*"
likeLikeRK = "like.like" // 点赞路由键
likeUnlikeRK = "like.unlike" // 取消点赞路由键
LikeLikeRK = "like.like" // 点赞路由键
LikeUnlikeRK = "like.unlike" // 取消点赞路由键
)
type LikeEvent struct {
@@ -34,16 +36,16 @@ func NewLikeMQ(base *RabbitMQ) (*LikeMQ, error) {
return nil, errors.New("rabbitmq base is nil")
}
// 拓扑交给管理器登记:每一代连接都会自动重放,不再只声明一次
base.RegisterTopology(Topology{Exchange: likeExchange, Queue: likeQueue, BindingKey: likeBindingKey})
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 {
return l.publish(ctx, "like", likeLikeRK, userID, videoID)
return l.publish(ctx, "like", LikeLikeRK, userID, videoID)
}
func (l *LikeMQ) Unlike(ctx context.Context, userID, videoID uint) error {
return l.publish(ctx, "unlike", likeUnlikeRK, userID, videoID)
return l.publish(ctx, "unlike", LikeUnlikeRK, userID, videoID)
}
func (l *LikeMQ) publish(ctx context.Context, action, routingKey string, userID, videoID uint) error {
@@ -64,5 +66,5 @@ func (l *LikeMQ) publish(ctx context.Context, action, routingKey string, userID,
VideoID: videoID,
OccurredAt: time.Now(),
}
return l.mgr.PublishJSON(ctx, likeExchange, routingKey, event)
return l.mgr.PublishJSON(ctx, LikeExchange, routingKey, event)
}

View File

@@ -0,0 +1,20 @@
package rabbitmq
// 通知队列复用 like / comment / social 三个 exchange,但用单一路由键只订阅
// "需要提醒用户"的那一个动作:
//
// like.events --like.like--> notification.like
// comment.events --comment.publish--> notification.comment
// social.events --social.follow--> notification.social
//
// 与主队列的区别:主队列用通配键(like.* 等)收全部动作,因为 DB 需要同步
// 点赞与取消点赞两个方向;通知只关心"发生过",取关 / 删评论不需要推送。
//
// 这三个队列原先以字符串字面量写在 internal/http/router.go 里,导致
// rabbitmq 包与 http 包各存一份"线缆契约"。改为导出常量后,登记拓扑
// 与启动消费者共用同一份定义。
const (
NotificationLikeQueue = "notification.like"
NotificationCommentQueue = "notification.comment"
NotificationSocialQueue = "notification.social"
)

View File

@@ -11,12 +11,13 @@ type PopularityMQ struct {
mgr *RabbitMQ
}
// 拓扑常量导出:见 likeMQ.go 中的说明。
const (
popularityExchange = "video.popularity.events"
popularityQueue = "video.popularity.events"
popularityBindingKey = "video.popularity.*"
PopularityExchange = "video.popularity.events"
PopularityQueue = "video.popularity.events"
PopularityBindingKey = "video.popularity.*"
popularityUpdateRK = "video.popularity.update"
PopularityUpdateRK = "video.popularity.update"
)
type PopularityEvent struct {
@@ -30,7 +31,7 @@ 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})
base.RegisterTopology(Topology{Exchange: PopularityExchange, Queue: PopularityQueue, BindingKey: PopularityBindingKey})
return &PopularityMQ{mgr: base}, nil
}
@@ -51,5 +52,5 @@ func (p *PopularityMQ) Update(ctx context.Context, videoID uint, change int64) e
Change: change,
OccurredAt: time.Now().UTC(),
}
return p.mgr.PublishJSON(ctx, popularityExchange, popularityUpdateRK, event)
return p.mgr.PublishJSON(ctx, PopularityExchange, PopularityUpdateRK, event)
}

View File

@@ -218,8 +218,7 @@ func (r *RabbitMQ) supervise(conn connection, notifyClose <-chan *amqp.Error) {
newNotify := newConn.NotifyClose(make(chan *amqp.Error, 1))
// 顺序不可换:拨号 → 注册关闭通知 → 重放拓扑 → 才宣告就绪。
// 若拓扑重放失败,这一代不对外发布,避免消费者拿到一个
// "连接健康但队列不存在"的伪可用连接。
// 若拓扑重放失败,这一代不对外发布,避免消费者拿到一个"连接健康但队列不存在"的伪可用连接。
if err := r.declareAll(newConn); err != nil {
log.Printf("rabbitmq: 重放拓扑失败: %v(%v 后重试)", err, backoff)
_ = newConn.Close()

View File

@@ -11,13 +11,14 @@ type SocialMQ struct {
mgr *RabbitMQ
}
// 拓扑常量导出:见 likeMQ.go 中的说明。
const (
socialExchange = "social.events"
socialQueue = "social.events"
socialBindingKey = "social.*"
SocialExchange = "social.events"
SocialQueue = "social.events"
SocialBindingKey = "social.*"
socialFollowRK = "social.follow"
socialUnfollowRK = "social.unfollow"
SocialFollowRK = "social.follow"
SocialUnfollowRK = "social.unfollow"
)
type SocialEvent struct {
@@ -32,16 +33,16 @@ func NewSocialMQ(base *RabbitMQ) (*SocialMQ, error) {
if base == nil {
return nil, errors.New("rabbitmq base is nil")
}
base.RegisterTopology(Topology{Exchange: socialExchange, Queue: socialQueue, BindingKey: socialBindingKey})
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 {
return s.publish(ctx, "follow", socialFollowRK, followerID, vloggerID)
return s.publish(ctx, "follow", SocialFollowRK, followerID, vloggerID)
}
func (s *SocialMQ) UnFollow(ctx context.Context, followerID, vloggerID uint) error {
return s.publish(ctx, "unfollow", socialUnfollowRK, followerID, vloggerID)
return s.publish(ctx, "unfollow", SocialUnfollowRK, followerID, vloggerID)
}
func (s *SocialMQ) publish(ctx context.Context, action, routingKey string, followerID, vloggerID uint) error {
@@ -62,5 +63,5 @@ func (s *SocialMQ) publish(ctx context.Context, action, routingKey string, follo
VloggerID: vloggerID,
OccurredAt: time.Now().UTC(),
}
return s.mgr.PublishJSON(ctx, socialExchange, routingKey, evt)
return s.mgr.PublishJSON(ctx, SocialExchange, routingKey, evt)
}

View File

@@ -11,11 +11,14 @@ type TimelineMQ struct {
mgr *RabbitMQ
}
// 拓扑常量导出:见 likeMQ.go 中的说明。
// 注意 TimelineQueue 与 TimelineExchange 刻意不同名:时间线交换机将来可能
// 挂多个队列(全局时间线、关注时间线…),队列名需要自带语义。
const (
timelineExchange = "video.timeline.events"
timelineQueue = "video.timeline.update.queue"
timelineBindingKey = "video.timeline.*"
timelinePublishRK = "video.timeline.publish"
TimelineExchange = "video.timeline.events"
TimelineQueue = "video.timeline.update.queue"
TimelineBindingKey = "video.timeline.*"
TimelinePublishRK = "video.timeline.publish"
)
type TimelineEvent struct {
@@ -29,7 +32,7 @@ func NewTimelineMQ(base *RabbitMQ) (*TimelineMQ, error) {
if base == nil {
return nil, errors.New("rabbitmq base is nil")
}
base.RegisterTopology(Topology{Exchange: timelineExchange, Queue: timelineQueue, BindingKey: timelineBindingKey})
base.RegisterTopology(Topology{Exchange: TimelineExchange, Queue: TimelineQueue, BindingKey: TimelineBindingKey})
return &TimelineMQ{mgr: base}, nil
}
@@ -50,5 +53,5 @@ func (t *TimelineMQ) PublishVideo(ctx context.Context, videoID uint, createTime
CreateTime: createTime.UnixMilli(),
OccurredAt: time.Now(),
}
return t.mgr.PublishJSON(ctx, timelineExchange, timelinePublishRK, timeline)
return t.mgr.PublishJSON(ctx, TimelineExchange, TimelinePublishRK, timeline)
}