Documentation
¶
Overview ¶
Package shutdown provides graceful HTTP server shutdown with readiness tracking.
Index ¶
- func Listen(addr string) (net.Listener, error)
- type State
- func (s *State) HealthzHandler(w http.ResponseWriter, _ *http.Request)
- func (s *State) IsReady() bool
- func (s *State) MarkShuttingDown()
- func (s *State) ReadyzHandler(w http.ResponseWriter, _ *http.Request)
- func (s *State) Run(ctx context.Context, srv *http.Server, drainDelay time.Duration, ...) error
- func (s *State) Serve(ctx context.Context, srv *http.Server, ln net.Listener, ...) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type State ¶
type State struct {
// contains filtered or unexported fields
}
State tracks server readiness and orchestrates the shutdown sequence.
func (*State) HealthzHandler ¶
func (s *State) HealthzHandler(w http.ResponseWriter, _ *http.Request)
HealthzHandler is a liveness probe that never touches PostgreSQL.
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.