How meshapi/go-shutdown works
Main Components
• Shutdown Manager (Pipeline): This is the core component that organizes and coordinates the shutdown steps. Files like pipeline.go, default.go, and types.go indicate the existence of a manager that groups handlers into sequential steps and parallel groups.
• Shutdown Handlers / Steps: These handlers represent individual shutdown tasks that are configured using methods like AddSteps, AddSequence, and AddParallelSequence. They encapsulate any clean-up operation (e.g., shutting down an HTTP server, finishing logging, etc.).
• Trigger Mechanism: The project supports both manual triggering (via Trigger function) and signal-based triggering using WaitForInterrupt. The latter listens for SIGTERM signals from the underlying OS.
• Logger Integration: A logging interface is supported (see logger.go), where any logger implementing Info and Error can be set. This decouples logging from the shutdown mechanism.
• Configuration Options & Timeouts: The package supports configurable timeouts and a completion callback for additional flexibility in handling shutdown completion (even in case of errors or panics).
Relationships and Interactions
• Signal or Manual Trigger → Shutdown Manager: The shutdown process is initiated either by an external signal (using WaitForInterrupt) or manually by calling Trigger().
• Shutdown Manager → Execution Pipeline: Once triggered, the manager executes configured shutdown steps. There is a grouping mechanism; handlers can run concurrently if added in a group via AddSteps, or sequentially via AddSequence.
• Shutdown Steps → Logger: Each shutdown step can log start/finish messages or errors by using the configured Logger.
• Completion Callback → Finalization: Once all shutdown steps are executed (subject to any timeouts), an optional completion function is called to perform any final clean-up tasks.
Architectural Patterns and Design Principles
• Pipeline/Chain of Responsibility: The shutdown process is built as a pipeline where tasks are added and executed either concurrently or sequentially.
• Decoupling via Interfaces: The logger is passed as an interface type, promoting loose coupling.
• Concurrency Management: The design supports configurable concurrency by grouping handlers, which demonstrates careful management of parallel and sequential execution of tasks.