signals

package
v0.0.0-...-3bfe646 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2023 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Overview

package signals implements support for managing interrupt signals

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WaitFor

func WaitFor(signals ...os.Signal)

WaitFor blocks until any of the specified signals has been signalled

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.

type Stopper

type Stopper interface {
	// Stop gracefully stops a process
	Stop(ctx context.Context) error
}

Stopper is an interface for processes that can be stopped

type StopperFunc

type StopperFunc func(ctx context.Context) error

StopperFunc is an adapter function that allows the use of ordinary functions as Stoppers

func (StopperFunc) Stop

func (r StopperFunc) Stop(ctx context.Context) error

Stop implements Stopper

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL