ctxsignal

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2018 License: MIT Imports: 6 Imported by: 5

README

ctxsignal

GoDoc Build Status Coverage Status codebeat badge Go Report Card

Package ctxsignal can be used to create contexts cancelable by system signals.

Example

Creating a context copy cancelable when intercepting a SIGINT, SIGTERM, or SIGHUP signal:

ctx, cancel := ctxsignal.WithSignals(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
defer cancel()

<-ctx.Done()

fmt.Println("Received signal!")

You can check what type of signal was received with:

sig, err := ctxsignal.Closed(ctx)

if err != nil {
        return err
}

fmt.Println(sig) // sig type is os.Signal

You can send a signal using kill -SIGNAL PID. Example: kill -SIGHUP 170.

On Unix-like systems you can read the manual about signals with

$ man signal

Signals available on a typical Linux system

$ kill -l
 1) SIGHUP         2) SIGINT         3) SIGQUIT         4) SIGILL
 5) SIGTRAP        6) SIGABRT        7) SIGBUS          8) SIGFPE
 9) SIGKILL       10) SIGUSR1       11) SIGSEGV        12) SIGUSR2
13) SIGPIPE       14) SIGALRM       15) SIGTERM        16) SIGSTKFLT
17) SIGCHLD       18) SIGCONT       19) SIGSTOP        20) SIGTSTP
21) SIGTTIN       22) SIGTTOU       23) SIGURG         24) SIGXCPU
25) SIGXFSZ       26) SIGVTALRM     27) SIGPROF        28) SIGWINCH
29) SIGPOLL       30) SIGPWR        31) SIGSYS         32) SIGRTMIN
64) SIGRTMAX

SIGKILL and SIGSTOP signals cannot be intercepted or handled.

See the docs for more examples and information.

Documentation

Overview

Package ctxsignal can be used to create contexts cancelable by system signals.

You can send a signal using kill -SIGNAL PID (e.g., kill -SIGHUP 170).

kill -l gives you a list of signals available on your system. On Unix-like systems you can use "man signal" to learn about signals. SIGKILL and SIGSTOP signals cannot be intercepted or handled.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Closed

func Closed(ctx context.Context) (os.Signal, error)

Closed gets the signal that closed a context channel.

Example
Output:

hangup

func WithSignals

func WithSignals(parent context.Context, signals ...os.Signal) (context.Context, context.CancelFunc)

WithSignals returns a copy of the parent context cancelable by the given system signals. The signals are reset when the context's Done channel is closed.

Example
Output:

Signaled!

func WithTermination

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

WithTermination creates a context canceled on signals SIGINT or SIGTERM.

Example
Output:

Interrupted!

Types

This section is empty.

Notes

Bugs

  • Be aware signal handling is vulnerable to race conditions.

Jump to

Keyboard shortcuts

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