signals

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: BSD-3-Clause Imports: 7 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CancelOnSignalsContext

func CancelOnSignalsContext(ctx context.Context, sig ...os.Signal) (context.Context, context.CancelFunc)

CancelOnSignalsContext returns a context that is cancelled when any of the provided signals are received.

func ContextWithShutdown

func ContextWithShutdown(ctx context.Context) (context.Context, context.CancelFunc)

ContextWithShutdown returns a context that is cancelled when the SIGTERM or SIGINT signal is received. This can be useful to use for cleanup tasks that would typically be deferred and which you want to run even if the program is terminated or cancelled. The following is a common usage pattern:

cleanupCtx, cancel := signals.ContextWithShutdown(context.Background())
cleanupDone := make(chan struct{})
defer func() {
	cancel()
	<-cleanupDone
}()
go func() {
	select {
	case <-cleanupCtx.Done():
		// perform cleanup action
	}
	cleanupDone <- struct{}{}
}()

func NewSignalReceiver

func NewSignalReceiver(sig ...os.Signal) <-chan os.Signal

NewSignalReceiver returns a buffered channel that is registered to receive the provided signals.

func RegisterStackTraceHandlerOnSignals

func RegisterStackTraceHandlerOnSignals(ctx context.Context, stackTraceHandler func(stackTraceOutput []byte) error, errHandler func(error), sig ...os.Signal)

RegisterStackTraceHandlerOnSignals starts a goroutine that listens for the specified signals and calls stackTraceHandler with a pprof-formatted snapshot of all running goroutines when any of the provided signals are received. If stackTraceHandler returns an error, that error is provided to the errHandler function if one is provided. No goroutine is created if stackTraceHandler is nil. If no signals are provided, the handler will receive notifications for all signals (matching the os/signal.Notify API). The handler will exit when ctx is cancelled.

func RegisterStackTraceWriter

func RegisterStackTraceWriter(out io.Writer, errHandler func(error)) (unregister func())

RegisterStackTraceWriter starts a goroutine that listens for the SIGQUIT (kill -3) signal and writes a pprof-formatted snapshot of all running goroutines when the signal is received. If writing to out returns an error, that error is provided to the errHandler function if one is provided. Returns a function that unregisters the listener when called.

func RegisterStackTraceWriterOnSignals

func RegisterStackTraceWriterOnSignals(out io.Writer, errHandler func(error), sig ...os.Signal) (unregister func())

RegisterStackTraceWriterOnSignals starts a goroutine that listens for the specified signals and writes a pprof-formatted snapshot of all running goroutines to out when any of the provided signals are received. If writing to out returns an error, that error is provided to the errHandler function if one is provided. Returns a function that unregisters the listener when called.

Types

This section is empty.

Jump to

Keyboard shortcuts

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