# apple/swift-nio architecture

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

- Interactive diagram: https://gitdiagram.com/apple/swift-nio
- Repository: https://github.com/apple/swift-nio

## Overview

##### Component Relationships

a) Core Relationships

b) Data Flow
- Direction of flow (e.g., from network to application)

##### Detailed Component Breakdown

For each major component, create a sub-section showing:

a) EventLoop Architecture
**MultiThreadedEventLoopGroup**
- EventLoop 1
- EventLoop 2
- EventLoop N

b) Channel Pipeline
**ChannelPipeline**
- Inbound Handlers
- Direction: Front to Back
- Outbound Handlers
- Direction: Back to Front

##### Integration Points

External integration points:

a) Bootstrap Components
- ServerBootstrap
- ClientBootstrap
- DatagramBootstrap

b) Protocol Adapters
- HTTP
- WebSocket
- TLS

##### Cross-Cutting Concerns

Cross-cutting features:

a) Concurrency Support
- EventLoopFuture
- EventLoopPromise
- Thread Pool

b) Buffer Management
- ByteBuffer
- Memory Management

##### Specific Implementation Details

SwiftNIO-specific elements:

a) Channel Types
- ServerSocketChannel
- SocketChannel
- DatagramChannel

b) Handler Categories
- Inbound Handlers
- Outbound Handlers
- Duplex Handlers

##### The high-performance focus of the architecture

Remember to include version compatibility information and note the relationship between different SwiftNIO repositories (swift-nio-ssl, swift-nio-http2, etc.) as satellite components.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles
    classDef core fill:#dae8fc,stroke:#6c8ebf
    classDef platform fill:#d5e8d4,stroke:#82b366
    classDef protocol fill:#ffe6cc,stroke:#d79b00
    classDef concurrency fill:#e1d5e7,stroke:#9673a6
    classDef buffer fill:#fff2cc,stroke:#d6b656
    classDef channel fill:#f8cecc,stroke:#b85450

    %% Core Layer
    subgraph CoreLayer["Core Layer"]
        Channel["Channel"]
        ChannelHandler["ChannelHandler"]
        EventLoop["EventLoop"]
        ByteBuffer["ByteBuffer"]
        EventLoopFuture["EventLoopFuture"]
        AsyncSupport["Async/Await Support"]
    end

    %% Platform Layer
    subgraph PlatformLayer["Platform-Specific Layer"]
        MTELG["MultiThreadedEventLoopGroup"]
        SEL["SelectableEventLoop"]
        Socket["Socket"]
        SocketChannel["SocketChannel"]
    end

    %% Protocol Layer
    subgraph ProtocolLayer["Protocol Implementations"]
        HTTP1["NIOHTTP1"]
        WebSocket["NIOWebSocket"]
        TLS["NIOTLS"]
    end

    %% Channel Pipeline
    subgraph PipelineLayer["Channel Pipeline"]
        Pipeline["ChannelPipeline"]
        InboundHandlers["Inbound Handlers"]
        OutboundHandlers["Outbound Handlers"]
    end

    %% Cross-Cutting Features
    subgraph CrossCutting["Cross-Cutting Features"]
        Concurrency["Concurrency Helpers"]
        BufferMgmt["Buffer Management"]
        Utilities["Utilities"]
    end

    %% Bootstrap Components
    subgraph Bootstrap["Bootstrap"]
        ServerBoot["ServerBootstrap"]
        ClientBoot["ClientBootstrap"]
        DatagramBoot["DatagramBootstrap"]
    end

    %% Relationships
    CoreLayer --> PlatformLayer
    ProtocolLayer --> CoreLayer
    Channel --> Pipeline
    Pipeline --> InboundHandlers
    Pipeline --> OutboundHandlers
    MTELG --> EventLoop
    Bootstrap --> Channel
    CrossCutting --> CoreLayer

    %% Component Classes
    class Channel,ChannelHandler,EventLoop,ByteBuffer,EventLoopFuture,AsyncSupport core
    class MTELG,SEL,Socket,SocketChannel platform
    class HTTP1,WebSocket,TLS protocol
    class Pipeline,InboundHandlers,OutboundHandlers channel
    class Concurrency,BufferMgmt,Utilities buffer
    class ServerBoot,ClientBoot,DatagramBoot channel

    %% Click Events
    click Channel "https://github.com/apple/swift-nio/blob/main/Sources/NIOCore/Channel.swift"
    click ChannelHandler "https://github.com/apple/swift-nio/blob/main/Sources/NIOCore/ChannelHandler.swift"
    click EventLoop "https://github.com/apple/swift-nio/blob/main/Sources/NIOCore/EventLoop.swift"
    click ByteBuffer "https://github.com/apple/swift-nio/blob/main/Sources/NIOCore/ByteBuffer-core.swift"
    click MTELG "https://github.com/apple/swift-nio/blob/main/Sources/NIOPosix/MultiThreadedEventLoopGroup.swift"
    click SEL "https://github.com/apple/swift-nio/blob/main/Sources/NIOPosix/SelectableEventLoop.swift"
    click Socket "https://github.com/apple/swift-nio/blob/main/Sources/NIOPosix/Socket.swift"
    click SocketChannel "https://github.com/apple/swift-nio/blob/main/Sources/NIOPosix/SocketChannel.swift"
    click HTTP1 "https://github.com/apple/swift-nio/tree/main/Sources/NIOHTTP1/"
    click WebSocket "https://github.com/apple/swift-nio/tree/main/Sources/NIOWebSocket/"
    click TLS "https://github.com/apple/swift-nio/tree/main/Sources/NIOTLS/"
    click Concurrency "https://github.com/apple/swift-nio/blob/main/Sources/NIOConcurrencyHelpers/NIOAtomic.swift"
    click BufferMgmt "https://github.com/apple/swift-nio/blob/main/Sources/NIOCore/ByteBuffer-aux.swift"
    click Pipeline "https://github.com/apple/swift-nio/blob/main/Sources/NIOCore/ChannelPipeline.swift"
    click ServerBoot "https://github.com/apple/swift-nio/blob/main/Sources/NIOPosix/Bootstrap.swift"
    click AsyncSupport "https://github.com/apple/swift-nio/blob/main/Sources/NIOCore/AsyncAwaitSupport.swift"
```
