Documentation ¶
Overview ¶
package signals implements support for managing interrupt signals
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type InterruptHandler ¶
type InterruptHandler struct { // C is the channel that receives interrupt requests C <-chan os.Signal // contains filtered or unexported fields }
InterruptHandler defines an interruption signal handler
func NewInterruptHandler ¶
func NewInterruptHandler(ctx context.Context, cancel context.CancelFunc, opts ...InterruptOption) *InterruptHandler
NewInterruptHandler creates a new interrupt handler for the specified configuration. Returned handler can be used to queue stoppers to the termination loop later by using AddStopper:
interrupt := NewInterruptHandler(...) interrupt.AddStopper(stoppers...)
Handler will execute all registered stoppers and exit iff it has been explicitly interrupted (see Abort).
Use the select loop and handle the receives on the interrupt channel:
ctx, cancel := ... interrupt := NewInterruptHandler(ctx, cancel) defer interrupt.Close()
for { select { case <-interrupt.C: if shouldTerminate() [ interrupt.Abort() } case <-interrupt.Done(): return } }
func WatchTerminationSignals ¶
func WatchTerminationSignals( ctx context.Context, cancel context.CancelFunc, printer utils.Printer, stoppers ...Stopper, ) *InterruptHandler
WatchTerminationSignals stops the provided stopper when it gets one of monitored signals. It is a convenience wrapper over NewInterruptHandler
func (*InterruptHandler) Abort ¶
func (r *InterruptHandler) Abort()
Abort sets the interrupted flag and interrupts the loop
func (*InterruptHandler) AddStopper ¶
func (r *InterruptHandler) AddStopper(stoppers ...Stopper)
AddStopper adds stoppers to the internal termination loop
func (*InterruptHandler) Close ¶
func (r *InterruptHandler) Close()
Close closes the loop and waits until all internal processes have stopped
func (*InterruptHandler) Done ¶
func (r *InterruptHandler) Done() <-chan struct{}
Done returns the channel that signals when this handler is closed
type InterruptOption ¶
type InterruptOption func(*InterruptHandler)
InterruptOption is a functional option to configure interrupt handler
func WithSignals ¶
func WithSignals(signals ...os.Signal) InterruptOption
WithSignals specifies which signal to consider interrupt signals.