Provides liveness and database-aware readiness HTTP probes for service monitoring. Use it in Go microservices to manage traffic routing during outages.
// Source: https://github.com/Mic92/niks3 | License: MIT | Copyright 2025 Jörg Thalheim
// Provided via justclaw.it.com; keep this notice with the code.
package server
import (
"context"
"log/slog"
"net/http"
"time"
)
const readinessTimeout = 2 * time.Second
// HealthCheckHandler is the liveness probe and deliberately has no
// dependencies, so a Postgres outage does not restart every replica.
func (s *Service) HealthCheckHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("OK"))
if err != nil {
slog.Warn("Could not write health check response", "error", err)
}
}
// ReadinessHandler fails while Postgres is unreachable so load balancers stop
// routing to this instance.
func (s *Service) ReadinessHandler(w http.ResponseWrite
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key