Documentation ¶
Overview ¶
Package graceful provides graceful shutdown for servers.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contexts ¶
func Contexts(ctx context.Context, log logr.Logger, delay, grace time.Duration) (context.Context, context.Context)
Contexts returns two contexts which respectively serve as soft and hard shutdown signals. They are cancelled after TERM or INT signals are received.
If delay is positive, it will wait that duration after receiving a signal before cancelling the first context. This is useful to allow loadbalancer updates before the server stops accepting new requests.
If grace is positive, it will wait that duration after cancelling the first context before cancelling the second context. This is useful to set a maximum time to allow pending requests to complete.
Repeated TERM or INT signals will bypass any delay or grace time.
Types ¶
type DualServerConfig ¶
type DualServerConfig struct { // ExternalServer is the server for the primary clients. ExternalServer Server // InternalServer is the server for health checks, metrics, debugging, // profiling, etc. It shuts down after the ExternalServer exits. InternalServer Server // ShutdownDelay gives time for load balancers to remove the server from // their backend pools before it stops listening. It's inserted after a // shutdown signal is received and before GracefulShutdown is called on // the servers. ShutdownDelay time.Duration // ShutdownGrace gives time for pending requests complete before the server // must forcibly shut down. It's the timeout on the context passed to // GracefulShutdown. ShutdownGrace time.Duration // Logger optionally specifies a logger which will be used to output info // and errors. Logger logr.Logger // contains filtered or unexported fields }
DualServerConfig specifies a server with functions split between internal and external use cases and graceful shutdown parameters.
func (*DualServerConfig) ListenAndServe ¶
func (cfg *DualServerConfig) ListenAndServe(ctx context.Context, intAddr, extAddr string) error
ListenAndServe listens on the given addresses and calls Serve.
type Server ¶
type Server interface { // Serve serves connections accepted from the listener. It does not return // an error if the listener is closed by the GracefulShutown method. Serve(net.Listener) error // GracefulShutdown immediately closes the server's listener and, if possible, // signals to clients that it is going away. It waits for pending requests to // finish or until the context is closed. If all pending requests finish, no // error is returned. GracefulShutdown(context.Context) error }
A Server is a networked server.
func HTTPServer ¶
HTTPServer converts an http.Server into a graceful.Server.
type ServerConfig ¶
type ServerConfig struct { // Server is the networked server. Server Server // ShutdownDelay gives time for load balancers to remove the server from // their backend pools before it stops listening. It's inserted after a // shutdown signal is received and before GracefulShutdown is called on // the servers. ShutdownDelay time.Duration // ShutdownGrace gives time for pending requests complete before the server // must forcibly shut down. It's the timeout on the context passed to // GracefulShutdown. ShutdownGrace time.Duration // Logger optionally specifies a logger which will be used to output info // and errors. Logger logr.Logger // contains filtered or unexported fields }
ServerConfig specifies a server with graceful shutdown parameters.
func (*ServerConfig) ListenAndServe ¶
func (cfg *ServerConfig) ListenAndServe(ctx context.Context, addr string) error
ListenAndServe listens on the given address and calls Serve.