feat(prod): 实时就绪探针——gateway /readyz 真 ping + dispatcher/mcp-go 加 HTTP 探针(B2)

此前 gateway /readyz 用的是 Enabled() 启动期降级标志(反映不了运行中 PG 掉线,LB 会继续
往已不可用实例导流);dispatcher/mcp-go 干脆没有 HTTP 探针(只有 NATS ServeHealth,k8s/LB
够不着、只能靠 gateway 经 NATS 代探)。

- gateway /readyz 改用 db.Ping 实时探活。只把 **DB 当硬依赖门**:Redis 掉线仍可服务(限流有
  A5 进程内 fail-safe 兜底、SSE 回落 live NATS),一 blip 就把全部实例踢出轮转反而制造整站
  故障,故 Redis 只上报不 gate。NATS 启动即连(fatal)不单列。
- 新 sundynix-shared/health 包:Serve/Handler 提供 /healthz(恒 200 liveness) + /readyz(由
  ready() 决定 readiness)。dispatcher(:8091, DISPATCHER_HEALTH_ADDR)与 mcp-go(:8092,
  MCP_GO_HEALTH_ADDR)各起一个,readiness = NATS 连接可用(bus.IsConnected)。接入各自优雅停机。
- bus 加 IsConnected()(nc.IsConnected 实时);dispatcher Subscriber 透传。

四模块 build+vet+test 绿;health 带 Handler 单测。探针端口仅内部用(对外仍只暴露 gateway)。
compose/k8s 的 healthcheck 编排属 C 层 ops,另做。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-21 15:50:31 +08:00
parent 4500335da7
commit da9c76f073
7 changed files with 124 additions and 4 deletions
+30
View File
@@ -0,0 +1,30 @@
package health
import (
"net/http"
"net/http/httptest"
"testing"
)
// /healthz 恒 200/readyz 随 ready() 翻 200/503。
func TestHandler(t *testing.T) {
ready := false
h := Handler("test", func() bool { return ready })
do := func(path string) int {
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil))
return w.Code
}
if c := do("/healthz"); c != 200 {
t.Fatalf("/healthz 应恒 200,得 %d", c)
}
if c := do("/readyz"); c != 503 {
t.Fatalf("未就绪时 /readyz 应 503,得 %d", c)
}
ready = true
if c := do("/readyz"); c != 200 {
t.Fatalf("就绪后 /readyz 应 200,得 %d", c)
}
}