# dark-vex/fasthttp architecture

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

- Interactive diagram: https://gitdiagram.com/dark-vex/fasthttp
- Repository: https://github.com/dark-vex/fasthttp

## Overview

##### Project type and purpose
- fasthttp is a Go library providing high-performance HTTP server and client implementations.
- It is not a standalone application but a modular framework with core HTTP engine, utility subpackages, adapters, and example apps.

##### File structure
- Top‐level .go files (args.go, http.go, server.go, client.go, etc.) make up the core package.
- Subdirectories (examples/, expvarhandler/, fasthttpadaptor/, fasthttputil/, reuseport/, stackless/) are first‐class subpackages adding optional functionality.
- Configuration and build: LICENSE, .travis.yml, tests alongside each .go file, benchmarks, and sample Certificate files for SSL.

##### README for additional insights
- Emphasizes two main features: HTTP server (ListenAndServe, zero allocations, pooling) and HTTP client (Do, GetEndToEnd).
- Lists these subpackages: expvarhandler (metrics), fasthttpadaptor (net/http adapter), fasthttputil (in‐memory listeners, pipe conns), reuseport (SO_REUSEPORT listener), stackless (zero‐alloc writer & func scheduling).
- Provides examples of switching from net/http to fasthttp and performance tips.

a. Core HTTP Engine
- RequestCtx & Request/Response handling
- Connection multiplexing and event loop
- Sync.Pool for object reuse
b. Server Layer
- ListenAndServe
- reuseport listener for multicore
- TLS support (snakeoil certs in examples)
c. Client Layer
- HTTPClient.Do / Get
- In‐memory vs. real TCP/instrumented transports
d. Utility Subpackages
- bytebuffer & bytesconv: zero‐alloc buffer and conversion utilities
- fasthttputil: in‐memory listener, pipeconns, testing harness
- fasthttpadaptor: converts net/http handlers to fasthttp.RequestHandler
- expvarhandler: /debug/vars style metrics handler
- stackless: zero‐copy writer and stackless function invocation
e. Examples
- helloworldserver, fileserver under examples/ showing real‐world composition

##### Relationships and interactions
- Client → Core HTTP Engine → Network I/O → Server
- Server.ListenAndServe → reuseport (optional) → core event loop → RequestCtx → user RequestHandler
- RequestHandler can invoke any utility (bytebuffer, bytesconv, stackless writer, expvarhandler, fs handler).
- fasthttpadaptor sits between net/http-style code and the Core HTTP Engine.
- expvarhandler registers on the server URL tree and writes metrics via RequestCtx.

##### Architectural patterns and design principles
- Zero‐allocation pipeline: reuseable buffers, no intermediate strings
- Pooling: sync.Pool for RequestCtx, buffers
- Modular plugin architecture: small clear subpackages for optional features
- Event‐driven nonblocking I/O

##### Detailed capture
- Don’t just show root vs. examples – break out each subpackage as its own component.
- External dependencies (Go net package, TLS certificates, sync.Pool).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Layers
    subgraph "Client Layer"
        direction TB
        HTTPClient["HTTPClient.Do / Get"]:::core
        LBClient["lbclient.go"]:::core
    end

    subgraph "Core HTTP Engine"
        direction TB
        HTTPGO["http.go"]:::core
        ARGS["args.go"]:::core
        COARSE["coarseTime.go"]:::core
        HEADER["header.go"]:::core
        STREAM["stream.go"]:::core
        STRINGS["strings.go"]:::core
        TCPDIALER["tcpdialer.go"]:::core
        TIMER["timer.go"]:::core
        USERDATA["userdata.go"]:::core
        WORKERPOOL["workerpool.go"]:::core
    end

    subgraph "Server Layer"
        direction TB
        Listen["server.go"]:::core
        ReusePort["reuseport/reuseport.go"]:::util
        TLSCert["ssl-cert-snakeoil.pem"]:::util
        TLSKey["ssl-cert-snakeoil.key"]:::util
    end

    subgraph "Utilities"
        direction TB
        ByteBuf["bytebuffer.go"]:::util
        BytesConv["bytesconv.go"]:::util
        Bytes32["bytesconv_32.go"]:::util
        Bytes64["bytesconv_64.go"]:::util
    end

    subgraph "fasthttputil (In-Memory Transport)" 
        direction TB
        InMem["inmemory_listener.go"]:::adapter
        PipeConns["pipeconns.go"]:::adapter
        DocFastUtil["doc.go"]:::adapter
    end

    subgraph "Adapters & Metrics"
        direction TB
        Adapter["adaptor.go"]:::adapter
        Expvar["expvar.go"]:::adapter
    end

    subgraph "Stackless"
        direction TB
        SLFunc["stackless/func.go"]:::util
        SLWriter["stackless/writer.go"]:::util
    end

    subgraph "Examples"
        direction TB
        HelloApp["helloworldserver.go"]:::example
        FileApp["fileserver.go"]:::example
    end

    %% Connections
    HTTPClient -->|"calls"| HTTPGO
    LBClient -->|":Load balancing"| HTTPGO
    HTTPGO -->|"event loop"| Listen
    Listen -->|":uses"| ReusePort
    Listen -->|":TLS"| TLSCert
    Listen -->|":TLS"| TLSKey
    Listen -->|"RequestCtx"| HTTPGO

    HTTPGO -->|"uses"| ByteBuf
    HTTPGO -->|"uses"| BytesConv
    HTTPGO -->|"uses"| Bytes32
    HTTPGO -->|"uses"| Bytes64

    HTTPGO -->|"adapts net/http"| Adapter
    HTTPGO -->|"registers metrics"| Expvar

    HTTPGO -->|"in-memory transport"| InMem
    HTTPGO -->|"in-memory transport"| PipeConns

    HTTPGO -->|"zero-allocation writer"| SLWriter
    HTTPGO -->|"schedules funcs"| SLFunc

    Listen -->|"serves"| HelloApp
    Listen -->|"serves"| FileApp

    %% Click Events
    click HTTPGO "https://github.com/dark-vex/fasthttp/blob/master/http.go"
    click ARGS "https://github.com/dark-vex/fasthttp/blob/master/args.go"
    click COARSE "https://github.com/dark-vex/fasthttp/blob/master/coarseTime.go"
    click HEADER "https://github.com/dark-vex/fasthttp/blob/master/header.go"
    click STREAM "https://github.com/dark-vex/fasthttp/blob/master/stream.go"
    click STRINGS "https://github.com/dark-vex/fasthttp/blob/master/strings.go"
    click TCPDIALER "https://github.com/dark-vex/fasthttp/blob/master/tcpdialer.go"
    click TIMER "https://github.com/dark-vex/fasthttp/blob/master/timer.go"
    click USERDATA "https://github.com/dark-vex/fasthttp/blob/master/userdata.go"
    click WORKERPOOL "https://github.com/dark-vex/fasthttp/blob/master/workerpool.go"
    click Listen "https://github.com/dark-vex/fasthttp/blob/master/server.go"
    click ReusePort "https://github.com/dark-vex/fasthttp/blob/master/reuseport/reuseport.go"
    click TLSCert "https://github.com/dark-vex/fasthttp/blob/master/ssl-cert-snakeoil.pem"
    click TLSKey "https://github.com/dark-vex/fasthttp/blob/master/ssl-cert-snakeoil.key"
    click HTTPClient "https://github.com/dark-vex/fasthttp/blob/master/client.go"
    click LBClient "https://github.com/dark-vex/fasthttp/blob/master/lbclient.go"
    click ByteBuf "https://github.com/dark-vex/fasthttp/blob/master/bytebuffer.go"
    click BytesConv "https://github.com/dark-vex/fasthttp/blob/master/bytesconv.go"
    click Bytes32 "https://github.com/dark-vex/fasthttp/blob/master/bytesconv_32.go"
    click Bytes64 "https://github.com/dark-vex/fasthttp/blob/master/bytesconv_64.go"
    click InMem "https://github.com/dark-vex/fasthttp/blob/master/fasthttputil/inmemory_listener.go"
    click PipeConns "https://github.com/dark-vex/fasthttp/blob/master/fasthttputil/pipeconns.go"
    click DocFastUtil "https://github.com/dark-vex/fasthttp/blob/master/fasthttputil/doc.go"
    click Adapter "https://github.com/dark-vex/fasthttp/blob/master/fasthttpadaptor/adaptor.go"
    click Expvar "https://github.com/dark-vex/fasthttp/blob/master/expvarhandler/expvar.go"
    click SLFunc "https://github.com/dark-vex/fasthttp/blob/master/stackless/func.go"
    click SLWriter "https://github.com/dark-vex/fasthttp/blob/master/stackless/writer.go"
    click HelloApp "https://github.com/dark-vex/fasthttp/blob/master/examples/helloworldserver/helloworldserver.go"
    click FileApp "https://github.com/dark-vex/fasthttp/blob/master/examples/fileserver/fileserver.go"

    %% Styles
    classDef core fill:#cce5ff,stroke:#004085,color:#004085;
    classDef util fill:#d4edda,stroke:#155724,color:#155724;
    classDef adapter fill:#fff3cd,stroke:#856404,color:#856404;
    classDef example fill:#e2e3e5,stroke:#6c757d,color:#6c757d;
```
