goji: github.com/zenazn/goji/graceful Index | Files | Directories

package graceful

import "github.com/zenazn/goji/graceful"

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

Package Files

clone16.go einhorn.go graceful.go serve13.go server.go signal.go

func AddSignal Uses

func AddSignal(sig ...os.Signal)

AddSignal adds the given signal to the set of signals that trigger a graceful shutdown.

func DoubleKickWindow Uses

func DoubleKickWindow(d time.Duration)

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 Uses

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 Uses

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

ListenAndServe behaves exactly like the net/http function of the same name.

func ListenAndServeTLS Uses

func ListenAndServeTLS(addr, certfile, keyfile string, handler http.Handler) error

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 Uses

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 Uses

func PostHookWithSignal(f func(os.Signal))

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 Uses

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 Uses

func PreHookWithSignal(f func(os.Signal))

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 Uses

func ResetSignals()

ResetSignals resets the list of signals that trigger a graceful shutdown.

func Serve Uses

func Serve(l net.Listener, handler http.Handler) error

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 Uses

func Shutdown()

Shutdown manually triggers a shutdown from your application. Like Wait, blocks until all connections have gracefully shut down.

func ShutdownNow Uses

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 Uses

func Timeout(d time.Duration)

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 Uses

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 Uses

func WrapListener(l net.Listener) net.Listener

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.

type Server Uses

type Server http.Server

A Server is exactly the same as an http.Server, but provides more graceful implementations of its methods.

func (*Server) ListenAndServe Uses

func (srv *Server) ListenAndServe() error

ListenAndServe behaves like the method on net/http.Server with the same name.

func (*Server) ListenAndServeTLS Uses

func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error

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.

func (*Server) Serve Uses

func (srv *Server) Serve(l net.Listener) error

Serve behaves like the method on net/http.Server with the same name.

Directories

PathSynopsis
listenerPackage listener provides a way to incorporate graceful shutdown to any net.Listener.

Package graceful imports 12 packages (graph) and is imported by 128 packages. Updated 2018-12-05. Refresh now. Tools for package owners.