Documentation ¶
Overview ¶
Package endless provides a drop in replacement for the `net/http` standard library functions `ListenAndServe` and `ListenAndServeTLS` to achieve zero downtime restarts and code upgrades.
The code is based on the excellent post [Graceful Restart in Golang](http://grisha.org/blog/2014/06/03/graceful-restart-in-golang/) by [Grisha Trubetskoy](https://github.com/grisha). I took his code as a start. So a lot of credit to Grisha!
Index ¶
- Variables
- func ListenAndServe(addr string, handler http.Handler) error
- func ListenAndServeTLS(addr string, certFile string, keyFile string, handler http.Handler) error
- func Restart() (*os.Process, error)
- func SetHandler(h Handler)
- func Shutdown() error
- func Terminate(d time.Duration) error
- type ErrUnsupportedListener
- type Handler
- type InvalidStateError
- type Listener
- type Manager
- func (m *Manager) Debugln(v ...interface{})
- func (m *Manager) Listen(s *Server) (net.Listener, error)
- func (m *Manager) Register(srv *Server)
- func (m *Manager) Restart() (*os.Process, error)
- func (m *Manager) SetHandler(h Handler)
- func (m *Manager) Shutdown() error
- func (m *Manager) Terminate(d time.Duration) error
- func (m *Manager) Unregister(srv *Server)
- type Server
- func (srv *Server) AddressKey() string
- func (srv *Server) Debugln(v ...interface{})
- func (srv *Server) GetState() State
- func (srv *Server) ListenAndServe() error
- func (srv *Server) ListenAndServeTLS(certFile, keyFile string) (err error)
- func (srv *Server) Printf(format string, v ...interface{})
- func (srv *Server) Println(v ...interface{})
- func (srv *Server) Serve() error
- func (srv *Server) Shutdown() error
- func (srv *Server) Terminate(d time.Duration) error
- type SignalHandler
- type SignalHook
- type State
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultReadTimeout is the default value assigned to ReadTimeout of Servers. DefaultReadTimeout time.Duration // DefaultWriteTimeout is the default value assigned to WriteTimeout of Servers. DefaultWriteTimeout time.Duration // DefaultMaxHeaderBytes is the default value assigned to MaxHeaderBytes of Servers. DefaultMaxHeaderBytes int // DefaultTerminateTimeout is the default value assigned to TerminateTimeout of Servers. DefaultTerminateTimeout = 60 * time.Second )
Default values used on server creation.
var ErrNoHandler = errors.New("no handler")
ErrNoHandler is the error returned if no Handler was configured on the Server.
var ErrRestartInProgress = errors.New("already forked")
ErrRestartInProgress is the error returned if Restart is called while one is already progress.
Functions ¶
func ListenAndServe ¶
ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections. Handler is typically nil, in which case the DefaultServeMux is used.
func ListenAndServeTLS ¶
ListenAndServeTLS acts identically to ListenAndServe, except that it expects HTTPS connections. Additionally, files containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate followed by the CA's certificate.
Types ¶
type ErrUnsupportedListener ¶
ErrUnsupportedListener is the error returned when the Listener doesn't support a File method.
func (*ErrUnsupportedListener) Error ¶
func (e *ErrUnsupportedListener) Error() string
type Handler ¶
type Handler interface { Handle(*Manager) Stop() }
Handler is the interface that objects implement to perform operations on a endless Servers.
type InvalidStateError ¶
InvalidStateError is the error type returned if an invalid state transition is attempted.
func (*InvalidStateError) Error ¶
func (err *InvalidStateError) Error() string
type Listener ¶
Listener represents an endless listener.
func NewListener ¶
NewListener creates a new Listener.
func (*Listener) Accept ¶
Accept implements the Accept method in the Listener interface; it waits for the next call and returns a generic Conn.
type Manager ¶
Manager is the responsible for managing Servers.
func (*Manager) Debugln ¶
func (m *Manager) Debugln(v ...interface{})
Debugln calls Println on Logger with the current pid prepended if Debug is true
func (*Manager) Listen ¶
Listen returns a listener created from the socket it was passed when restarted or a new listener created from the details given.
func (*Manager) SetHandler ¶
SetHandler sets the active handler on the Manager. If the manager already had an active handler then its Stop method is called.
func (*Manager) Unregister ¶
Unregister removes a server to the managers registered servers.
type Server ¶
type Server struct { http.Server BeforeBegin func(add string) TerminateTimeout time.Duration Done chan struct{} Debug bool Net string // contains filtered or unexported fields }
Server represents a endless server.
func NewServer ¶
NewServer returns an initialized Server Object. Calling Serve on it will actually "start" the server.
func (*Server) AddressKey ¶
AddressKey returns the unique address key for the server.
func (*Server) Debugln ¶
func (srv *Server) Debugln(v ...interface{})
Debugln calls Println with the current pid prepended if Debug is true
func (*Server) ListenAndServe ¶
ListenAndServe listens on the TCP network address srv.Addr and then calls Serve to handle requests on incoming connections. If srv.Addr is blank, ":http" is used.
func (*Server) ListenAndServeTLS ¶
ListenAndServeTLS listens on the TCP network address srv.Addr and then calls Serve to handle requests on incoming TLS connections.
Filenames containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate followed by the CA's certificate.
If srv.Addr is blank, ":https" is used.
func (*Server) Println ¶
func (srv *Server) Println(v ...interface{})
Println calls Println on ErrorLog if not nil otherwise it calls log.Println.
func (*Server) Serve ¶
Serve accepts incoming HTTP connections on the Listener l, creating a new service goroutine for each. The service goroutines read requests and then call handler to reply to them. Handler is typically nil, in which case the DefaultServeMux is used.
In addition to the standard library Serve behaviour each connection is added to a sync.Waitgroup so that all outstanding connections can be served before shutting down the server.
func (*Server) Shutdown ¶
Shutdown closes the Listener so that no new connections are accepted. it also starts a goroutine that will terminate (stop all running requests) the server after TerminateTimeout.
func (*Server) Terminate ¶
Terminate forces the server to shutdown in a given timeout - whether it finished outstanding requests or not. if Read/WriteTimeout are not set or the max header size is very big a connection could hang...
srv.Serve() will not return until all connections are served. this will unblock the srv.wg.Wait() in Serve() thus causing ListenAndServe(TLS) to return.
type SignalHandler ¶
type SignalHandler struct {
// contains filtered or unexported fields
}
SignalHandler listens for signals and takes action on the Manager.
By default: SIGHUP: calls Restart() SIGUSR2: calls Terminate(0), Shutdown() must have been called first. SIGINT & SIGTERM: calls Shutdown()
Pre and post signal handles can also be registered for custom actions.
func NewSignalHandler ¶
func NewSignalHandler() *SignalHandler
NewSignalHandler create a new SignalHandler for the s
func (*SignalHandler) Handle ¶
func (s *SignalHandler) Handle(m *Manager)
Handle listens for os.Signal's and calls any registered function hooks.
func (*SignalHandler) RegisterPostSignalHook ¶
func (s *SignalHandler) RegisterPostSignalHook(sig os.Signal, f SignalHook)
RegisterPostSignalHook registers a function to be run after any built in signal action.
func (*SignalHandler) RegisterPreSignalHook ¶
func (s *SignalHandler) RegisterPreSignalHook(sig os.Signal, f SignalHook)
RegisterPreSignalHook registers a function to be run before any built in signal action.
func (*SignalHandler) Stop ¶
func (s *SignalHandler) Stop()
Stop stops the handler from taking any more action
type SignalHook ¶
SignalHook represents a signal processing hook. If false is returned no further processing of the signal is performed.
type State ¶
type State uint8
State represents the current state of Server
const ( // StateInit is the state of server when its created. StateInit State = iota // StateRunning is the state of server when its running. StateRunning // StateShuttingDown is the state of server when it is shutting down. StateShuttingDown // StateShutdown is the state of server when it has shutdown. StateShutdown // StateTerminated is the state of server if it was forcibly terminated. StateTerminated // StateFailed is the state of server if failed unexpectedly. StateFailed )