Documentation
¶
Overview ¶
Package signals provides utilities for handling OS signals in Go applications.
This package offers functionality to create contexts that can be canceled when specific OS signals are received, making it easier to implement graceful shutdown or interruption handling in applications.
The key feature is NotifyContext, which creates a context that is canceled when one of the specified signals is received, and executes a function with that context. The signal that caused cancellation can be retrieved using FromContext.
Example (NotifyContext) ¶
package main
import (
"context"
"errors"
"fmt"
"os"
"syscall"
"github.com/goaux/signals"
)
func main() {
ctx := context.Background()
err := signals.NotifyContext(ctx, runN, syscall.SIGINT, syscall.SIGTERM)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
}
func runN(ctx context.Context) error {
fmt.Println(ctx)
syscall.Kill(os.Getpid(), syscall.SIGINT)
<-ctx.Done()
err := context.Cause(ctx)
fmt.Println(err.Error())
fmt.Println(errors.Is(err, context.Canceled))
fmt.Println(signals.FromContext(ctx))
return nil
}
Output: signals.NotifyContext(context.Background, [interrupt terminated]) context canceled (interrupt) true interrupt true
Example (Wait) ¶
package main
import (
"context"
"fmt"
"syscall"
"time"
"github.com/goaux/signals"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
fmt.Println("Waiting for SIGINT or SIGTERM...")
sig := signals.Wait(ctx, syscall.SIGINT, syscall.SIGTERM)
if sig != nil {
fmt.Printf("Received signal: %v\n", sig)
} else {
fmt.Println("Context canceled")
}
}
Output: Waiting for SIGINT or SIGTERM... Context canceled
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromContext ¶ added in v1.2.0
FromContext retrieves the signal that caused a context to be canceled.
If the context was canceled by NotifyContext due to receiving a signal, FromContext returns that signal and true. Otherwise, it returns nil and false.
Example:
if sig, ok := signals.FromContext(ctx); ok {
fmt.Printf("Context was canceled by signal: %v\n", sig)
}
func NotifyContext ¶ added in v1.1.0
func NotifyContext( parent context.Context, run func(context.Context) error, signals ...os.Signal, ) error
NotifyContext creates a context that is canceled when one of the specified signals is received, and returns the result of calling run with that context. The result of run is returned as is, regardless of whether it is an error.
The function takes a parent context, a function to run with the created context, and a list of signals to watch for. When one of these signals is received, the context is canceled with a Canceled error that wraps the signal.
FromContext can be used with the context to retrieve the signal that caused cancellation. context.Cause can also be used to retrieve the signal within Canceled.
func Wait ¶
Wait waits for the specified OS signals or context cancellation. It returns the received signal or nil if the context is canceled.
If no signals are provided, all incoming signals will be relayed. Otherwise, only the provided signals will be monitored.
Multiple calls to Wait with the same signals are allowed and will work correctly: each call will receive copies of incoming signals independently.
Types ¶
type Canceled ¶ added in v1.2.0
type Canceled struct {
// contains filtered or unexported fields
}
Canceled is an error that wraps context.Canceled and includes the OS signal that caused the cancellation.
It implements the error interface and provides methods to access the underlying signal and unwrap to context.Canceled.
func (Canceled) Error ¶ added in v1.2.0
Error returns the error message, which includes the context.Canceled error and the string representation of the signal.