shutdown

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package shutdown provides graceful shutdown coordination for control-plane components. It handles SIGTERM/SIGINT signals, stops accepting new requests, waits for in-flight operations to complete, and closes resources cleanly.

**Validates: Requirements 15.1, 15.2, 15.3, 15.4, 15.5**

Index

Constants

View Source
const DefaultTimeout = 30 * time.Second

DefaultTimeout is the default graceful shutdown timeout.

Variables

This section is empty.

Functions

This section is empty.

Types

type CloserComponent

type CloserComponent struct {
	// contains filtered or unexported fields
}

CloserComponent wraps an io.Closer for graceful shutdown.

**Validates: Requirements 15.4**

func NewCloserComponent

func NewCloserComponent(name string, closer io.Closer) *CloserComponent

NewCloserComponent creates a new closer shutdown component.

func (*CloserComponent) Name

func (c *CloserComponent) Name() string

Name returns the component name.

func (*CloserComponent) Shutdown

func (c *CloserComponent) Shutdown(ctx context.Context) error

Shutdown closes the underlying resource.

**Validates: Requirements 15.4**

type Component

type Component interface {
	// Name returns the component name for logging.
	Name() string
	// Shutdown gracefully shuts down the component.
	// It should return within the given context deadline.
	Shutdown(ctx context.Context) error
}

Component represents a component that can be gracefully shut down.

type Coordinator

type Coordinator struct {
	// contains filtered or unexported fields
}

Coordinator manages graceful shutdown of multiple components. It handles SIGTERM/SIGINT signals and coordinates shutdown of registered components.

**Validates: Requirements 15.1, 15.2, 15.3, 15.4, 15.5**

func NewCoordinator

func NewCoordinator(opts ...Option) *Coordinator

NewCoordinator creates a new shutdown coordinator.

func (*Coordinator) ExitCode

func (c *Coordinator) ExitCode() int

ExitCode returns the exit code after shutdown. Returns 0 for clean shutdown, 1 for forced termination.

**Validates: Requirements 15.5**

func (*Coordinator) Register

func (c *Coordinator) Register(component Component)

Register adds a component to be shut down during graceful shutdown. Components are shut down in reverse order of registration (LIFO).

func (*Coordinator) Shutdown

func (c *Coordinator) Shutdown()

Shutdown initiates graceful shutdown of all registered components. It stops accepting new requests, waits for in-flight operations to complete (up to the configured timeout), and closes resources cleanly.

**Validates: Requirements 15.1, 15.2, 15.3, 15.4, 15.5**

func (*Coordinator) Wait

func (c *Coordinator) Wait()

Wait blocks until shutdown is complete.

func (*Coordinator) WaitForSignal

func (c *Coordinator) WaitForSignal()

WaitForSignal blocks until a SIGTERM or SIGINT signal is received, then initiates graceful shutdown.

**Validates: Requirements 15.1**

type FuncComponent

type FuncComponent struct {
	// contains filtered or unexported fields
}

FuncComponent wraps a shutdown function as a component.

func NewFuncComponent

func NewFuncComponent(name string, fn func(ctx context.Context) error) *FuncComponent

NewFuncComponent creates a new function-based shutdown component.

func (*FuncComponent) Name

func (c *FuncComponent) Name() string

Name returns the component name.

func (*FuncComponent) Shutdown

func (c *FuncComponent) Shutdown(ctx context.Context) error

Shutdown calls the wrapped function.

type GRPCServerComponent

type GRPCServerComponent struct {
	// contains filtered or unexported fields
}

GRPCServerComponent wraps a gRPC server for graceful shutdown.

func NewGRPCServerComponent

func NewGRPCServerComponent(name string, server GRPCServerShutdowner) *GRPCServerComponent

NewGRPCServerComponent creates a new gRPC server shutdown component.

func (*GRPCServerComponent) Name

func (c *GRPCServerComponent) Name() string

Name returns the component name.

func (*GRPCServerComponent) Shutdown

func (c *GRPCServerComponent) Shutdown(ctx context.Context) error

Shutdown gracefully stops the gRPC server.

type GRPCServerShutdowner

type GRPCServerShutdowner interface {
	GracefulStop()
}

GRPCServerShutdowner is the interface for gRPC servers that can be gracefully stopped.

type HTTPServerComponent

type HTTPServerComponent struct {
	// contains filtered or unexported fields
}

HTTPServerComponent wraps an http.Server for graceful shutdown.

**Validates: Requirements 15.1, 15.2, 15.3**

func NewHTTPServerComponent

func NewHTTPServerComponent(name string, server *http.Server) *HTTPServerComponent

NewHTTPServerComponent creates a new HTTP server shutdown component.

func (*HTTPServerComponent) Name

func (c *HTTPServerComponent) Name() string

Name returns the component name.

func (*HTTPServerComponent) Shutdown

func (c *HTTPServerComponent) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the HTTP server. It stops accepting new connections and waits for in-flight requests to complete.

**Validates: Requirements 15.1, 15.2, 15.3**

type Option

type Option func(*Coordinator)

Option configures a Coordinator.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger.

func WithSignalChannel

func WithSignalChannel(ch chan os.Signal) Option

WithSignalChannel sets a custom signal channel (for testing).

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the shutdown timeout.

type WorkerComponent

type WorkerComponent struct {
	// contains filtered or unexported fields
}

WorkerComponent wraps a worker for graceful shutdown.

func NewWorkerComponent

func NewWorkerComponent(name string, worker WorkerShutdowner) *WorkerComponent

NewWorkerComponent creates a new worker shutdown component.

func (*WorkerComponent) Name

func (c *WorkerComponent) Name() string

Name returns the component name.

func (*WorkerComponent) Shutdown

func (c *WorkerComponent) Shutdown(ctx context.Context) error

Shutdown stops the worker and waits for in-progress jobs to complete.

type WorkerShutdowner

type WorkerShutdowner interface {
	Stop()
}

WorkerShutdowner is the interface for workers that can be stopped.

Jump to

Keyboard shortcuts

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