# phuslu/fastdns architecture

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

- Interactive diagram: https://gitdiagram.com/phuslu/fastdns
- Repository: https://github.com/phuslu/fastdns

## Overview

##### Project Type Analysis
This is a specialized DNS implementation library in Go, featuring both client and server components with a focus on high performance and zero allocations. The project includes DNS and DoH (DNS over HTTPS) capabilities.

##### Main Components

a. Core Components:
- DNS Server Component
- DNS Client Component
- DoH Server Component
- DoH Client Component
- Worker Pool System
- Message Pool System

b. Supporting Components:
- Stats Collection System
- Record Handling System
- Message Processing Pipeline

**┌─ Client Interfaces ─────┐    ┌─ Server Interfaces ────┐**
- DNS Client           │    │ - DNS Server           │
- DoH Client          │    │ - DoH Server           │
- ┘    └──────────────────────────┘

**┌─ Message Processing ──────────────────────┐**
- ┌─ Parser ─┐  ┌─ Handler ─┐  ┌─ Writer ─┐│
- (0-alloc) │→ │          │→ │(0-alloc) ││
- ┘  └──────────┘  └──────────┘│
- ┘

**┌─ Pool System ────┐    ┌─ Performance Features ─┐**
- Worker Pool    │    │ - Prefork             │
- Message Pool   │    │ - Reuse Port          │
- ┘    │ - CPU Affinity        │
- ┘

##### Key Elements to Highlight

- Blue rectangles for main components
- Yellow diamonds for decision points
- Red outlines for critical performance components

##### Specific Implementation Details

Annotations for:
- Zero-allocation implementations
- Performance optimization points
- Metrics collection points
- Protocol support (DNS, DoH)

##### Connection Flow Representation

Following flows:
**Client Request → Parser → Handler → Writer → Response**
- ↕         ↕         ↕
- Message Pool  Worker Pool  Stats

##### Performance Metrics

A section showing:
- Benchmark results
- QPS capabilities
- Memory allocation statistics

- High performance through zero allocations
- Worker pool and message pool optimization
- Prefork and port reuse capabilities
- Compatibility with standard interfaces

Remember to maintain a balance between showing the high-level architecture and the performance-critical implementation details that make this DNS implementation unique.

This system design should clearly communicate both the overall architecture and the performance optimization features that allow it to achieve 1.4M QPS on a single machine.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "External Interface Layer"
        DNS_Client["DNS Client"]
        DoH_Client["DoH Client"]
        DNS_Server["DNS Server"]
        DoH_Server["DoH Server"]
    end

    subgraph "Message Processing Layer"
        Parser["Parser\n(Zero Allocation)"]
        Handler["Request Handler"]
        Writer["Message Writer\n(Zero Allocation)"]
    end

    subgraph "Resource Management Layer"
        WorkerPool["Worker Pool"]
        MessagePool["Message Pool"]
        PerfFeatures["Performance Features"]
    end

    subgraph "Performance Optimizations"
        Prefork["Prefork"]
        ReusePort["Port Reuse"]
        CPUAffinity["CPU Affinity"]
    end

    subgraph "Record Management"
        RecordHandler["Record Handler"]
        RecordTypes["DNS Record Types\n(A, AAAA, CNAME)"]
    end

    subgraph "Monitoring"
        Stats["Stats Collection"]
    end

    %% Connections
    DNS_Client --> Parser
    DoH_Client --> Parser
    Parser --> Handler
    Handler --> Writer
    Writer --> DNS_Server
    Writer --> DoH_Server
    
    Handler <--> WorkerPool
    Parser <--> MessagePool
    Writer <--> MessagePool
    
    Handler <--> RecordHandler
    RecordHandler --> RecordTypes
    
    PerfFeatures --> Prefork
    PerfFeatures --> ReusePort
    PerfFeatures --> CPUAffinity
    
    Handler --> Stats
    Writer --> Stats

    %% Click Events
    click DNS_Client "https://github.com/phuslu/fastdns/blob/master/client.go"
    click DNS_Server "https://github.com/phuslu/fastdns/blob/master/server.go"
    click DoH_Server "https://github.com/phuslu/fastdns/blob/master/cmd/fastdoh/fastdoh.go"
    click WorkerPool "https://github.com/phuslu/fastdns/blob/master/workerpool.go"
    click MessagePool "https://github.com/phuslu/fastdns/blob/master/message.go"
    click Stats "https://github.com/phuslu/fastdns/blob/master/stats.go"
    click RecordHandler "https://github.com/phuslu/fastdns/blob/master/record.go"
    click Writer "https://github.com/phuslu/fastdns/blob/master/writer.go"
    click Handler "https://github.com/phuslu/fastdns/blob/master/handler.go"

    %% Styling
    classDef external fill:#a8d1ff,stroke:#333
    classDef processing fill:#ffe0a8,stroke:#333
    classDef resource fill:#a8ffd1,stroke:#333
    classDef perf fill:#ffa8a8,stroke:#333
    classDef record fill:#d1a8ff,stroke:#333
    classDef monitoring fill:#f0f0f0,stroke:#333

    class DNS_Client,DoH_Client,DNS_Server,DoH_Server external
    class Parser,Handler,Writer processing
    class WorkerPool,MessagePool,PerfFeatures resource
    class Prefork,ReusePort,CPUAffinity perf
    class RecordHandler,RecordTypes record
    class Stats monitoring
```
