Server.Shutdown
The http.Server method that stops accepting new connections and waits for active requests to finish, up to the deadline of the context you pass.
In more detail
Call it after receiving SIGTERM, with a context from context.WithTimeout that fits inside the orchestrator's grace period. It returns the context's error if the deadline passes first, which you should report. After it returns, ListenAndServe returns http.ErrServerClosed.
Example
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := server.Shutdown(ctx)Related terms
- graceful shutdown — Stopping a service in an orderly way: finish current work, close connections, then exit.
- SIGTERM — The signal that politely asks a process to finish its work and exit.
- context deadline — A time limit attached to a context, after which it is cancelled automatically and ctx.Err() reports context.DeadlineExceeded.
- http.Server — The Go type that listens on a network address and serves HTTP requests through a handler, with fields for limits such as timeouts.
- terminationGracePeriodSeconds — The pod setting for how long Kubernetes waits after sending SIGTERM before killing the container with SIGKILL, 30 seconds by default.
Taught in
- Graceful Shutdown on SIGTERM — Concurrent Systems Programming with Go