graceful

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2021 License: MIT Imports: 5 Imported by: 3

README

gone/http/graceful

A golang library wrapping around the stdlib http.Server to provide graceful shutdown.

Example

	s := &Server{
		Server: &http.Server{},
	}
	
	l, err := net.Listen("tcp", "")
	if err != nil {
		log.Fatal(err)
	}
	
	go func() {
		time.Sleep(time.Second)
		s.Shutdown() // Signal the server for shutdown.
		s.Wait() // Wait for the last connection to be closed.
	}() 

	err = s.Serve(l) // Block until Shutdown() is called
	if err != nil {
		log.Fatal(err)
	}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NotReadyError

type NotReadyError struct {
	Err error
	// Quitting is true when a shutdown is in progress
	Quitting bool
}

NotReadyError is returned from a Serve() call if the server is already running, or during shutdown.

func (*NotReadyError) Error

func (e *NotReadyError) Error() string

type Server

type Server struct {
	*http.Server

	// Timeout is the duration to allow outstanding requests to survive
	// before forcefully terminating them.
	// A timeout of 0 will make the server wait forever for connections to close
	// by them selves. (as per github.com/tylerb/graceful doc.)
	Timeout 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)

	// SyncShutdown makes the server not exit immediately on Shutdown signal.
	// But wait for keep-alive connections to finish.
	// If not setting SyncShutdown true, Call Wait() to wait for shutdown to finish
	SyncShutdown bool
	// contains filtered or unexported fields
}

Server implements an extended http.Server with a Shutdown() method which enables it to be shut down gracefully, by first stopping accepting and disabling keepalives and then wait for all connections to have terminated. A Timeout can be set to finally kill the last outstanding connections forcefully.

func (*Server) ConnectionsKilled

func (srv *Server) ConnectionsKilled() int

ConnectionsKilled returns the number of HTTP connections killed by the server during last shutdown.

func (*Server) Serve

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

Serve is equivalent to net/http.Server.Serve() with graceful shutdown enabled.

Example
s := &Server{
	Server: &http.Server{},
}

l, err := net.Listen("tcp", "")
if err != nil {
	log.Fatal(err)
}

go func() {
	time.Sleep(time.Second)
	s.Shutdown()
	s.Wait()
}()

err = s.Serve(l)
if err != nil {
	log.Fatal(err)
}
fmt.Println("OK")
Output:

OK

func (*Server) Shutdown

func (srv *Server) Shutdown()

Shutdown signales the server asynchronously to start shutdown process It only has effect the first time it's called.

func (*Server) ShutdownOK

func (srv *Server) ShutdownOK() bool

ShutdownOK stops the server from accepting new requets and begins shutting down. It returns true if this call ended up being the one triggering a shutdown

func (*Server) Wait

func (srv *Server) Wait()

Wait for server shutdown to finish (having killed any remaning keep-alive connections)

Jump to

Keyboard shortcuts

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