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:
@@ -1,6 +1,7 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -27,7 +28,29 @@ func doGet(t *testing.T, r *gin.Engine, path string) (int, map[string]any) {
|
||||
func newRouterForTest(t *testing.T, rmq *mqrabbit.RabbitMQ) *gin.Engine {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
return SetRouter(nil, nil, rmq)
|
||||
return SetRouter(context.Background(), nil, nil, rmq)
|
||||
}
|
||||
|
||||
// 通知路由属于 API 进程:消费者搬到 Worker 之后,SSE 与列表/已读接口必须还在。
|
||||
// 这条回归挡的是"搬家时顺手把整个 notification 域一起删掉"。
|
||||
func TestRouter_NotificationRoutesRegistered(t *testing.T) {
|
||||
r := newRouterForTest(t, nil)
|
||||
|
||||
want := []string{
|
||||
"GET /notification/stream",
|
||||
"POST /notification/list",
|
||||
"POST /notification/markRead",
|
||||
"POST /notification/unreadCount",
|
||||
}
|
||||
registered := make(map[string]bool, len(want))
|
||||
for _, route := range r.Routes() {
|
||||
registered[route.Method+" "+route.Path] = true
|
||||
}
|
||||
for _, route := range want {
|
||||
if !registered[route] {
|
||||
t.Errorf("route %q is not registered", route)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 这是 /readyz 存在的全部意义所在,也是最容易被后人"顺手合并"掉的不变式:
|
||||
|
||||
Reference in New Issue
Block a user