Documentation
¶
Overview ¶
Package signal provides a stateful signal context for interactive CLI applications.
Unlike the standard signal.NotifyContext which cancels on the first signal, this package distinguishes between SIGINT (soft interrupt) and SIGTERM (hard stop).
This allows applications to implement patterns like "Press Ctrl+C again to exit" or to prompt for confirmation before shutting down.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
Context wraps a context and captures the signal that cancelled it.
func NewContext ¶
NewContext creates a context that is cancelled on SIGTERM or SIGINT (standard termination). On the first signal received, it cancels the context to initiate graceful shutdown (if configured). If a second signal is received before the program exits, it performs an immediate os.Exit(1) (if configured).
func (*Context) OnShutdown ¶ added in v1.1.0
func (sc *Context) OnShutdown(f func())
OnShutdown registers a function to be called when the context receives a shutdown signal. Hooks are executed in LIFO (Last-In-First-Out) order, simulating `defer`. Execution happens asynchronously after the context is cancelled. Call Wait() to block until all hooks (including those registered dynamically) have finished.
func (*Context) Signal ¶
Signal returns the signal that caused the context to be cancelled/interrupted, or nil.
type Option ¶ added in v1.1.0
type Option func(*options)
Option is a functional option for configuring signal behavior.
func WithForceExit ¶ added in v1.1.0
WithForceExit configures the threshold of signals required to trigger an immediate os.Exit(1). Set to 0 to disable forced exit. Default is 2.
func WithHookTimeout ¶ added in v1.1.0
WithHookTimeout configures the duration after which a running hook produces a warning log. Default is 5 seconds.
func WithInterrupt ¶ added in v1.1.0
WithInterrupt configures whether SIGINT (Ctrl+C) should cancel the context. Default is true.