# meshapi/go-shutdown architecture

> Architecture diagram of the GitHub repository meshapi/go-shutdown, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/meshapi/go-shutdown
- Repository: https://github.com/meshapi/go-shutdown

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "External Triggers"
        Trigger_Manual["Manual Trigger (Trigger(context.Context))"]:::external
        Trigger_SIG["OS SIGTERM Listener (WaitForInterrupt)"]:::external
    end

    Trigger_Manual -->|"initiates"| Shutdown_Manager
    Trigger_SIG -->|"initiates"| Shutdown_Manager

    Shutdown_Manager["Shutdown Manager"]:::core
    Shutdown_Manager -->|"executes"| Group_Concurrent["Concurrent Handlers (AddSteps)"]:::step
    Group_Concurrent -->|"then"| Group_Sequential["Sequential Handlers (AddSequence)"]:::step
    Group_Sequential -->|"then"| Group_Parallel["Parallel Sequence Handlers (AddParallelSequence)"]:::step
    Group_Parallel -->|"finalize"| Timeout_Config["Configuration & Timeout Options"]:::config

    Shutdown_Manager <-->|"logs"| Logger["Logger Integration"]:::logger

    click Shutdown_Manager "https://github.com/meshapi/go-shutdown/blob/main/default.go"
    click Shutdown_Manager "https://github.com/meshapi/go-shutdown/blob/main/pipeline.go"
    click Shutdown_Manager "https://github.com/meshapi/go-shutdown/blob/main/types.go"

    click Group_Concurrent "https://github.com/meshapi/go-shutdown/blob/main/pipeline.go"
    click Group_Sequential "https://github.com/meshapi/go-shutdown/blob/main/pipeline.go"
    click Group_Parallel "https://github.com/meshapi/go-shutdown/blob/main/pipeline.go"

    click Trigger_Manual "https://github.com/meshapi/go-shutdown/blob/main/pipeline.go"
    click Trigger_SIG "https://github.com/meshapi/go-shutdown/blob/main/pipeline.go"

    click Logger "https://github.com/meshapi/go-shutdown/blob/main/logger.go"

    click Timeout_Config "https://github.com/meshapi/go-shutdown/blob/main/default.go"
    click Timeout_Config "https://github.com/meshapi/go-shutdown/blob/main/types.go"

    classDef external fill:#f4d03f,stroke:#8e44ad,stroke-width:2px;
    classDef core fill:#85c1e9,stroke:#2e86c1,stroke-width:2px;
    classDef step fill:#82e0aa,stroke:#229954,stroke-width:2px;
    classDef config fill:#f5b7b1,stroke:#c0392b,stroke-width:2px;
    classDef logger fill:#d7bde2,stroke:#8e44ad,stroke-width:2px;
```
