Documentation
¶
Overview ¶
Package httpx provides HTTP glue for Modulex health and readiness checks, plus a managed net/http.Server lifecycle, without pulling net/http into the core modulex package.
Modulex's core Registry abstracts health (liveness) and readiness checks as named functions (see modulex.HealthCheckRegistrar and modulex.ReadinessRegistrar), but deliberately stays free of HTTP dependencies. This package exists for consumers that expose those checks over HTTP:
- HealthHandler serves the aggregated result of every registered health (liveness) check. A failing check means the process should be restarted.
- ReadinessHandler serves the aggregated result of every registered readiness check. A failing check means the instance should be pulled from load balancing, not restarted.
- Serve spawns a *http.Server via a modulex.TaskSpawner and shuts it down gracefully when the supplied context is cancelled, removing the "ListenAndServe + select + Shutdown" boilerplate every HTTP-serving consumer would otherwise hand-write.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HealthHandler ¶
func HealthHandler(p modulex.HealthCheckProvider) http.HandlerFunc
HealthHandler returns an http.HandlerFunc that runs every health (liveness) check registered on p concurrently and reports the aggregate result as JSON.
It responds 200 with {"status":"ok","checks":{...}} if every check passes, or 503 with {"status":"unhealthy","checks":{...}} if any check fails. The checks map always lists every registered check, with a value of "ok" for passing checks and the check's error message for failing ones.
A failing health check means the process is broken and should be restarted; wire this handler to an orchestrator's liveness probe.
func ReadinessHandler ¶
func ReadinessHandler(p modulex.ReadinessProvider) http.HandlerFunc
ReadinessHandler returns an http.HandlerFunc that runs every readiness check registered on p concurrently and reports the aggregate result as JSON.
It responds 200 with {"status":"ready","checks":{...}} if every check passes, or 503 with {"status":"not-ready","checks":{...}} if any check fails. The checks map always lists every registered check, with a value of "ok" for passing checks and the check's error message for failing ones.
A failing readiness check means the instance should be pulled from load balancing, not restarted; wire this handler to an orchestrator's readiness probe.
func Serve ¶
func Serve(ctx context.Context, spawner modulex.TaskSpawner, name string, server *http.Server, shutdownTimeout time.Duration) (*modulex.TaskHandle, error)
Serve spawns server.ListenAndServe as a supervised background task via spawner.Go under the given name, and gracefully shuts the server down with shutdownTimeout when ctx is cancelled.
A modulex.TaskSpawner derives the context its task function runs with from the manager's own lifecycle, not from ctx, so Serve also watches the task's context: shutdown is triggered by whichever of ctx or the manager's shutdown happens first. This means Serve responds correctly both to an explicit caller cancellation and to modulex.Manager.StopModules.
http.ErrServerClosed is treated as a clean exit, not an error: both a natural server exit and a Shutdown-triggered exit surface as a nil error from the returned TaskHandle's Wait. Any other error from ListenAndServe, or a Shutdown that does not complete within shutdownTimeout, is returned by Wait.
Serve exists to remove the repeated "spawn ListenAndServe, select on ctx.Done, Shutdown with a timeout" boilerplate that HTTP-serving consumers of modulex would otherwise hand-write for every service.
Types ¶
This section is empty.