shutdown

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 8 Imported by: 0

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

View Source
const DefaultTimeout = 30 * time.Second

DefaultTimeout is the default graceful shutdown timeout.

Variables

This section is empty.

Functions

func NotifyContext

func NotifyContext(parent context.Context) (context.Context, context.CancelFunc)

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

type Hook func(ctx context.Context) error

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 New

func New(opts ...Option) *Manager

New creates a Manager with the given options.

func (*Manager) Register

func (m *Manager) Register(h Hook)

Register adds a hook that will be called during shutdown. Hooks are called in LIFO order (last registered, first executed).

func (*Manager) Shutdown

func (m *Manager) Shutdown() error

Shutdown runs all hooks immediately with a timeout-bounded context. Useful for programmatic shutdown (e.g., in tests).

func (*Manager) Wait

func (m *Manager) Wait() error

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.

type Option

type Option func(*Manager)

Option configures a Manager.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the shutdown timeout.

Jump to

Keyboard shortcuts

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