graceful

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2021 License: MIT Imports: 10 Imported by: 2

README

Graceful

License GoDev Reference Go Report Card

Package graceful provides graceful shutdown for servers.

Example

Dual HTTP Server
// DualServerConfig specifies a server with functions split between
// internal and external use cases and graceful shutdown parameters.
srv := graceful.DualServerConfig{
	// ExternalServer is the server for the primary clients.
    ExternalServer: graceful.HTTPServer(&http.Server{
        Handler: extMux,
    }),
	// InternalServer is the server for health checks, metrics, debugging,
	// profiling, etc. It shuts down after the ExternalServer exits.
    InternalServer: graceful.HTTPServer(&http.Server{
        Handler: intMux,
    }),
	// 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: 10 * time.Second,
	// 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: 30 * time.Second,
	// Logger optionally specifies a logger which will be used to output info
    // and errors.
    Logger: log,
}
if err := srv.ListenAndServe(ctx, *intAddr, *extAddr); err != nil {
    log.Error(err, "Serving failed")
}

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.

func (*DualServerConfig) Serve

func (cfg *DualServerConfig) Serve(ctx context.Context, intLis, extLis net.Listener) error

Serve serves with the given listeners. It waits for shutdown signals with the given context and calls GracefulShutdown as configured. If the context is cancelled a hard shutdown is initiated.

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

func HTTPServer(srv *http.Server) Server

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.

func (*ServerConfig) Serve

func (cfg *ServerConfig) Serve(ctx context.Context, lis net.Listener) error

Serve serves with the given listener. It waits for shutdown signals with the given context and calls GracefulShutdown as configured. If the context is cancelled a hard shutdown is initiated.

Jump to

Keyboard shortcuts

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