# riadizur/mac-parallel-downloader architecture

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

- Interactive diagram: https://gitdiagram.com/riadizur/mac-parallel-downloader
- Repository: https://github.com/riadizur/mac-parallel-downloader

## Overview

##### Project Type and Purpose
- This is a standalone, command-line interface (CLI) tool written in Go for high-performance parallel file downloading.
- It leverages HTTP range requests to split a large file into N parts, downloads them concurrently, then merges them into a single output file while displaying real-time progress metrics.

##### Key Components
a. CLI Layer
- Parses command-line arguments: `<url>`, `<output_filename>`, `<part_count>`.
- Invokes the download coordinator.
b. Download Coordinator (main.go)
- Issues an HTTP HEAD request to retrieve Content-Length.
- Calculates byte ranges for each part.
- Spawns N goroutines (one per part).
c. Concurrent Download Workers (goroutines)
- Each worker performs an HTTP GET with the appropriate `Range` header.
- Streams data directly to a temporary `.partX` file on disk.
- Reports per-part progress updates via a channel.
d. Progress Bar Aggregator (`mpb` library)
- Listens for progress updates.
- Renders aligned, real-time progress bars for each part in the console.
e. Merger
- Waits for all workers to complete.
- Opens each `.partX` in sequence; writes/concatenates into the final `<output_filename>`.
- Cleans up intermediate files.
f. Metrics Reporter
- Calculates total download time and average speed.
- Prints final summary to stdout.

##### External Dependencies and Interactions
- HTTP Server(s): hosting the target file (supports Range requests).
- Local File System: for writing parts and final merged file.
- `mpb` Go library: for console‐based progress bars.

##### Relationships and Data Flow
- User → CLI Layer → Coordinator
- Coordinator → HTTP HEAD → HTTP Server → Coordinator
- Coordinator → Worker Goroutines → HTTP GET → HTTP Server
- Workers → Temp Files
- Workers → Progress Aggregator
- Coordinator → Merger → Temp Files → Final File
- Coordinator → Metrics Reporter → Console Output

##### Architectural Patterns and Principles
- Concurrency via goroutines and channels.
- Single responsibility per component (parsing, networking, progress UI, merging).
- Pipeline style: split → download → aggregate → merge → report.
- Clean separation between I/O (network/disk) and UI (progress display).

##### Essential Elements Checklist
- Concurrency note: annotate the worker container with “N goroutines”.

## Diagram (Mermaid)

```mermaid
flowchart TD
    U["User CLI Invocation"]:::external
    CLI["CLI Layer<br>(Argument Parsing)"]:::internal
    Coordinator["Download Coordinator"]:::internal
    subgraph "Parallel Download Workers<br>(N goroutines)"
        Worker["Worker Goroutine"]:::internal
        WorkerCont["..."]:::internal
    end
    HTTP["HTTP Server<br>(Range Support)"]:::network
    Temp[".partX Files (Temp Storage)"]:::disk
    Final["Final Output File"]:::disk
    Progress["Progress Bar Aggregator<br>(mpb)"]:::ui
    Merger["Merger"]:::internal
    Metrics["Metrics Reporter"]:::ui
    Binary["Compiled Binary<br>(CLI Executable)"]:::external
    Manifest["Dependency Manifest"]:::external
    Docs["Project Documentation"]:::external

    U -->|"run downloader"| CLI
    CLI -->|"invoke"| Coordinator
    Coordinator -->|"HTTP HEAD"| HTTP
    Coordinator -->|"spawn goroutines"| Worker
    Worker -->|"HTTP GET Range"| HTTP
    Worker -->|"Write .partX"| Temp
    Worker -->|"Progress updates"| Progress
    Progress -->|"renders bars"| U
    Coordinator -->|"wait for all"| Merger
    Merger -->|"read each .partX"| Temp
    Merger -->|"write final file"| Final
    Merger -->|"cleanup temp files"| Temp
    Coordinator -->|"report metrics"| Metrics
    Metrics -->|"print summary"| U

    click CLI "https://github.com/riadizur/mac-parallel-downloader/blob/main/main.go"
    click Coordinator "https://github.com/riadizur/mac-parallel-downloader/blob/main/main.go"
    click Worker "https://github.com/riadizur/mac-parallel-downloader/blob/main/main.go"
    click Progress "https://github.com/riadizur/mac-parallel-downloader/blob/main/main.go"
    click Progress "https://github.com/riadizur/mac-parallel-downloader/blob/main/go.mod"
    click Progress "https://github.com/riadizur/mac-parallel-downloader/blob/main/go.sum"
    click Merger "https://github.com/riadizur/mac-parallel-downloader/blob/main/main.go"
    click Metrics "https://github.com/riadizur/mac-parallel-downloader/blob/main/main.go"
    click Binary "https://github.com/riadizur/mac-parallel-downloader/tree/main/bin/downloader"
    click Manifest "https://github.com/riadizur/mac-parallel-downloader/blob/main/go.mod"
    click Manifest "https://github.com/riadizur/mac-parallel-downloader/blob/main/go.sum"
    click Docs "https://github.com/riadizur/mac-parallel-downloader/blob/main/readme.md"

    subgraph Legend
        I1["Internal Component"]:::internal
        N1["Network Component"]:::network
        D1["Disk Storage"]:::disk
        U1["UI Component"]:::ui
        E1["External Entity"]:::external
    end

    classDef internal fill:#D6E9FE,stroke:#4A90E2,color:#000;
    classDef network fill:#DFF0D8,stroke:#3C763D,color:#000;
    classDef disk fill:#FCF8E3,stroke:#8A6D3B,color:#000;
    classDef ui fill:#FAEBE4,stroke:#EB984E,color:#000;
    classDef external fill:#F2DEDE,stroke:#A94442,color:#000;
```
