signals

package
v0.1.20 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2021 License: BSD-3-Clause Imports: 6 Imported by: 116

Documentation

Overview

Package signals implements utilities for managing process shutdown with support for signal-handling.

Index

Constants

View Source
const (
	DoubleStopExitCode = 1
)

Variables

View Source
var SameSignalTimeWindow time.Duration

SameSignalTimeWindow specifies the time window during which multiple deliveries of the same signal are counted as one signal. If set to zero, no such de-duping occurs. This is useful in situations where a process receives a signal explicitly sent by its parent when the parent receives the signal, but also receives it independently by virtue of being part of the same process group.

This is a variable, so that it can be set appropriately. Note, there is no locking around it, the assumption being that it's set during initialization and never reset afterwards.

Functions

func Default

func Default() []os.Signal

Default returns a set of platform-specific signals that applications are encouraged to listen on.

func ShutdownOnSignals

func ShutdownOnSignals(ctx *context.T, signals ...os.Signal) <-chan os.Signal

ShutdownOnSignals registers signal handlers for the specified signals, or, if none are specified, the default signals. The first signal received will be made available on the returned channel; upon receiving a second signal, the process will exit if that signal differs from the first, or if the same it arrives more than a second after the first.

Types

type ContextDoneSignal added in v0.1.10

type ContextDoneSignal string

func (ContextDoneSignal) Signal added in v0.1.10

func (ContextDoneSignal) Signal()

func (ContextDoneSignal) String added in v0.1.10

func (s ContextDoneSignal) String() string

type Handler added in v0.1.10

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 ShutdownOnSignalsWithCancel added in v0.1.10

func ShutdownOnSignalsWithCancel(ctx *context.T, signals ...os.Signal) (*context.T, *Handler)

ShutdownOnSignalsWithCancel is like ShutdownOnSignals except it forks the supplied context to obtain a cancel function which is called by the returned function when a signal is received. The returned function can be called to wait for the signal to be received or for the context to be canceled. Typical usage would be:

   func main() {
	    ctx, shutdown := v23.Init()
     defer shutdown()
     ctx, handler := ShutdownOnSignalsWithCancel(ctx)
     defer handler.WaitForSignal()

     _, srv, err := v23.WithNewServer(ctx, ...)

   }

waitForInterrupt will wait for a signal to be received at which point it will cancel the context and thus the server created by WithNewServer to initiate its internal shutdown. The deferred shutdown returned by v23.Init() will then wait for that the server to complete its shutdown. Canceling the context is treated as receipt of a custom signal, ContextDoneSignal, in terms of its returns value.

func (*Handler) RegisterCancel added in v0.1.10

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 added in v0.1.10

func (h *Handler) WaitForSignal() os.Signal

WaitForSignal will wait for a signal to be received. Context cancelation is translated into a ContextDoneSignal signal.

Jump to

Keyboard shortcuts

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