manners

package module
v0.0.0-...-27fa941 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2013 License: MIT Imports: 5 Imported by: 41

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()
  signal.Notify(manners.ShutdownChannel)
  manners.ListenAndServe(handler, ":7000")
}

Advanced users have full access to Manners' internals, so they can construct custom handling procedures:

func main() {
  handler = MyHTTPHandler()
  baseListener, err := net.Listen(":7000")
  if err != nil {
    panic(err)
  }
  listener := manners.NewListener(baseListener)

  // Do all sorts of stuff with the listener

  manners.Serve(listener, handler)
}

It's also easy to trigger the shutdown command programmically:

manners.ShutdownChannel <- syscall.SIGINT

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 StarRoutine function:

func (this *MyHTTPHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
  DoAsynchronousComputations()
  // Implicitly return 200
}

func DoAsynchronousComputations() {
  manners.StartRoutine()
  go func() {
    defer manners.FinishRoutine()
    // Do the computations
  }()
}
Installation

go get github.com/braintree/manners

Contributors

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ShutdownChannel = make(chan os.Signal)
)

Functions

func FinishRoutine

func FinishRoutine()

func ListenAndServe

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

func RunRoutine

func RunRoutine(f func())

func Serve

func Serve(listener *GracefulListener, handler http.Handler) error

func StartRoutine

func StartRoutine()

func WaitForShutdown

func WaitForShutdown()

Types

type GracefulConnection

type GracefulConnection struct {
	net.Conn
}

func (GracefulConnection) Close

func (this GracefulConnection) Close() error

type GracefulListener

type GracefulListener struct {
	net.Listener
	// contains filtered or unexported fields
}

func NewListener

func NewListener(oldListener net.Listener) *GracefulListener

func (*GracefulListener) Accept

func (this *GracefulListener) Accept() (net.Conn, error)

func (*GracefulListener) Close

func (this *GracefulListener) Close() error

func (*GracefulListener) CloseOnShutdown

func (this *GracefulListener) CloseOnShutdown()

type MyResponseWriter

type MyResponseWriter struct {
	MyHeader http.Header
	Content  *bytes.Buffer
}

func NewResponseWriter

func NewResponseWriter() *MyResponseWriter

func (*MyResponseWriter) Header

func (this *MyResponseWriter) Header() http.Header

func (*MyResponseWriter) Write

func (this *MyResponseWriter) Write(content []byte) (int, error)

func (*MyResponseWriter) WriteHeader

func (this *MyResponseWriter) WriteHeader(status int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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