package signals

import "github.com/influxdata/influxdb/kit/signals"

Example (WithUnregisteredSignals)

Code:

{
	dctx, cancel := context.WithTimeout(context.TODO(), time.Millisecond*100)
	defer cancel()

	ctx := WithSignals(dctx, syscall.SIGUSR1)
	go func() {
		time.Sleep(10 * time.Millisecond) // after some time SIGUSR2 is sent
		// mimicking a signal from the outside, WithSignals will not handle it
		syscall.Kill(syscall.Getpid(), syscall.SIGUSR2)
	}()

	<-ctx.Done()
	fmt.Println("finished")
	// Output:
	// finished
}

Output:

finished

Index

Examples

Functions

func WithSignals

func WithSignals(ctx context.Context, sigs ...os.Signal) context.Context

WithSignals returns a context that is canceled with any signal in sigs.

Example

Code:

{
	ctx := WithSignals(context.Background(), syscall.SIGUSR1)
	go func() {
		time.Sleep(500 * time.Millisecond) // after some time SIGUSR1 is sent
		// mimicking a signal from the outside
		syscall.Kill(syscall.Getpid(), syscall.SIGUSR1)
	}()

	<-ctx.Done()
	fmt.Println("finished")
	// Output:
	// finished
}

Output:

finished

func WithStandardSignals

func WithStandardSignals(ctx context.Context) context.Context

WithStandardSignals cancels the context on os.Interrupt, syscall.SIGTERM.

Source Files

context.go

Version
v1.12.0 (latest)
Published
Apr 8, 2025
Platform
linux/amd64
Imports
4 packages
Last checked
2 days ago

Tools for package owners.