signal

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package signal provides a stateful signal context with introspection and LIFO hooks.

It solves two main problems for CLI applications:

  1. Distinguishing between SIGINT (soft interrupt) and SIGTERM (hard termination).
  2. Managing cleanup operations (Hooks) in a reliable, observable way.

Dual Signal Handling

Unlike standard signal.NotifyContext, this package allows configuring SIGINT to initiate a graceful shutdown logic rather than immediate cancellation. Repeated signals can trigger a "Force Exit" (os.Exit(1)) to prevent hung processes.

Lifecycle Hooks

Users can register cleanup functions via Context.OnShutdown. These hooks execution is guaranteed to run in LIFO (Last-In-First-Out) order, simulating `defer`.

Introspection & Visualization

The package implements an "Introspection Pattern". The Context.State method returns a complete snapshot of the configuration. The [Mermaid] function can consume this state to generate a visual representation of the lifecycle policy for documentation. The MermaidState function can consume this state.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MermaidState added in v1.3.0

func MermaidState(s State) string

MermaidState returns a Mermaid state diagram string representing the lifecycle configuration.

Types

type Context

type Context struct {
	context.Context
	Cancel func()
	// contains filtered or unexported fields
}

Context wraps a context and captures the signal that cancelled it.

func NewContext

func NewContext(parent context.Context, opts ...Option) *Context

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

func (sc *Context) Signal() os.Signal

Signal returns the signal that caused the context to be cancelled/interrupted, or nil.

func (*Context) State added in v1.2.0

func (sc *Context) State() State

State returns a snapshot of the current configuration.

func (*Context) Stop added in v1.1.0

func (sc *Context) Stop()

Stop stops the signal monitoring and restores default behavior. It also ensures Wait() unblocks if called.

func (*Context) Wait added in v1.1.0

func (sc *Context) Wait()

Wait blocks until all registered hooks have completed execution. This is essential to prevent the main function from exiting before cleanup is done.

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

func WithForceExit(threshold int) Option

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

func WithHookTimeout(d time.Duration) Option

WithHookTimeout configures the duration after which a running hook produces a warning log. Default is 5 seconds.

func WithInterrupt added in v1.1.0

func WithInterrupt(cancel bool) Option

WithInterrupt configures whether SIGINT (Ctrl+C) should cancel the context. Default is true.

type State added in v1.2.0

type State struct {
	InterruptCancel    bool
	ForceExitThreshold int
	HookTimeout        time.Duration
	Received           os.Signal
	Stopping           bool
}

State represents the configuration state of the SignalContext.

Jump to

Keyboard shortcuts

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