feat(api): 接进程 ctx 并停止启动异步链路
API 只装配 HTTP 入口。SetRouter 增加 ctx,删除 Outbox 轮询、Timeline 消费者与三个通知消费者的启动,改调 StartNotificationSubscriber。 SSEHub 记录进程 ctx。ctx 结束时 SSEHandler 主动断开。Shutdown 只关空闲连接、不取消在途请求,而 SSE 响应永不结束。少了这条路径,每次停机都要耗满 10 秒超时。 cmd/main 改用 signal.NotifyContext 与 http.Server。ListenAndServe 放进 goroutine,主协程等信号或启动失败,再以 10 秒上界 Shutdown,超时才 Close 兜底。 router_test 适配新签名,并新增用例守住四条通知路由仍注册。
This commit is contained in:
@@ -17,11 +17,12 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *gin.Engine {
|
||||
// SetRouter 只装配 HTTP 入口。所有队列消费者与 Outbox 轮询都在 cmd/worker,
|
||||
// 这里的 ctx 是进程生命周期:通知订阅器与 SSE 连接都随它一起退出。
|
||||
func SetRouter(ctx context.Context, db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *gin.Engine {
|
||||
r := gin.Default()
|
||||
if err := r.SetTrustedProxies(nil); err != nil {
|
||||
log.Printf("SetTrustedProxies failed: %v", err)
|
||||
@@ -212,47 +213,20 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *g
|
||||
protectedMessageGroup.POST("/send", messageHandler.Send)
|
||||
protectedMessageGroup.POST("/list", messageHandler.List)
|
||||
}
|
||||
// worker:Outbox 轮询 + Timeline 消费者
|
||||
// 连接级重连 / 拓扑重放 / channel 重建统一由连接管理器(rabbitmq.RunConsumer)负责
|
||||
timelineMQ, err := rabbitmq.NewTimelineMQ(rmq)
|
||||
if err != nil {
|
||||
log.Printf("timelineMQ init failed (mq disabled): %v", err)
|
||||
timelineMQ = nil
|
||||
}
|
||||
worker.StartOutboxPoller(db, timelineMQ)
|
||||
worker.StartConsumer(context.Background(), rmq, rabbitmq.TimelineQueue, cache)
|
||||
|
||||
// SSE notification
|
||||
sseHub := worker.NewSSEHub(db)
|
||||
// SSE notification:本进程只负责把通知推到自己持有的连接上。
|
||||
// 通知在 Worker 进程里产生并落库,再经 Redis Pub/Sub 跨进程送达——
|
||||
// 进程内广播会让"用户连在实例 A、消费者在实例 B"变成永远收不到。
|
||||
sseHub := worker.NewSSEHub(ctx, db)
|
||||
notifGroup := r.Group("/notification")
|
||||
notifGroup.Use(sseHub.SSERequireAuth())
|
||||
sseHub.RegisterRoutes(r, notifGroup)
|
||||
|
||||
// 3 个 notification 消费者:拓扑登记后由管理器随每一代连接重放。
|
||||
// 交换机名、队列名、路由键全部复用 rabbitmq 包的导出常量——它们是跨进程的
|
||||
// 线缆契约,在这里再写一遍字面量就会与 Worker 侧悄悄漂移。
|
||||
if rmq != nil {
|
||||
rmq.RegisterTopology(
|
||||
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{rabbitmq.NotificationLikeQueue, rabbitmq.NotificationCommentQueue, rabbitmq.NotificationSocialQueue} {
|
||||
queue := q
|
||||
go rabbitmq.RunConsumer(context.Background(), rmq, "notification-"+queue, 10,
|
||||
func(ch *amqp.Channel) error {
|
||||
return worker.NewNotificationWorker(ch, rmq, db, queue, sseHub).Run(context.Background())
|
||||
})
|
||||
}
|
||||
} else {
|
||||
log.Printf("Notification SSE disabled (MQ not available)")
|
||||
}
|
||||
worker.StartNotificationSubscriber(ctx, cache, sseHub)
|
||||
|
||||
// 所有 publisher / consumer 都已登记拓扑:在【当前】连接上立即声明一遍。
|
||||
// 重连时管理器会自动重放,因此这里只需处理"首次连接早于拓扑登记"的启动顺序。
|
||||
// 用带超时的 ctx,避免连接恰在此刻断开时把 API 启动挂住。
|
||||
if rmq != nil {
|
||||
initCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
initCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
if err := rmq.EnsureTopology(initCtx); err != nil {
|
||||
log.Printf("rabbitmq topology init failed (will be redeclared on reconnect): %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user