P1-9: readiness-проба /ready отражает состояние bridge/consumer
/health остаётся liveness (процесс жив); /ready возвращает 503, если bridge или consumer не в строю (горутина упала/не поднялась). Добавлен api.Readiness с atomic-флагами, NewRouter принимает *Readiness, main поднимает/опускает флаги вокруг фоновых горутин.
This commit is contained in:
@@ -26,13 +26,16 @@ func corsMiddleware(next http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
// NewRouter собирает все маршруты сервиса.
|
||||
func NewRouter(h *handler.Handler, log *slog.Logger, authTestMode bool, jwtHMACSecret string, version string) http.Handler {
|
||||
func NewRouter(h *handler.Handler, log *slog.Logger, authTestMode bool, jwtHMACSecret string, version string, ready *Readiness) http.Handler {
|
||||
r := mux.NewRouter()
|
||||
|
||||
// Health — для платформенных проверок контейнера.
|
||||
// Health — для платформенных проверок контейнера (liveness).
|
||||
r.HandleFunc("/health", healthHandler(version)).Methods(http.MethodGet)
|
||||
r.HandleFunc("/healthz", healthHandler(version)).Methods(http.MethodGet)
|
||||
|
||||
// Ready — readiness: 503, если фоновые подсистемы (bridge/consumer) не в строю.
|
||||
r.HandleFunc("/ready", readyHandler(ready)).Methods(http.MethodGet)
|
||||
|
||||
// Лендинг "/" — инструкция (дизайн Nubes, как у shared-sqs) + статика.
|
||||
r.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
||||
ServeLanding(w, req, version)
|
||||
@@ -70,3 +73,18 @@ func healthHandler(version string) http.HandlerFunc {
|
||||
_, _ = w.Write([]byte(`{"status":"ok","version":"` + version + `"}`))
|
||||
}
|
||||
}
|
||||
|
||||
// readyHandler отдаёт 200 если все подсистемы в строю, иначе 503.
|
||||
// Отделяет readiness от liveness: процесс может быть жив, но pipeline стоять.
|
||||
func readyHandler(ready *Readiness) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if ready != nil && !ready.Ready() {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
_, _ = w.Write([]byte(`{"status":"not ready"}`))
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{"status":"ready"}`))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user