httpserver

package
v0.0.0-...-50d06ef Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func Live(alive Response) http.Handler

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 New

func New(opts Options) *http.Server

New returns a server with every timeout set.

func Ready

func Ready(check Check, ready, notReady Response, onFailure func(error)) http.Handler

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

func Run(ctx context.Context, server *http.Server, shutdownTimeout time.Duration) error

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

type Check func(ctx context.Context) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL