Documentation ¶
Overview ¶
Package manners provides a wrapper for a standard net/http server that ensures all active HTTP client have completed their current request before the server shuts down.
It can be used a drop-in replacement for the standard http package, or can wrap a pre-configured Server.
eg.
http.Handle("/hello", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello\n")) }) log.Fatal(manners.ListenAndServe(":8080", nil))
or for a customized server:
s := manners.NewWithServer(&http.Server{ Addr: ":8080", Handler: myHandler, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, }) log.Fatal(s.ListenAndServe())
The server will shut down cleanly when the Close() method is called:
go func() { sigchan := make(chan os.Signal, 1) signal.Notify(sigchan, os.Interrupt, os.Kill) <-sigchan log.Info("Shutting down...") manners.Close() }() http.Handle("/hello", myHandler) log.Fatal(manners.ListenAndServe(":8080", nil))
Index ¶
- func NewTLSListener(inner net.Listener, config *tls.Config) net.Listener
- type Listener
- type ListenerMutateFunc
- type Options
- type Server
- func (gs *Server) BlockingClose() bool
- func (gs *Server) Close() bool
- func (gs *Server) FinishRoutine()
- func (gs *Server) GetFile() (*os.File, error)
- func (gs *Server) HijackListener(s *http.Server, fn ListenerMutateFunc) (*Server, error)
- func (gs *Server) ListenAndServe() error
- func (gs *Server) ListenAndServeTLS(certFile, keyFile string) error
- func (gs *Server) ListenAndServeTLSWithConfig(config *tls.Config) error
- func (gs *Server) RoutinesCount() int
- func (gs *Server) Serve(listener net.Listener) error
- func (gs *Server) StartRoutine()
- type StateHandler
- type TCPKeepAliveListener
- type TLSListener
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Listener ¶
type Listener struct {
// contains filtered or unexported fields
}
A GracefulListener differs from a standard net.Listener in one way: if Accept() is called after it is gracefully closed, it returns a listenerAlreadyClosed error. The GracefulServer will ignore this error.
func NewListener ¶
NewListener wraps an existing listener for use with GracefulServer.
Note that you generally don't need to use this directly as GracefulServer will automatically wrap any non-graceful listeners supplied to it.
type Options ¶
type Options struct { Server *http.Server StateHandler StateHandler Listener net.Listener }
Options used by NewWithOptions to provide parameters for a GracefulServer instance to be created.
type Server ¶
A GracefulServer maintains a WaitGroup that counts how many in-flight requests the server is handling. When it receives a shutdown signal, it stops accepting new requests but does not actually shut down until all in-flight requests terminate.
GracefulServer embeds the underlying net/http.Server making its non-override methods and properties avaiable.
It must be initialized by calling NewServer or NewWithServer
func NewWithOptions ¶
NewWithOptions creates a GracefulServer instance with the specified options.
func NewWithServer ¶
NewWithServer wraps an existing http.Server object and returns a GracefulServer that supports all of the original Server operations.
func (*Server) BlockingClose ¶
BlockingClose is similar to Close, except that it blocks until the last connection has been closed.
func (*Server) Close ¶
Close stops the server from accepting new requets and begins shutting down. It returns true if it's the first time Close is called.
func (*Server) FinishRoutine ¶
func (gs *Server) FinishRoutine()
FinishRoutine decrements the server's WaitGroup. Use this to complement StartRoutine().
func (*Server) HijackListener ¶
func (*Server) ListenAndServe ¶
ListenAndServe provides a graceful equivalent of net/http.Serve.ListenAndServe.
func (*Server) ListenAndServeTLS ¶
ListenAndServeTLS provides a graceful equivalent of net/http.Serve.ListenAndServeTLS.
func (*Server) ListenAndServeTLSWithConfig ¶
ListenAndServeTLS provides a graceful equivalent of net/http.Serve.ListenAndServeTLS.
func (*Server) RoutinesCount ¶
RoutinesCount returns the number of currently running routines
func (*Server) Serve ¶
Serve provides a graceful equivalent net/http.Server.Serve.
If listener is not an instance of *GracefulListener it will be wrapped to become one.
func (*Server) StartRoutine ¶
func (gs *Server) StartRoutine()
StartRoutine increments the server's WaitGroup. Use this if a web request starts more goroutines and these goroutines are not guaranteed to finish before the request.
type StateHandler ¶
StateHandler can be called by the server if the state of the connection changes. Notice that it passed previous state and the new state as parameters.
type TCPKeepAliveListener ¶
type TCPKeepAliveListener struct {
*net.TCPListener
}
TCPKeepAliveListener sets TCP keep-alive timeouts on accepted connections. It's used by ListenAndServe and ListenAndServeTLS so dead TCP connections (e.g. closing laptop mid-download) eventually go away.
direct lift from net/http/server.go
type TLSListener ¶
A listener implements a network listener (net.Listener) for TLS connections. direct lift from crypto/tls.go