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 DoubleKickWindow(d time.Duration)
- func HandleSignals()
- func ListenAndServe(addr string, handler http.Handler) error
- func ListenAndServeTLS(addr, certfile, keyfile string, handler http.Handler) error
- func PostHook(f func())
- func PostHookWithSignal(f func(os.Signal))
- func PreHook(f func())
- func PreHookWithSignal(f func(os.Signal))
- func ResetSignals()
- func Serve(l net.Listener, handler http.Handler) error
- func Shutdown()
- func ShutdownNow()
- func Timeout(d time.Duration)
- 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 DoubleKickWindow ¶ added in v0.8.3
DoubleKickWindow sets the length of the window during which two back-to-back signals are treated as an especially urgent or forceful request to exit (i.e., ShutdownNow instead of Shutdown). Signals delivered more than this duration apart are treated as separate requests to exit gracefully as usual.
Setting DoubleKickWindow to 0 disables the feature.
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 almost exactly like the net/http function of the same name. Unlike net/http, however, this function defaults to enforcing TLS 1.0 or higher in order to address the POODLE vulnerability. Users who wish to enable SSLv3 must do so by explicitly instantiating a server with an appropriately configured TLSConfig property.
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 PostHookWithSignal ¶ added in v1.0.1
PostHookWithSignal registers a function to be called after all of this package's normal shutdown actions, which receives the signal that caused the shutdown (or nil for manual shutdowns). 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 PreHookWithSignal ¶ added in v1.0.1
PreHookWithSignal registers a function to be called before any of this package's normal shutdown actions, which recieves the signal that caused the shutdown (or nil for manual shutdowns). 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 Serve ¶
Serve mostly behaves like the net/http function of the same name, except that if the passed listener is a net.TCPListener, TCP keep-alives are enabled on accepted connections.
func Shutdown ¶
func Shutdown()
Shutdown manually triggers a shutdown from your application. Like Wait, blocks until all connections have gracefully shut down.
func ShutdownNow ¶ added in v0.8.3
func ShutdownNow()
ShutdownNow triggers an immediate shutdown from your application. All connections (not just those that are idle) are immediately closed, even if they are in the middle of serving a request.
func Timeout ¶ added in v0.8.3
Timeout sets the maximum amount of time package graceful will wait for connections to gracefully shut down after receiving a signal. After this timeout, connections will be forcefully shut down (similar to calling ShutdownNow).
Setting Timeout to 0 disables the feature.
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 ¶
A Server is exactly the same as an http.Server, but provides more graceful implementations of its methods.
func (*Server) ListenAndServe ¶
ListenAndServe behaves like the method on net/http.Server with the same name.
func (*Server) ListenAndServeTLS ¶
ListenAndServeTLS behaves like the method on net/http.Server with the same name. Unlike the method of the same name on http.Server, this function defaults to enforcing TLS 1.0 or higher in order to address the POODLE vulnerability. Users who wish to enable SSLv3 must do so by supplying a TLSConfig explicitly.