Documentation
¶
Index ¶
- Constants
- func DefaultShutdownSignals() []os.Signal
- func DrainMiddleware(drainer *Drainer) func(http.Handler) http.Handler
- func ForceExit(code int)
- func IsPanic(err error) bool
- func IsTimeout(err error) bool
- func ShutdownReadyMiddleware(manager *Manager) func(http.Handler) http.Handler
- func WithTimeout(ctx context.Context, timeout time.Duration, name string, ...) error
- func WithTimeoutAndPanicRecovery(ctx context.Context, timeout time.Duration, name string, ...) (err error)
- type Config
- type Drainer
- type HTTPDrainer
- type Hook
- type HookFunc
- type HookResult
- type Manager
- func (m *Manager) Config() Config
- func (m *Manager) Done() <-chan struct{}
- func (m *Manager) Errors() []error
- func (m *Manager) HookCount() int
- func (m *Manager) IsShutdown() bool
- func (m *Manager) IsShuttingDown() bool
- func (m *Manager) ListenForSignals() <-chan struct{}
- func (m *Manager) Register(name string, priority int, fn HookFunc)
- func (m *Manager) RegisterHook(hook Hook)
- func (m *Manager) Shutdown()
- func (m *Manager) ShutdownWithContext(ctx context.Context)
- func (m *Manager) State() State
- func (m *Manager) Wait()
- type PanicError
- type Registry
- type SignalHandler
- type State
- type TimeoutError
Constants ¶
const ( // PriorityHTTPServer is used for HTTP server shutdown (stop accepting new connections). PriorityHTTPServer = 90 // PriorityBackgroundWorkers is used for background worker shutdown. PriorityBackgroundWorkers = 80 // PriorityDatabase is used for database connection shutdown. PriorityDatabase = 70 // PriorityCache is used for cache connection shutdown. PriorityCache = 60 // PriorityMetrics is used for metrics/logging flush. PriorityMetrics = 50 )
Standard priorities for shutdown hooks (higher = earlier execution).
Variables ¶
This section is empty.
Functions ¶
func DefaultShutdownSignals ¶
DefaultShutdownSignals returns the default signals used for graceful shutdown.
func DrainMiddleware ¶
DrainMiddleware is a middleware that tracks requests for graceful shutdown.
func ForceExit ¶
func ForceExit(code int)
ForceExit terminates the process immediately with the given exit code. This should only be used when graceful shutdown fails completely.
func ShutdownReadyMiddleware ¶
ShutdownReadyMiddleware returns a middleware that checks if the manager is shutting down and returns 503 for the /health/ready endpoint.
Types ¶
type Config ¶
type Config struct {
// OverallTimeout is the maximum time allowed for all shutdown hooks to complete.
// Default: 30 seconds.
OverallTimeout time.Duration
// PerHookTimeout is the maximum time allowed for a single hook to complete.
// Default: 10 seconds.
PerHookTimeout time.Duration
// DrainTimeout is the time to wait for in-flight requests to complete.
// Default: 10 seconds.
DrainTimeout time.Duration
// SlowHookThreshold is the duration after which a hook is considered slow
// and a warning is logged.
// Default: 5 seconds.
SlowHookThreshold time.Duration
}
Config holds configuration for the shutdown manager.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns the default shutdown configuration.
type Drainer ¶
type Drainer struct {
// contains filtered or unexported fields
}
Drainer tracks and waits for in-flight operations to complete.
func (*Drainer) Add ¶
Add increments the count of in-flight operations. Returns false if draining has started.
func (*Drainer) IsDraining ¶
IsDraining returns true if draining has started.
func (*Drainer) StartDrain ¶
func (d *Drainer) StartDrain()
StartDrain signals that draining has started. New operations will be rejected after this call.
type HTTPDrainer ¶
type HTTPDrainer struct {
// contains filtered or unexported fields
}
HTTPDrainer wraps an HTTP handler to track in-flight requests.
func NewHTTPDrainer ¶
func NewHTTPDrainer(handler http.Handler) *HTTPDrainer
NewHTTPDrainer creates a new HTTP drainer wrapping the given handler.
func (*HTTPDrainer) ActiveRequests ¶
func (d *HTTPDrainer) ActiveRequests() int64
ActiveRequests returns the number of in-flight requests.
func (*HTTPDrainer) IsDraining ¶
func (d *HTTPDrainer) IsDraining() bool
IsDraining returns true if draining has started.
func (*HTTPDrainer) ServeHTTP ¶
func (d *HTTPDrainer) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP handles HTTP requests, tracking them for draining.
func (*HTTPDrainer) StartDrain ¶
func (d *HTTPDrainer) StartDrain()
StartDrain starts draining connections.
type Hook ¶
type Hook struct {
// Name identifies the hook for logging purposes.
Name string
// Priority determines execution order. Higher priorities execute first.
// Use the Priority* constants for standard components.
Priority int
// Fn is the shutdown function to execute.
Fn HookFunc
}
Hook represents a shutdown hook with metadata.
type HookFunc ¶
HookFunc is a function that performs shutdown logic. It receives a context that will be canceled when the hook timeout expires.
type HookResult ¶
type HookResult struct {
Name string
Priority int
Error error
Duration interface{} // time.Duration, but stored as interface for flexibility
}
HookResult contains the result of executing a shutdown hook.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager coordinates graceful shutdown of all registered components.
func NewManager ¶
NewManager creates a new shutdown manager with the given configuration.
func NewManagerWithDefaults ¶
func NewManagerWithDefaults() *Manager
NewManagerWithDefaults creates a shutdown manager with default configuration.
func (*Manager) Done ¶
func (m *Manager) Done() <-chan struct{}
Done returns a channel that is closed when shutdown is complete.
func (*Manager) IsShutdown ¶
IsShutdown returns true if shutdown is complete.
func (*Manager) IsShuttingDown ¶
IsShuttingDown returns true if shutdown is in progress.
func (*Manager) ListenForSignals ¶
func (m *Manager) ListenForSignals() <-chan struct{}
ListenForSignals starts listening for OS shutdown signals and triggers shutdown when a signal is received. Returns a channel that is closed when shutdown is complete.
func (*Manager) Register ¶
Register adds a shutdown hook with the given name, priority, and function. Higher priority hooks are executed first.
func (*Manager) RegisterHook ¶
RegisterHook adds a Hook struct to the registry.
func (*Manager) Shutdown ¶
func (m *Manager) Shutdown()
Shutdown initiates graceful shutdown. It is safe to call multiple times. The first call triggers the shutdown, subsequent calls are no-ops.
func (*Manager) ShutdownWithContext ¶
ShutdownWithContext initiates graceful shutdown with an external context.
type PanicError ¶
type PanicError struct {
Operation string
Value interface{}
}
PanicError is returned when a shutdown hook panics.
func (*PanicError) Error ¶
func (e *PanicError) Error() string
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry manages shutdown hooks.
func (*Registry) RegisterHook ¶
RegisterHook adds a Hook struct to the registry.
type SignalHandler ¶
type SignalHandler struct {
// contains filtered or unexported fields
}
SignalHandler manages OS signal handling for graceful shutdown.
func NewSignalHandler ¶
func NewSignalHandler(signals ...os.Signal) *SignalHandler
NewSignalHandler creates a new signal handler that listens for the specified signals. If no signals are provided, it defaults to SIGTERM, SIGINT, and SIGQUIT.
func (*SignalHandler) Listen ¶
func (h *SignalHandler) Listen() <-chan os.Signal
Listen starts listening for shutdown signals. It returns a channel that will receive the signal when one is caught. The signal channel will only deliver one signal before being closed.
func (*SignalHandler) Signals ¶
func (h *SignalHandler) Signals() []os.Signal
Signals returns the list of signals being monitored.
func (*SignalHandler) Stop ¶
func (h *SignalHandler) Stop()
Stop stops listening for signals and cleans up.
type TimeoutError ¶
TimeoutError is returned when a shutdown operation times out.
func (*TimeoutError) Error ¶
func (e *TimeoutError) Error() string