manners

package
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2016 License: MIT, AGPL-3.0, Apache-2.0 Imports: 5 Imported by: 0

README

Manners

A polite webserver for Go.

Manners allows you to shut your Go webserver down gracefully, without dropping any requests. It can act as a drop-in replacement for the standard library's http.ListenAndServe function:

func main() {
  handler := MyHTTPHandler()
  manners.ListenAndServe(":7000", handler)
}

Then, when you want to shut the server down:

manners.Close()

(Note that this does not block until all the requests are finished. Rather, the call to manners.ListenAndServe will stop blocking when all the requests are finished.)

Manners ensures that all requests are served by incrementing a WaitGroup when a request comes in and decrementing it when the request finishes.

If your request handler spawns Goroutines that are not guaranteed to finish with the request, you can ensure they are also completed with the StartRoutine and FinishRoutine functions on the server.

Known Issues

Manners does not correctly shut down long-lived keepalive connections when issued a shutdown command. Clients on an idle keepalive connection may see a connection reset error rather than a close. See https://github.com/braintree/manners/issues/13 for details.

Compatability

Manners 0.3.0 and above uses standard library functionality introduced in Go 1.3.

Installation

go get github.com/braintree/manners

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

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

func ListenAndServe(addr string, handler http.Handler) error

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

func ListenAndServeTLS(addr string, certFile string, keyFile string, handler http.Handler) error

ListenAndServeTLS provides a graceful version of the function provided by the net/http package. Call Close() to stop the server.

func Serve added in v1.3.0

func Serve(l net.Listener, handler http.Handler) error

Serve provides a graceful version of the function provided by the net/http package. Call Close() to stop the server.

Types

type GracefulServer

type GracefulServer struct {
	*http.Server
	// contains filtered or unexported fields
}

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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