Documentation ¶
Overview ¶
Package graceful implements graceful shutdown for HTTP servers by closing idle connections after receiving a signal. By default, this package listens for interrupts (i.e., SIGINT), but when it detects that it is running under Einhorn it will additionally listen for SIGUSR2 as well, giving your application automatic support for graceful restarts/code upgrades.
Index ¶
- func AddSignal(sig ...os.Signal)
- func HandleSignals()
- func ListenAndServe(addr string, handler http.Handler) error
- func ListenAndServeTLS(addr, certfile, keyfile string, handler http.Handler) error
- func Middleware(h http.Handler) http.Handler
- func PostHook(f func())
- func PreHook(f func())
- func ResetSignals()
- func Serve(l net.Listener, handler http.Handler) error
- func Shutdown()
- func Wait()
- func WrapListener(l net.Listener) net.Listener
- type Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddSignal ¶
AddSignal adds the given signal to the set of signals that trigger a graceful shutdown.
func HandleSignals ¶
func HandleSignals()
HandleSignals installs signal handlers for a set of standard signals. By default, this set only includes keyboard interrupts, however when the package detects that it is running under Einhorn, a SIGUSR2 handler is installed as well.
func ListenAndServe ¶
ListenAndServe behaves exactly like the net/http function of the same name.
func ListenAndServeTLS ¶
ListenAndServeTLS behaves exactly like the net/http function of the same name.
func Middleware ¶
Middleware is a stub that does nothing. When used with versions of Go before Go 1.3, it provides functionality similar to net/http.Server's SetKeepAlivesEnabled.
func PostHook ¶
func PostHook(f func())
PostHook registers a function to be called after all of this package's normal shutdown actions. All listeners will be called in the order they were added, from a single goroutine, and are guaranteed to be called after all listening connections have been closed, but before Wait() returns.
If you've Hijacked any connections that must be gracefully shut down in some other way (since this library disowns all hijacked connections), it's reasonable to use a PostHook to signal and wait for them.
func PreHook ¶
func PreHook(f func())
PreHook registers a function to be called before any of this package's normal shutdown actions. All listeners will be called in the order they were added, from a single goroutine.
func ResetSignals ¶
func ResetSignals()
ResetSignals resets the list of signals that trigger a graceful shutdown.
func Shutdown ¶
func Shutdown()
Shutdown manually triggers a shutdown from your application. Like Wait(), blocks until all connections have gracefully shut down.
func Wait ¶
func Wait()
Wait for all connections to gracefully shut down. This is commonly called at the bottom of the main() function to prevent the program from exiting prematurely.
func WrapListener ¶
WrapListener wraps an arbitrary net.Listener for use with graceful shutdowns. In the background, it uses the listener sub-package to Wrap the listener in Deadline mode. If another mode of operation is desired, you should call listener.Wrap yourself: this function is smart enough to not double-wrap listeners.
Types ¶
type Server ¶
Type Server is exactly the same as an http.Server, but provides more graceful implementations of its methods.