Documentation
¶
Overview ¶
Package httpserver holds what every service repeats around net/http: a server with all four timeouts set, a run loop that shuts down gracefully, and the liveness and readiness handlers.
The router is the caller's: any http.Handler works, and this package depends on no router. It also decides no path and no response body. The caller mounts the health handlers where its contract says (new services: /healthz and /readyz) and passes the exact answers.
Index ¶
- Constants
- func Live(alive Response) http.Handler
- func New(opts Options) *http.Server
- func Ready(check Check, ready, notReady Response, onFailure func(error)) http.Handler
- func Run(ctx context.Context, server *http.Server, shutdownTimeout time.Duration) error
- func Serve(ctx context.Context, server *http.Server, listener net.Listener, ...) error
- type Check
- type Options
- type Response
Constants ¶
const ( DefaultReadHeaderTimeout = 5 * time.Second DefaultReadTimeout = 10 * time.Second DefaultWriteTimeout = 30 * time.Second DefaultIdleTimeout = 60 * time.Second // DefaultShutdownTimeout is how long Run and Serve wait for requests in flight. DefaultShutdownTimeout = 10 * time.Second )
Timeouts used when the matching Options field is zero. They suit an API that answers quickly; a service that streams or serves large bodies raises WriteTimeout.
Variables ¶
This section is empty.
Functions ¶
func Live ¶
Live returns the liveness handler: it always sends alive, because a process that can answer at all is alive. It panics when alive carries no HTTP status; that is a mistake in the wiring and shows at the first start.
func Ready ¶
Ready returns the readiness handler: ready when check returns nil, notReady otherwise. The error goes to onFailure (nil: dropped) and never into the response, so a probe from outside learns nothing about the inside. It panics on a nil check or a response without an HTTP status, as Live does.
func Run ¶
Run listens on server.Addr and serves until ctx ends, then shuts down as Serve does. An address that cannot be bound is reported at once, before anything is served.
func Serve ¶
func Serve(ctx context.Context, server *http.Server, listener net.Listener, shutdownTimeout time.Duration) error
Serve serves on listener until ctx ends, then stops accepting connections and waits up to shutdownTimeout (zero or less: DefaultShutdownTimeout) for requests in flight. It returns nil after a clean shutdown, and an error when serving failed or the wait ran out; in the latter case the remaining connections are closed. Serve closes the listener.
Types ¶
type Check ¶
Check reports whether the service can do its work right now, for example by pinging its database. It must respect ctx, which ends when the prober gives up.
type Options ¶
type Options struct {
// Addr is the listen address, such as ":8080".
Addr string
// Handler is the service's router.
Handler http.Handler
// A zero timeout means the default above; none can be switched off, because a server without
// them is open to slow-client attacks.
ReadHeaderTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
}
Options describe one HTTP server.
type Response ¶
type Response struct {
// Status is the HTTP status code.
Status int
// ContentType is sent when Body is not empty.
ContentType string
// Body is sent as is; empty for a 204.
Body string
}
Response is one fixed answer of a health endpoint. There is no default: every existing service answers differently (204, "ok", a JSON object) and probes and dashboards depend on it, so the caller writes the answer out.