# sneako/finch architecture

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

- Interactive diagram: https://gitdiagram.com/sneako/finch
- Repository: https://github.com/sneako/finch

## Overview

##### Main Components

a. Client Interface Layer:
- Entry point through `Finch` module
- Request builder and response handlers
- Configuration management

b. Pool Management Layer:
- Pool Manager (handles connection pool lifecycle)
- Pool implementations (HTTP/1 and HTTP/2)
- Connection pooling strategies

c. Protocol Handlers:
- HTTP/1 implementation
- HTTP/2 implementation
- SSL/TLS handling

d. Monitoring & Instrumentation:
- Telemetry integration
- Pool metrics
- Error handling

**┌─────────────────────────────────────────┐**
- Client Application           │
- ┘
- ↓
**┌─────────────────────────────────────────┐**
- Finch Public API              │
- (build/request/start_link interfaces)  │
- ┘
- ↓
**┌─────────────────────────────────────────┐**
- Pool Manager Layer             │
- ┐        ┌───────────────┤
- HTTP/1 Pool  │        │  HTTP/2 Pool  │
- ┘        └───────────────┘
- ↓
**┌─────────────────────────────────────────┐**
- Protocol Handlers               │
- ┐        ┌───────────────┤
- HTTP/1 Handler │        │HTTP/2 Handler │
- ┘        └───────────────┘
- ↓
**┌─────────────────────────────────────────┐**
- Mint (HTTP Client)         │
- ┘

##### Important Relationships to Highlight

- How requests flow from the public API through the pool manager to the appropriate protocol handler
- Relationship between pools and their respective protocol implementations
- Demonstrate how telemetry events are emitted at various points
- Connection pooling strategy with multiple instances

##### Additional Elements

A separate section showing:
- Configuration options
- Telemetry events
- SSL/TLS handling
- Pool metrics collection points

##### Key Architectural Patterns to Emphasize

- Pool-based connection management
- Protocol-specific implementations
- Supervisor tree structure
- Event-based monitoring

##### Implementation Details

- `lib/finch.ex` as the main entry point
- `lib/finch/http1` and `lib/finch/http2` as separate protocol implementations
- `lib/finch/pool.ex` and `lib/finch/pool_manager.ex` for connection management
- `lib/finch/telemetry.ex` for instrumentation

- Keep the main flow clear and centered
- Pool configuration examples
- Error handling paths
- Demonstrate how the library integrates with external applications
- Notes about performance optimization points
- Connection pooling strategy as it's a key feature

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        App[Client Application]
    end

    subgraph Interface
        Finch["Finch Interface"]:::interface
        Request["Request Handler"]:::interface
        Response["Response Handler"]:::interface
        Error["Error Handler"]:::interface
    end

    subgraph PoolManagement
        PoolManager["Pool Manager"]:::pool
        BasePool["Base Pool"]:::pool
        HTTP1Pool["HTTP/1 Pool"]:::pool
        HTTP2Pool["HTTP/2 Pool"]:::pool
    end

    subgraph ProtocolHandlers
        HTTP1Conn["HTTP/1 Connection"]:::protocol
        HTTP2Stream["HTTP/2 Stream"]:::protocol
        SSL["SSL Handler"]:::protocol
    end

    subgraph Monitoring
        Telemetry["Telemetry"]:::monitoring
        HTTP1Metrics["HTTP/1 Metrics"]:::monitoring
        HTTP2Metrics["HTTP/2 Metrics"]:::monitoring
    end

    subgraph External
        Mint["Mint HTTP Client"]:::external
    end

    %% Relationships
    App --> Finch
    Finch --> Request
    Finch --> Response
    Finch --> Error
    Request --> PoolManager
    Response --> PoolManager
    
    PoolManager --> BasePool
    BasePool --> HTTP1Pool
    BasePool --> HTTP2Pool
    
    HTTP1Pool --> HTTP1Conn
    HTTP2Pool --> HTTP2Stream
    HTTP1Conn --> SSL
    HTTP2Stream --> SSL
    
    HTTP1Pool --> HTTP1Metrics
    HTTP2Pool --> HTTP2Metrics
    
    HTTP1Conn --> Mint
    HTTP2Stream --> Mint
    
    Telemetry -.-> HTTP1Metrics
    Telemetry -.-> HTTP2Metrics

    %% Click Events
    click Finch "https://github.com/sneako/finch/blob/main/lib/finch.ex"
    click Request "https://github.com/sneako/finch/blob/main/lib/finch/request.ex"
    click Response "https://github.com/sneako/finch/blob/main/lib/finch/response.ex"
    click Error "https://github.com/sneako/finch/blob/main/lib/finch/error.ex"
    click PoolManager "https://github.com/sneako/finch/blob/main/lib/finch/pool_manager.ex"
    click BasePool "https://github.com/sneako/finch/blob/main/lib/finch/pool.ex"
    click HTTP1Pool "https://github.com/sneako/finch/blob/main/lib/finch/http1/pool.ex"
    click HTTP2Pool "https://github.com/sneako/finch/blob/main/lib/finch/http2/pool.ex"
    click HTTP1Conn "https://github.com/sneako/finch/blob/main/lib/finch/http1/conn.ex"
    click HTTP2Stream "https://github.com/sneako/finch/blob/main/lib/finch/http2/request_stream.ex"
    click SSL "https://github.com/sneako/finch/blob/main/lib/finch/ssl.ex"
    click Telemetry "https://github.com/sneako/finch/blob/main/lib/finch/telemetry.ex"
    click HTTP1Metrics "https://github.com/sneako/finch/blob/main/lib/finch/http1/pool_metrics.ex"
    click HTTP2Metrics "https://github.com/sneako/finch/blob/main/lib/finch/http2/pool_metrics.ex"

    %% Styling
    classDef interface fill:#2374ab
    classDef pool fill:#2a9d8f
    classDef protocol fill:#e76f51
    classDef monitoring fill:#f4a261
    classDef external fill:#grey

    %% Legend
    subgraph Legend
        L1["Interface Components"]:::interface
        L2["Pool Components"]:::pool
        L3["Protocol Handlers"]:::protocol
        L4["Monitoring Components"]:::monitoring
        L5["External Dependencies"]:::external
    end
```
