# lhd-creator/fastserver architecture

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

- Interactive diagram: https://gitdiagram.com/lhd-creator/fastserver
- Repository: https://github.com/lhd-creator/fastserver

## Overview

##### Project Type and Purpose
- This is a C++ concurrent‐server framework, modeled after muduo, targeting Linux.
- It provides a Reactor‐based core library for building high‐performance network services (TCP, HTTP, timers, thread pool), plus several examples (HTTP server, echo server, timer wheel).

##### File Structure Analysis
- Top‐level directories:
- Example/… – standalone demo apps (Any, Timer, Timewheel)
- source/… – core library headers & source, subfolders for Http module, tests, examples, echo and WebBench.
- Key modules in source/:
- EventLoop.hpp, Poller.hpp, Channel.hpp – Reactor core
- TcpServer.hpp, Connection.hpp, TcpSocket.hpp – TCP abstraction
- TimerWheel.hpp, LoopThreadPool.hpp – scheduling & concurrency
- Http/… – HTTP client/server examples & wwwroot
- examples under source/example – repeat of top-level Example for reference

##### Core Components
a. EventLoop
b. Poller (epoll abstraction)
c. Channel (file‐descriptor wrapper & callback registry)
d. TcpServer (listens for new connections)
e. Connection (manages a single client session)
f. TimerWheel (manages scheduled callbacks)
g. LoopThreadPool (multiple EventLoop threads)
h. HTTP Module (parsing, request/response handling)
i. Example Applications (Echo, HTTP server, Timer tests)

##### Component Interactions & Data Flows
- OS/epoll → Poller
- Poller → EventLoop
- EventLoop dispatches via Channel to:
- Connection callbacks (read/write/close)
- TimerWheel callbacks (timeout events)
- New client connections: Poller triggers TcpServer → creates Connection → registers with EventLoop
- TimerWheel schedules timers; on expiry pushes tasks into EventLoop
- LoopThreadPool: main EventLoop accepts connections and distributes to worker EventLoops
- HTTP module sits on top of Connection to parse requests and generate responses

##### Architectural Patterns & Design Principles
- Reactor pattern (EventLoop + Poller + Channel)
- Single‐threaded event loop with optional thread pool for concurrency
- Modular layering: core library (network + timer), protocol (HTTP), examples
- Pluggable scheduling: TimerWheel for O(1) timeouts

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Reactor Core
    subgraph "Reactor Core"
        direction TB
        OS["OS / epoll"]:::external
        Poller["Poller"]:::core
        EventLoop["EventLoop"]:::core
        Channel["Channel"]:::core
    end

    %% Network Abstraction
    subgraph "Network Abstraction"
        direction TB
        TcpServer["TcpServer"]:::network
        Connection["Connection"]:::network
        TcpSocket["TcpSocket"]:::network
    end

    %% Concurrency
    subgraph "Concurrency"
        direction TB
        TimerWheel["TimerWheel"]:::concurrency
        LoopThreadPool["LoopThreadPool"]:::concurrency
    end

    %% Protocol Module
    subgraph "Protocol Module"
        direction TB
        HttpServer["HTTP Server"]:::protocol
        HttpClient["HTTP Client"]:::protocol
        HttpUtility["HTTP Utility"]:::protocol
        StaticContent["Static Content"]:::protocol
    end

    %% Examples
    subgraph "Example Applications"
        direction TB
        EchoExample["Echo Server"]:::example
        HTTPExample["HTTP Server Example"]:::example
        TimerWheelTest["Timer Wheel Test"]:::example
        TimerFDTest["Timer FD Test"]:::example
        AnyExample["Any Example"]:::example
        TopTimerExample["Top-level Timer"]:::example
        TopTimewheelExample["Top-level Timewheel"]:::example
        WebBench["WebBench Load Tester"]:::example
    end

    %% Connections
    OS -->|"events"| Poller
    Poller -->|"dispatch"| EventLoop
    EventLoop -->|"event demux"| Channel
    Channel -->|"read/write/close"| Connection
    Channel -->|"timeout events"| TimerWheel
    Channel -->|"http events"| HttpServer
    TcpServer -->|"accept new connection"| Connection
    Connection -->|"register with"| EventLoop
    TimerWheel -->|"schedule tasks"| EventLoop
    EventLoop -->|"distribute to workers"| LoopThreadPool
    HttpServer -->|"parse/handle"| HttpUtility
    HttpServer -->|"serve static"| StaticContent
    HttpClient -->|"issue request"| HttpServer

    %% Examples interactions
    EchoExample -->|"uses"| TcpServer
    HTTPExample -->|"uses"| HttpServer
    TimerWheelTest -->|"tests"| TimerWheel
    TimerFDTest -->|"tests"| TimerWheel
    AnyExample -->|"demonstrates"| EventLoop
    TopTimerExample -->|"demonstrates"| TimerWheel
    TopTimewheelExample -->|"demonstrates"| TimerWheel
    WebBench -->|"benchmarks"| TcpServer

    %% Click Events
    click EventLoop "https://github.com/lhd-creator/fastserver/blob/master/source/EventLoop.hpp"
    click Poller "https://github.com/lhd-creator/fastserver/blob/master/source/Poller.hpp"
    click Channel "https://github.com/lhd-creator/fastserver/blob/master/source/Channel.hpp"
    click TcpServer "https://github.com/lhd-creator/fastserver/blob/master/source/TcpServer.hpp"
    click Connection "https://github.com/lhd-creator/fastserver/blob/master/source/Connection.hpp"
    click TcpSocket "https://github.com/lhd-creator/fastserver/blob/master/source/TcpSocket.hpp"
    click TimerWheel "https://github.com/lhd-creator/fastserver/blob/master/source/TimerWheel.hpp"
    click LoopThreadPool "https://github.com/lhd-creator/fastserver/blob/master/source/LoopThreadPool.hpp"
    click HttpServer "https://github.com/lhd-creator/fastserver/blob/master/source/Http/server/server.cc"
    click HttpClient "https://github.com/lhd-creator/fastserver/blob/master/source/Http/client/client.cc"
    click StaticContent "https://github.com/lhd-creator/fastserver/blob/master/source/Http/wwwroot/index.html"
    click HttpUtility "https://github.com/lhd-creator/fastserver/blob/master/source/httplib.hpp"
    click EchoExample "https://github.com/lhd-creator/fastserver/blob/master/source/echo/Server.hpp"
    click EchoExample "https://github.com/lhd-creator/fastserver/blob/master/source/echo/EchoServer.hpp"
    click EchoExample "https://github.com/lhd-creator/fastserver/blob/master/source/echo/Client.cc"
    click TimerWheelTest "https://github.com/lhd-creator/fastserver/blob/master/source/MyTest/Test_TimeWheel.cpp"
    click TimerFDTest "https://github.com/lhd-creator/fastserver/blob/master/source/MyTest/timerfd.cpp"
    click AnyExample "https://github.com/lhd-creator/fastserver/blob/master/Example/Any/Any.hpp"
    click AnyExample "https://github.com/lhd-creator/fastserver/blob/master/Example/Any/Main.cc"
    click TopTimerExample "https://github.com/lhd-creator/fastserver/blob/master/Example/Timer/timer.cpp"
    click TopTimewheelExample "https://github.com/lhd-creator/fastserver/blob/master/Example/Timewheel/timewheel.hpp"
    click TopTimewheelExample "https://github.com/lhd-creator/fastserver/blob/master/Example/Timewheel/Main.cc"
    click WebBench "https://github.com/lhd-creator/fastserver/tree/master/source/WebBench"

    %% Styles
    classDef core fill:#AED6F1,stroke:#1B4F72,stroke-width:2px
    classDef network fill:#A9CCE3,stroke:#154360,stroke-width:2px
    classDef concurrency fill:#F9E79F,stroke:#7D6608,stroke-width:2px
    classDef protocol fill:#A3E4D7,stroke:#0E6251,stroke-width:2px
    classDef example fill:#D5D8DC,stroke:#566573,stroke-width:2px,stroke-dasharray: 5 5
    classDef external fill:#F5B7B1,stroke:#78281F,stroke-width:2px,stroke-dasharray: 2 2
```
