Documentation ¶
Index ¶
- func CancelOnSignalsContext(ctx context.Context, sig ...os.Signal) (context.Context, context.CancelFunc)
- func ContextWithShutdown(ctx context.Context) (context.Context, context.CancelFunc)
- func NewSignalReceiver(sig ...os.Signal) <-chan os.Signal
- func RegisterStackTraceHandlerOnSignals(ctx context.Context, stackTraceHandler func(stackTraceOutput []byte) error, ...)
- func RegisterStackTraceWriter(out io.Writer, errHandler func(error)) (unregister func())
- func RegisterStackTraceWriterOnSignals(out io.Writer, errHandler func(error), sig ...os.Signal) (unregister func())
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 ¶
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 ¶
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 ¶
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.