Documentation
¶
Overview ¶
Package shutdown provides graceful shutdown support by listening for SIGINT/SIGTERM signals with a configurable timeout and ordered teardown.
Package shutdown provides graceful shutdown support by listening for SIGINT/SIGTERM signals with a configurable timeout.
Index ¶
Constants ¶
const DefaultTimeout = 30 * time.Second
DefaultTimeout is the default graceful shutdown timeout.
Variables ¶
This section is empty.
Functions ¶
func NotifyContext ¶
NotifyContext returns a context that is canceled when an OS shutdown signal is received (per-OS set: SIGINT+SIGTERM on Unix, os.Interrupt elsewhere). Callers must call the returned cancel func to release resources.
This is the cross-platform equivalent of signal.NotifyContext(parent, syscall.SIGINT, syscall.SIGTERM) — the latter is incorrect on Windows because Windows cannot deliver SIGTERM, leaving the registration silently half-broken.
Types ¶
type Hook ¶
Hook is a function that runs during graceful shutdown. It receives a context that will be canceled after the shutdown timeout expires.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager coordinates graceful shutdown.
func (*Manager) Register ¶
Register adds a hook that will be called during shutdown. Hooks are called in LIFO order (last registered, first executed).
func (*Manager) Shutdown ¶
Shutdown runs all hooks immediately with a timeout-bounded context. Useful for programmatic shutdown (e.g., in tests).
func (*Manager) Wait ¶
Wait blocks until SIGINT or SIGTERM is received, then runs all hooks with a timeout-bounded context. Returns nil if all hooks reported nil error. Returns a joined error if any hook returned a non-nil error.
Note: if the shutdown timeout expires mid-hook, the per-hook context is canceled. A hook that respects ctx.Done() and returns ctx.Err() will cause Wait to return that error via the hook's error. If all hooks return nil (they completed before the deadline), Wait returns nil — it does not return context.DeadlineExceeded on its own.