Documentation ¶
Overview ¶
Package signals implements utilities for managing process shutdown with support for signal-handling.
Index ¶
Constants ¶
const (
DoubleStopExitCode = 1
)
Variables ¶
var SameSignalTimeWindow time.Duration
SameSignalTimeWindow specifies the time window during which multiple deliveries of the same signal are counted as one signal. If set to zero, no such de-duping occurs. This is useful in situations where a process receives a signal explicitly sent by its parent when the parent receives the signal, but also receives it independently by virtue of being part of the same process group.
This is a variable, so that it can be set appropriately. Note, there is no locking around it, the assumption being that it's set during initialization and never reset afterwards.
Functions ¶
func Default ¶
Default returns a set of platform-specific signals that applications are encouraged to listen on.
func ShutdownOnSignals ¶
ShutdownOnSignals registers signal handlers for the specified signals, or, if none are specified, the default signals. The first signal received will be made available on the returned channel; upon receiving a second signal, the process will exit if that signal differs from the first, or if the same it arrives more than a second after the first.
Types ¶
type ContextDoneSignal ¶ added in v0.1.10
type ContextDoneSignal string
func (ContextDoneSignal) Signal ¶ added in v0.1.10
func (ContextDoneSignal) Signal()
func (ContextDoneSignal) String ¶ added in v0.1.10
func (s ContextDoneSignal) String() string
type Handler ¶ added in v0.1.10
type Handler struct {
// contains filtered or unexported fields
}
Handler represents a signal handler that can be used to wait for signal reception or context cancelation as per NotifyWithCancel. In addition it can be used to register additional cancel functions to be invoked on signal reception or context cancelation.
func ShutdownOnSignalsWithCancel ¶ added in v0.1.10
ShutdownOnSignalsWithCancel is like ShutdownOnSignals except it forks the supplied context to obtain a cancel function which is called by the returned function when a signal is received. The returned function can be called to wait for the signal to be received or for the context to be canceled. Typical usage would be:
func main() { ctx, shutdown := v23.Init() defer shutdown() ctx, handler := ShutdownOnSignalsWithCancel(ctx) defer handler.WaitForSignal() _, srv, err := v23.WithNewServer(ctx, ...) }
waitForInterrupt will wait for a signal to be received at which point it will cancel the context and thus the server created by WithNewServer to initiate its internal shutdown. The deferred shutdown returned by v23.Init() will then wait for that the server to complete its shutdown. Canceling the context is treated as receipt of a custom signal, ContextDoneSignal, in terms of its returns value.
func (*Handler) RegisterCancel ¶ added in v0.1.10
func (h *Handler) RegisterCancel(fns ...func())
RegisterCancel registers one or more cancel functions to be invoked when a signal is received or the original context is canceled.
func (*Handler) WaitForSignal ¶ added in v0.1.10
WaitForSignal will wait for a signal to be received. Context cancelation is translated into a ContextDoneSignal signal.