manners: github.com/braintree/manners Index | Files | Directories

package manners

import "github.com/braintree/manners"

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

Package Files

interfaces.go server.go static.go

func Close Uses

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 Uses

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 Uses

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 Uses

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.

type GracefulServer Uses

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 NewServer Uses

func NewServer() *GracefulServer

NewServer creates a new GracefulServer.

func NewWithServer Uses

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 Uses

func (s *GracefulServer) BlockingClose() bool

BlockingClose is similar to Close, except that it blocks until the last connection has been closed.

func (*GracefulServer) Close Uses

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 Uses

func (s *GracefulServer) FinishRoutine()

FinishRoutine decrements the server's WaitGroup. Use this to complement StartRoutine().

func (*GracefulServer) ListenAndServe Uses

func (s *GracefulServer) ListenAndServe() error

ListenAndServe provides a graceful equivalent of net/http.Serve.ListenAndServe.

func (*GracefulServer) ListenAndServeTLS Uses

func (s *GracefulServer) ListenAndServeTLS(certFile, keyFile string) error

ListenAndServeTLS provides a graceful equivalent of net/http.Serve.ListenAndServeTLS.

func (*GracefulServer) RoutinesCount Uses

func (s *GracefulServer) RoutinesCount() int

RoutinesCount returns the number of currently running routines

func (*GracefulServer) Serve Uses

func (s *GracefulServer) Serve(listener net.Listener) error

Serve provides a graceful equivalent net/http.Server.Serve.

func (*GracefulServer) StartRoutine Uses

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

PathSynopsis
test_helpers

Package manners imports 5 packages (graph) and is imported by 167 packages. Updated 2016-07-19. Refresh now. Tools for package owners.