# kourier-server/kourier architecture

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

- Interactive diagram: https://gitdiagram.com/kourier-server/kourier
- Repository: https://github.com/kourier-server/kourier

## Overview

##### Project type and purpose
- Kourier is an open-source, high-performance HTTP server library written in modern C++.
- It exposes two main building blocks for your applications: a blazing-fast HTTP server (HttpServer) and low-level socket classes (TcpSocket, TlsSocket).
- Its focus is on strict HTTP syntax compliance, minimal memory footprint, and maximal throughput.

##### File structure
- Top-level “Src” directory broken into four domains:
- Core – event loop, epoll integration, timers, signal-slot mechanism.
- Http – HTTP parser (state-machine + SIMD), request/router, HttpBroker, HttpServer and its options.
- Server – generic connection listener and worker abstractions, factories, repositories.
- Lib/Kourier – CMake install targets and packaging.
- Tests are organized under Src/Tests (unit specs, benchmark resources, Spectator test framework).
- Configuration & build: CMakeLists.txt, Config.cmake.in, Documentation folder.

##### Extract architecture notes from the README
- Layered design: Core underneath, Transport (TCP/TLS sockets) on top of Core, HTTP layer above transport, then Server orchestration.
- Key technologies: Linux epoll (edge-triggered), custom memory BIOs on OpenSSL, SIMD-accelerated HTTP parser, template-metaprogramming signal-slot.
- Patterns: event-driven/reactive, state machine for parsing, separation of concerns into modular libraries.

##### Main components and their interactions
a. Core Layer
- EpollEventNotifier / EpollEventSource: integrates Linux epoll into an AsyncQObject-based event loop.
- Timer, TimerPrivate_epoll: lightweight userspace timers.
- Signal-Slot machinery (AsyncQObject.h, MetaTypeSystem): dispatch events inside worker threads.
b. Transport Layer
- TcpSocket / TlsSocket classes: wrap POSIX sockets/Epoll integration; TlsContext & TlsConfiguration build custom BIOs for OpenSSL.
- IOChannel abstraction for buffered I/O.
c. HTTP Layer
- HttpRequestParser: byte-by-byte state-machine with SIMD acceleration.
- HttpBroker & HttpConnectionHandlerFactory: accept parsed requests, hand off to business logic via HttpRequestRouter.
- HttpServer / HttpServerWorker: listen for connections, spawn workers (based on ServerWorkerFactory).
d. Server Orchestration
- ConnectionListener (QTcpServerBasedConnectionListener & AsyncServerWorker): accept incoming sockets, register them in Core.
- ConnectionHandlerRepository & Factory: map raw sockets to HttpConnectionHandlers.
- Server – ties together listener, worker factories, error handling.
e. External Dependencies
- Linux kernel’s epoll interface.
- OpenSSL (custom memory BIOs).
- CMake build system.
- Spectator test framework for behavior specifications.

##### Architectural patterns & design principles
- Event-driven, reactive design with non-blocking I/O.
- Strict layering and separation of concerns.
- Factory & repository patterns for handler instantiation.
- State-machine and SIMD for parsing performance.
- Custom memory BIO pattern for TLS efficiency.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Dependencies
    subgraph "External Dependencies"
        direction TB
        EpollInterface(("Linux Epoll")):::external
        OpenSSL(("OpenSSL")):::external
        CMake(("CMake Build System")):::external
        Spectator(("Spectator Test Framework")):::external
    end

    %% Core Layer
    subgraph "Core Layer"
        direction TB
        EpollEventNotifier["EpollEventNotifier"]:::core
        EpollEventSource["EpollEventSource"]:::core
        AsyncQObject["AsyncQObject"]:::core
        SignalSlot["Signal-Slot (MetaTypeSystem)"]:::core
        Timer["Timer"]:::core
        TimerPrivate["TimerPrivate_epoll"]:::core
    end

    %% Transport Layer
    subgraph "Transport Layer"
        direction TB
        IOChannel["IOChannel"]:::transport
        TcpSocket["TcpSocket"]:::transport
        TlsSocket["TlsSocket"]:::transport
    end

    %% HTTP & Server Layer
    subgraph "HTTP & Server Layer"
        direction TB
        HttpRequestParser["HttpRequestParser"]:::http
        HttpBroker["HttpBroker"]:::http
        HttpRouter["HttpRequestRouter"]:::http
        HttpHandlerFactory["HttpConnectionHandlerFactory"]:::http
        HttpServer["HttpServer"]:::http
        HttpServerWorker["HttpServerWorkerFactory"]:::http

        ConnectionListener["ConnectionListener"]:::server
        AsyncServerWorker["AsyncServerWorker"]:::server
        HandlerRepo["ConnectionHandlerRepository"]:::server
        HandlerFactory["ConnectionHandlerFactory"]:::server
        ServerCore["Server (Orchestration)"]:::server
        ServerWorkerFactory["ServerWorkerFactory"]:::server
    end

    %% Data Flows
    EpollInterface -->|"edge-triggered events"| EpollEventSource
    EpollEventSource -->|"notifies"| AsyncQObject
    AsyncQObject -->|"dispatches"| SignalSlot
    SignalSlot -->|"read ready"| TcpSocket
    TcpSocket -->|"buffered I/O"| IOChannel
    IOChannel -->|"raw data"| HttpRequestParser
    HttpRequestParser -->|"parsed request"| HttpRouter
    HttpRouter -->|"route to handler"| HttpHandlerFactory
    HttpHandlerFactory -->|"invokes handler"| HttpBroker
    HttpBroker -->|"response"| TlsSocket
    TlsSocket -->|"TLS encrypt"| EpollInterface

    ConnectionListener -->|"accept connection"| ServerWorkerFactory
    ServerWorkerFactory -->|"spawn worker"| HttpServerWorker
    HttpServerWorker -->|"uses Core, Transport, HTTP"| AsyncServerWorker
    AsyncServerWorker -->|"handles events"| ServerCore

    %% Click Events Core
    click EpollEventNotifier "https://github.com/kourier-server/kourier/blob/main/Src/Core/EpollEventNotifier.h"
    click EpollEventSource "https://github.com/kourier-server/kourier/blob/main/Src/Core/EpollEventSource.h"
    click Timer "https://github.com/kourier-server/kourier/blob/main/Src/Core/Timer.h"
    click TimerPrivate "https://github.com/kourier-server/kourier/blob/main/Src/Core/TimerPrivate_epoll.h"
    click AsyncQObject "https://github.com/kourier-server/kourier/blob/main/Src/Core/AsyncQObject.h"
    click SignalSlot "https://github.com/kourier-server/kourier/blob/main/Src/Core/MetaTypeSystem.h"

    %% Click Events Transport
    click IOChannel "https://github.com/kourier-server/kourier/blob/main/Src/Core/IOChannel.h"
    click TcpSocket "https://github.com/kourier-server/kourier/blob/main/Src/Core/TcpSocket.h"
    click TlsSocket "https://github.com/kourier-server/kourier/blob/main/Src/Core/TlsSocket.h"

    %% Click Events HTTP
    click HttpRequestParser "https://github.com/kourier-server/kourier/blob/main/Src/Http/HttpRequestParser.h"
    click HttpBroker "https://github.com/kourier-server/kourier/blob/main/Src/Http/HttpBroker.h"
    click HttpHandlerFactory "https://github.com/kourier-server/kourier/blob/main/Src/Http/HttpConnectionHandlerFactory.h"
    click HttpRouter "https://github.com/kourier-server/kourier/blob/main/Src/Http/HttpRequestRouter.h"
    click HttpServer "https://github.com/kourier-server/kourier/blob/main/Src/Http/HttpServer.h"
    click HttpServerWorker "https://github.com/kourier-server/kourier/blob/main/Src/Http/HttpServerWorkerFactory.h"

    %% Click Events Server
    click ConnectionListener "https://github.com/kourier-server/kourier/blob/main/Src/Server/ConnectionListener.h"
    click AsyncServerWorker "https://github.com/kourier-server/kourier/blob/main/Src/Server/AsyncServerWorker.h"
    click HandlerRepo "https://github.com/kourier-server/kourier/blob/main/Src/Server/ConnectionHandlerRepository.h"
    click HandlerFactory "https://github.com/kourier-server/kourier/blob/main/Src/Server/ConnectionHandlerFactory.h"
    click ServerCore "https://github.com/kourier-server/kourier/blob/main/Src/Server/Server.h"
    click ServerWorkerFactory "https://github.com/kourier-server/kourier/blob/main/Src/Server/ServerWorkerFactory.h"

    %% Click Events External
    click Spectator "https://github.com/kourier-server/kourier/tree/main/Src/Tests"
    
    %% Styles
    classDef external fill:#d3d3d3,stroke:#333,stroke-width:1px
    classDef core fill:#add8e6,stroke:#333,stroke-width:1px
    classDef transport fill:#90ee90,stroke:#333,stroke-width:1px
    classDef http fill:#ffa500,stroke:#333,stroke-width:1px
    classDef server fill:#ffdead,stroke:#333,stroke-width:1px
```
