graceful – github.com/tylerb/graceful Index | Files | Directories

package graceful

import "github.com/tylerb/graceful"

Index

Variables

var ErrNotTCP = errors.New("only tcp connections have keepalive")

ErrNotTCP indicates that network connection is not a TCP connection.

Functions

func DefaultLogger

func DefaultLogger() *log.Logger

DefaultLogger returns the logger used by Run, RunWithErr, ListenAndServe, ListenAndServeTLS and Serve. The logger outputs to STDERR by default.

func LimitListener

func LimitListener(l net.Listener, n int) net.Listener

LimitListener returns a Listener that accepts at most n simultaneous connections from the provided Listener.

func ListenAndServe

func ListenAndServe(server *http.Server, timeout time.Duration) error

ListenAndServe is equivalent to http.Server.ListenAndServe with graceful shutdown enabled.

timeout is the duration to wait until killing active requests and stopping the server. If timeout is 0, the server never times out. It waits for all active requests to finish.

func ListenAndServeTLS

func ListenAndServeTLS(server *http.Server, certFile, keyFile string, timeout time.Duration) error

ListenAndServeTLS is equivalent to http.Server.ListenAndServeTLS with graceful shutdown enabled.

timeout is the duration to wait until killing active requests and stopping the server. If timeout is 0, the server never times out. It waits for all active requests to finish.

func Run

func Run(addr string, timeout time.Duration, n http.Handler)

Run serves the http.Handler with graceful shutdown enabled.

timeout is the duration to wait until killing active requests and stopping the server. If timeout is 0, the server never times out. It waits for all active requests to finish.

func RunWithErr

func RunWithErr(addr string, timeout time.Duration, n http.Handler) error

RunWithErr is an alternative version of Run function which can return error.

Unlike Run this version will not exit the program if an error is encountered but will return it instead.

func Serve

func Serve(server *http.Server, l net.Listener, timeout time.Duration) error

Serve is equivalent to http.Server.Serve with graceful shutdown enabled.

timeout is the duration to wait until killing active requests and stopping the server. If timeout is 0, the server never times out. It waits for all active requests to finish.

func TLSConfigHasHTTP2Enabled

func TLSConfigHasHTTP2Enabled(t *tls.Config) bool

TLSConfigHasHTTP2Enabled checks to see if a given TLS Config has http2 enabled.

Types

type Server

type Server struct {
	*http.Server

	// Timeout is the duration to allow outstanding requests to survive
	// before forcefully terminating them.
	Timeout time.Duration

	// Limit the number of outstanding requests
	ListenLimit int

	// TCPKeepAlive sets the TCP keep-alive timeouts on accepted
	// connections. It prunes dead TCP connections ( e.g. closing
	// laptop mid-download)
	TCPKeepAlive time.Duration

	// ConnState specifies an optional callback function that is
	// called when a client connection changes state. This is a proxy
	// to the underlying http.Server's ConnState, and the original
	// must not be set directly.
	ConnState func(net.Conn, http.ConnState)

	// BeforeShutdown is an optional callback function that is called
	// before the listener is closed. Returns true if shutdown is allowed
	BeforeShutdown func() bool

	// ShutdownInitiated is an optional callback function that is called
	// when shutdown is initiated. It can be used to notify the client
	// side of long lived connections (e.g. websockets) to reconnect.
	ShutdownInitiated func()

	// NoSignalHandling prevents graceful from automatically shutting down
	// on SIGINT and SIGTERM. If set to true, you must shut down the server
	// manually with Stop().
	NoSignalHandling bool

	// Logger used to notify of errors on startup and on stop.
	Logger *log.Logger

	// LogFunc can be assigned with a logging function of your choice, allowing
	// you to use whatever logging approach you would like
	LogFunc func(format string, args ...interface{})

	// Interrupted is true if the server is handling a SIGINT or SIGTERM
	// signal and is thus shutting down.
	Interrupted bool
	// contains filtered or unexported fields
}

Server wraps an http.Server with graceful connection handling. It may be used directly in the same way as http.Server, or may be constructed with the global functions in this package.

Example:

srv := &graceful.Server{
	Timeout: 5 * time.Second,
	Server: &http.Server{Addr: ":1234", Handler: handler},
}
srv.ListenAndServe()

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe() error

ListenAndServe is equivalent to http.Server.ListenAndServe with graceful shutdown enabled.

func (*Server) ListenAndServeTLS

func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error

ListenAndServeTLS is equivalent to http.Server.ListenAndServeTLS with graceful shutdown enabled.

func (*Server) ListenAndServeTLSConfig

func (srv *Server) ListenAndServeTLSConfig(config *tls.Config) error

ListenAndServeTLSConfig can be used with an existing TLS config and is equivalent to http.Server.ListenAndServeTLS with graceful shutdown enabled,

func (*Server) ListenTLS

func (srv *Server) ListenTLS(certFile, keyFile string) (net.Listener, error)

ListenTLS is a convenience method that creates an https listener using the provided cert and key files. Use this method if you need access to the listener object directly. When ready, pass it to the Serve method.

func (*Server) Serve

func (srv *Server) Serve(listener net.Listener) error

Serve is equivalent to http.Server.Serve with graceful shutdown enabled.

func (*Server) Stop

func (srv *Server) Stop(timeout time.Duration)

Stop instructs the type to halt operations and close the stop channel when it is finished.

timeout is grace period for which to wait before shutting down the server. The timeout value passed here will override the timeout given when constructing the server, as this is an explicit command to stop the server.

func (*Server) StopChan

func (srv *Server) StopChan() <-chan struct{}

StopChan gets the stop channel which will block until stopping has completed, at which point it is closed. Callers should never close the stop channel.

Source Files

graceful.go keepalive_listener.go limit_listen.go signal.go

Directories

PathSynopsis
tests
Version
v1.2.15 (latest)
Published
Feb 1, 2017
Platform
js/wasm
Imports
10 packages
Last checked
2 weeks ago

Tools for package owners.