Documentation ¶
Overview ¶
Package signals provides support for working with operating system signals and contexts.
Index ¶
Constants ¶
const ( // ExitCode is the exit code passed to os.Exit when a subsequent signal is // received. ExitCode = 1 )
Variables ¶
var DebounceDuration = time.Second
DebounceDuration is the time period during which subsequent identical signals are ignored.
Functions ¶
Types ¶
type ContextDoneSignal ¶
type ContextDoneSignal string
ContextDoneSignal implements os.Signal and is used to translate a canceled context into an os.Signal as forwarded by NotifyWithCancel.
func (ContextDoneSignal) String ¶
func (s ContextDoneSignal) String() string
Stringimplements os.Signal.
type Handler ¶
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 NotifyWithCancel ¶
NotifyWithCancel is like signal.Notify except that it forks (and returns) the supplied context to obtain a cancel function that is called when a signal is received. It will also catch the cancelation of the supplied context and turn it into an instance of ContextDoneSignal. The returned handler can be used to wait for the signals to be received and to register additional cancelation functions to be invoked when a signal is received. Typical usage would be:
func main() { ctx, handler := signals.NotifyWithCancel(context.Background(), signals.Defaults()...) .... handler.RegisterCancel(func() { ... }) ... defer hanlder.WaitForSignal() // wait for a signal or context cancelation. }
If a second, different, signal is received then os.Exit(ExitCode) is called. Subsequent signals are the same as the first are ignored for one second but after that will similarly lead to os.Exit(ExitCode) being called.
func (*Handler) RegisterCancel ¶
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 ¶
WaitForSignal will wait for a signal to be received. Context cancelation is translated into a ContextDoneSignal signal.