Article
manners — gracefully shutdown Go server
Manners is a Go package from Braintree that allows you to create a web server that you can gracefully shutdown.
When creating a standard HTTP server in Go, it is common to use How to use
Just a create a new server like this:
Where to get
Manners can be installed with
ListenAndServe
to start listening to incoming connections and launch request handlers. ListenAndServe
blocks until there is an error. What if you want to unblock the server and stop it from running? With Manners, you can do it.
How to use manners
Just a create a new server like this:
func main() {
handler := MyHTTPHandler()
server := manners.NewServer()
server.ListenAndServe(":7000", handler)
}
As with the standard net/http
package, server.ListenAndServe
from the above example will block. However, when you want to shut it down, pass true
to the server.Shutdown
channel from another goroutine (for example, from a signal handler):
server.Shutdown <- true
Manners then will wait until all requests are finished serving, and shut down the server.
Where to get manners
Manners can be installed with go get
:
go get github.com/braintree/manners
Manners requires Go 1.3, since it's the first Golang version to introduce ConnState
hooks.