shutdown

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package shutdown provides graceful HTTP server shutdown with readiness tracking.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Listen

func Listen(addr string) (net.Listener, error)

Listen binds addr (":8080", "127.0.0.1:9090") and returns the live listener. A failed bind returns the error UNANNOUNCED — nothing may claim to serve yet.

Types

type State

type State struct {
	// contains filtered or unexported fields
}

State tracks server readiness and orchestrates the shutdown sequence.

func New

func New() *State

New returns a State in the ready=1 (serving) condition.

func (*State) HealthzHandler

func (s *State) HealthzHandler(w http.ResponseWriter, _ *http.Request)

HealthzHandler is a liveness probe that never touches PostgreSQL.

func (*State) IsReady

func (s *State) IsReady() bool

IsReady reports whether the server is currently ready to accept requests.

func (*State) MarkShuttingDown

func (s *State) MarkShuttingDown()

MarkShuttingDown sets ready=0; /readyz will return 503 from this point on.

func (*State) ReadyzHandler

func (s *State) ReadyzHandler(w http.ResponseWriter, _ *http.Request)

ReadyzHandler is a readiness probe. Returns 503 during and after shutdown.

func (*State) Run

func (s *State) Run(ctx context.Context, srv *http.Server, drainDelay time.Duration, onClose ...func()) error

Run binds srv.Addr and serves on it, blocking until ctx is cancelled, then executes the shutdown sequence in the mandated order:

ready=0  →  sleep drainDelay  →  srv.Shutdown(10s)  →  each onClose()

drainDelay should be ~5 s in production (LB drain time) and 0 in tests.

Callers that want to ANNOUNCE the server ("serving on :PORT") must bind first with Listen and pass the live listener to Serve — printing before the bind is ENG-34: the old single call forced the announcement before ListenAndServe ever ran, so a process could declare itself serving and then die on `bind: address already in use` while a draining predecessor still answered on the port (a client then talks to the STALE binary believing it is the new one — measured costing a fresh agent 10 minutes).

func (*State) Serve

func (s *State) Serve(ctx context.Context, srv *http.Server, ln net.Listener, drainDelay time.Duration, onClose ...func()) error

Serve runs srv on an ALREADY-BOUND listener and blocks until ctx is cancelled, then executes the same shutdown sequence as Run. The split exists so the boot can bind → announce → serve, in that order: once Listen has returned, the port is truly held and the kernel queues connections, so a "serving on" line printed between Listen and Serve is a statement of fact.

Jump to

Keyboard shortcuts

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