refactor(rabbitmq): 拓扑常量改为导出,worker/router 复用同一份定义
Some checks failed
CI/CD / Deploy (push) Failing after 5s
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:
@@ -22,24 +22,6 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
socialExchange = "social.events"
|
||||
socialQueue = "social.events"
|
||||
socialBindingKey = "social.*"
|
||||
|
||||
likeExchange = "like.events"
|
||||
likeQueue = "like.events"
|
||||
likeBindingKey = "like.*"
|
||||
|
||||
commentExchange = "comment.events"
|
||||
commentQueue = "comment.events"
|
||||
commentBindingKey = "comment.*"
|
||||
|
||||
popularityExchange = "video.popularity.events"
|
||||
popularityQueue = "video.popularity.events"
|
||||
popularityBindingKey = "video.popularity.*"
|
||||
)
|
||||
|
||||
// 带重试机制的基础设施连接函数
|
||||
func connectWithRetry(name string, maxRetries int, fn func() error) {
|
||||
for i := 0; i < maxRetries; i++ {
|
||||
@@ -126,14 +108,16 @@ func main() {
|
||||
|
||||
// 登记拓扑:每一代连接(含重连后的新连接)都会自动重放这些声明。
|
||||
// 这是"broker 侧拓扑丢失"也能恢复的关键,不能只在启动时声明一次。
|
||||
// 交换机名 / 队列名 / 绑定键一律取自 rabbitmq 包的导出常量——它们是跨进程的
|
||||
// 线缆契约,本地再定义一份就会漂移成"消费一个没人发布的队列"。
|
||||
rmqMgr.RegisterTopology(
|
||||
mqrabbit.Topology{Exchange: socialExchange, Queue: socialQueue, BindingKey: socialBindingKey},
|
||||
mqrabbit.Topology{Exchange: likeExchange, Queue: likeQueue, BindingKey: likeBindingKey},
|
||||
mqrabbit.Topology{Exchange: commentExchange, Queue: commentQueue, BindingKey: commentBindingKey},
|
||||
mqrabbit.Topology{Exchange: mqrabbit.SocialExchange, Queue: mqrabbit.SocialQueue, BindingKey: mqrabbit.SocialBindingKey},
|
||||
mqrabbit.Topology{Exchange: mqrabbit.LikeExchange, Queue: mqrabbit.LikeQueue, BindingKey: mqrabbit.LikeBindingKey},
|
||||
mqrabbit.Topology{Exchange: mqrabbit.CommentExchange, Queue: mqrabbit.CommentQueue, BindingKey: mqrabbit.CommentBindingKey},
|
||||
)
|
||||
if cache != nil {
|
||||
rmqMgr.RegisterTopology(
|
||||
mqrabbit.Topology{Exchange: popularityExchange, Queue: popularityQueue, BindingKey: popularityBindingKey},
|
||||
mqrabbit.Topology{Exchange: mqrabbit.PopularityExchange, Queue: mqrabbit.PopularityQueue, BindingKey: mqrabbit.PopularityBindingKey},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -160,17 +144,17 @@ func main() {
|
||||
|
||||
// 每个 Worker 独立 Channel;连接断开由管理器统一重拨 + 重放拓扑 + 重建 Channel
|
||||
go mqrabbit.RunConsumer(ctx, rmqMgr, "SocialWorker", 50, func(ch *amqp.Channel) error {
|
||||
return worker.NewSocialWorker(ch, socialRepo, socialQueue).Run(ctx)
|
||||
return worker.NewSocialWorker(ch, socialRepo, mqrabbit.SocialQueue).Run(ctx)
|
||||
})
|
||||
go mqrabbit.RunConsumer(ctx, rmqMgr, "LikeWorker", 50, func(ch *amqp.Channel) error {
|
||||
return worker.NewLikeWorker(ch, likeRepo, videoRepo, likeQueue).Run(ctx)
|
||||
return worker.NewLikeWorker(ch, likeRepo, videoRepo, mqrabbit.LikeQueue).Run(ctx)
|
||||
})
|
||||
go mqrabbit.RunConsumer(ctx, rmqMgr, "CommentWorker", 50, func(ch *amqp.Channel) error {
|
||||
return worker.NewCommentWorker(ch, commentRepo, videoRepo, commentQueue).Run(ctx)
|
||||
return worker.NewCommentWorker(ch, commentRepo, videoRepo, mqrabbit.CommentQueue).Run(ctx)
|
||||
})
|
||||
if cache != nil {
|
||||
go mqrabbit.RunConsumer(ctx, rmqMgr, "PopularityWorker", 50, func(ch *amqp.Channel) error {
|
||||
return worker.NewPopularityWorker(ch, cache, popularityQueue).Run(ctx)
|
||||
return worker.NewPopularityWorker(ch, cache, mqrabbit.PopularityQueue).Run(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -225,7 +225,7 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *g
|
||||
timelineMQ = nil
|
||||
}
|
||||
worker.StartOutboxPoller(db, timelineMQ)
|
||||
worker.StartConsumer(context.Background(), rmq, "video.timeline.update.queue", cache)
|
||||
worker.StartConsumer(context.Background(), rmq, rabbitmq.TimelineQueue, cache)
|
||||
|
||||
// SSE notification
|
||||
sseHub := worker.NewSSEHub(db)
|
||||
@@ -233,14 +233,16 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *g
|
||||
notifGroup.Use(sseHub.SSERequireAuth())
|
||||
sseHub.RegisterRoutes(r, notifGroup)
|
||||
|
||||
// 3 个 notification 消费者:拓扑登记后由管理器随每一代连接重放
|
||||
// 3 个 notification 消费者:拓扑登记后由管理器随每一代连接重放。
|
||||
// 交换机名、队列名、路由键全部复用 rabbitmq 包的导出常量——它们是跨进程的
|
||||
// 线缆契约,在这里再写一遍字面量就会与 Worker 侧悄悄漂移。
|
||||
if rmq != nil {
|
||||
rmq.RegisterTopology(
|
||||
rabbitmq.Topology{Exchange: "like.events", Queue: "notification.like", BindingKey: "like.like"},
|
||||
rabbitmq.Topology{Exchange: "comment.events", Queue: "notification.comment", BindingKey: "comment.publish"},
|
||||
rabbitmq.Topology{Exchange: "social.events", Queue: "notification.social", BindingKey: "social.follow"},
|
||||
rabbitmq.Topology{Exchange: rabbitmq.LikeExchange, Queue: rabbitmq.NotificationLikeQueue, BindingKey: rabbitmq.LikeLikeRK},
|
||||
rabbitmq.Topology{Exchange: rabbitmq.CommentExchange, Queue: rabbitmq.NotificationCommentQueue, BindingKey: rabbitmq.CommentPublishRK},
|
||||
rabbitmq.Topology{Exchange: rabbitmq.SocialExchange, Queue: rabbitmq.NotificationSocialQueue, BindingKey: rabbitmq.SocialFollowRK},
|
||||
)
|
||||
for _, q := range []string{"notification.like", "notification.comment", "notification.social"} {
|
||||
for _, q := range []string{rabbitmq.NotificationLikeQueue, rabbitmq.NotificationCommentQueue, rabbitmq.NotificationSocialQueue} {
|
||||
queue := q
|
||||
go rabbitmq.RunConsumer(context.Background(), rmq, "notification-"+queue, 10,
|
||||
func(ch *amqp.Channel) error {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
54
backend/internal/middleware/rabbitmq/constants_test.go
Normal file
54
backend/internal/middleware/rabbitmq/constants_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
20
backend/internal/middleware/rabbitmq/notification.go
Normal file
20
backend/internal/middleware/rabbitmq/notification.go
Normal 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"
|
||||
)
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ func (w *NotificationWorker) process(ctx context.Context, d amqp.Delivery) error
|
||||
var notif *Notification
|
||||
|
||||
switch {
|
||||
case routingKey == "like.like":
|
||||
case routingKey == rabbitmq.LikeLikeRK:
|
||||
var evt rabbitmq.LikeEvent
|
||||
if err := json.Unmarshal(body, &evt); err != nil {
|
||||
return nil
|
||||
@@ -120,7 +120,7 @@ func (w *NotificationWorker) process(ctx context.Context, d amqp.Delivery) error
|
||||
}
|
||||
notif = &Notification{RecipientID: authorID, SenderID: evt.UserID, Type: "like", TargetID: evt.VideoID, Content: "点赞了你的视频"}
|
||||
|
||||
case routingKey == "comment.publish":
|
||||
case routingKey == rabbitmq.CommentPublishRK:
|
||||
var evt rabbitmq.CommentEvent
|
||||
if err := json.Unmarshal(body, &evt); err != nil {
|
||||
return nil
|
||||
@@ -140,7 +140,7 @@ func (w *NotificationWorker) process(ctx context.Context, d amqp.Delivery) error
|
||||
}
|
||||
notif = &Notification{RecipientID: authorID, SenderID: evt.AuthorID, Type: "comment", TargetID: evt.VideoID, Content: "评论了你的视频"}
|
||||
|
||||
case routingKey == "social.follow":
|
||||
case routingKey == rabbitmq.SocialFollowRK:
|
||||
var evt rabbitmq.SocialEvent
|
||||
if err := json.Unmarshal(body, &evt); err != nil {
|
||||
return nil
|
||||
|
||||
@@ -34,7 +34,12 @@ func StartOutboxPoller(db *gorm.DB, tmq *rabbitmq.TimelineMQ) {
|
||||
|
||||
err := db.Where("status = ?", "pending").Order("create_time ASC").Limit(100).Find(&messages).Error
|
||||
|
||||
if err != nil || len(messages) == 0 {
|
||||
if err != nil {
|
||||
log.Printf("outbox 轮询失败: %v", err)
|
||||
time.Sleep(1 * time.Second)
|
||||
continue
|
||||
}
|
||||
if len(messages) == 0 {
|
||||
time.Sleep(1 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user