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 Close() bool
- func ListenAndServe(addr string, handler http.Handler) error
- func ListenAndServeTLS(addr string, certFile string, keyFile string, handler http.Handler) error
- func Serve(l net.Listener, handler http.Handler) error
- type GracefulServer
- func (s *GracefulServer) BlockingClose() bool
- func (s *GracefulServer) Close() bool
- func (s *GracefulServer) FinishRoutine()
- func (s *GracefulServer) ListenAndServe() error
- func (s *GracefulServer) ListenAndServeTLS(certFile, keyFile string) error
- func (s *GracefulServer) Serve(listener net.Listener) error
- func (s *GracefulServer) StartRoutine()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Close ¶ added in v1.3.0
func Close() bool
Shuts down the default server used by ListenAndServe, ListenAndServeTLS and Serve. It returns true if it's the first time Close is called.
func ListenAndServe ¶ added in v1.3.0
ListenAndServe provides a graceful version of the function provided by the net/http package. Call Close() to stop the server.
func ListenAndServeTLS ¶ added in v1.3.0
ListenAndServeTLS provides a graceful version of the function provided by the net/http package. Call Close() to stop the server.
Types ¶
type GracefulServer ¶
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 NewWithServer.
func NewWithServer ¶ added in v1.3.0
func NewWithServer(s *http.Server) *GracefulServer
NewWithServer wraps an existing http.Server object and returns a GracefulServer that supports all of the original Server operations.
func (*GracefulServer) BlockingClose ¶ added in v1.3.0
func (s *GracefulServer) BlockingClose() bool
BlockingClose is similar to Close, except that it blocks until the last connection has been closed.
func (*GracefulServer) Close ¶ added in v1.3.0
func (s *GracefulServer) Close() bool
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 (*GracefulServer) FinishRoutine ¶
func (s *GracefulServer) FinishRoutine()
FinishRoutine decrements the server's WaitGroup. Use this to complement StartRoutine().
func (*GracefulServer) ListenAndServe ¶
func (s *GracefulServer) ListenAndServe() error
ListenAndServe provides a graceful equivalent of net/http.Serve.ListenAndServe.
func (*GracefulServer) ListenAndServeTLS ¶ added in v1.3.0
func (s *GracefulServer) ListenAndServeTLS(certFile, keyFile string) error
ListenAndServeTLS provides a graceful equivalent of net/http.Serve.ListenAndServeTLS.
func (*GracefulServer) Serve ¶
func (s *GracefulServer) Serve(listener net.Listener) error
Serve provides a graceful equivalent net/http.Server.Serve.
func (*GracefulServer) StartRoutine ¶
func (s *GracefulServer) 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.