# panjf2000/gnet architecture

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

- Interactive diagram: https://gitdiagram.com/panjf2000/gnet
- Repository: https://github.com/panjf2000/gnet

## Overview

##### Main Components Identification
The system should be divided into these key components:

a. Core Engine Layer:
- Event Loop System (eventloop_unix.go, eventloop_windows.go)
- Reactor Components (reactor_default.go, reactor_ultimate.go)
- Connection Management (conn_map.go, conn_matrix.go)

b. Platform-Specific Network Layer:
- Network Polling Subsystem
- epoll (Linux)
- kqueue (BSD/macOS)
- IOCP (Windows)

c. Protocol Support Layer:
- TCP Support
- UDP Support
- Unix Domain Socket Support

d. Resource Management:
- Buffer Pool System
- Goroutine Pool
- Memory Management

##### Component Relationships

**[Client Connections] → [Load Balancer]**
- ↓
**[Network Polling Subsystem] ← → [Event Loop] ← → [Connection Manager]**
- ↑                                           ↑
- ↓                                           ↓
**[Platform-Specific Implementation] ← → [Buffer Management]**
- ↑
- ↓
- [Resource Pools]

##### Important Elements

a. Core Features:
- Multi-threading model
- Lock-free architecture
- Event-driven loop system
- Buffer management system

b. Platform Support:
- Linux (epoll)
- BSD/macOS (kqueue)
- Windows (IOCP)

c. Protocol Support:
- TCP
- UDP
- Unix Domain Socket

##### Implementation Details to Highlight

- Event flow from network polling to event processing
- Buffer management system
- Demonstrate the load balancing mechanisms
- Resource pooling system

##### Technical Notes

- Non-blocking nature of operations
- Separation between networking and business logic
- Where memory optimization occurs
- Points of platform-specific implementations

Remember to include version information and supported platforms in a reference section, as this is crucial for understanding the framework's capabilities and limitations.

This design should reflect gnet's core strength as a high-performance networking framework while showing its flexibility in supporting multiple protocols and platforms.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client["Client Layer"]
        CL[Client Connections]:::client
        LB[Load Balancer]:::core
    end

    subgraph Network["Network Layer"]
        NPS[Network Polling Subsystem]:::network
        
        subgraph Platform["Platform-Specific Polling"]
            EP[epoll - Linux]:::platform
            KQ[kqueue - BSD/macOS]:::platform
            IOCP[IOCP - Windows]:::platform
        end
    end

    subgraph Core["Core Engine Layer"]
        EL[Event Loop System]:::core
        RC[Reactor Components]:::core
        CM[Connection Manager]:::core
    end

    subgraph Protocol["Protocol Support"]
        TCP[TCP Support]:::protocol
        UDP[UDP Support]:::protocol
        UDS[Unix Domain Socket]:::protocol
    end

    subgraph Resources["Resource Management"]
        BP[Buffer Pool]:::resource
        GP[Goroutine Pool]:::resource
        MM[Memory Management]:::resource
    end

    %% Relationships
    CL --> LB
    LB --> NPS
    NPS <--> EL
    EL <--> CM
    NPS --- Platform
    EP & KQ & IOCP --> CM
    CM <--> BP
    BP <--> Resources
    Protocol --- CM

    %% Click Events
    click LB "https://github.com/panjf2000/gnet/blob/dev/load_balancer.go"
    click EL "https://github.com/panjf2000/gnet/blob/dev/eventloop_unix.go"
    click RC "https://github.com/panjf2000/gnet/blob/dev/reactor_default.go"
    click CM "https://github.com/panjf2000/gnet/blob/dev/conn_map.go"
    click EP "https://github.com/panjf2000/gnet/blob/dev/internal/netpoll/poller_epoll_default.go"
    click KQ "https://github.com/panjf2000/gnet/blob/dev/internal/netpoll/poller_kqueue_default.go"
    click TCP "https://github.com/panjf2000/gnet/blob/dev/internal/socket/tcp_socket.go"
    click UDP "https://github.com/panjf2000/gnet/blob/dev/internal/socket/udp_socket.go"
    click UDS "https://github.com/panjf2000/gnet/blob/dev/internal/socket/unix_socket.go"
    click BP "https://github.com/panjf2000/gnet/blob/dev/pkg/buffer/ring/ring_buffer.go"
    click GP "https://github.com/panjf2000/gnet/blob/dev/pkg/pool/goroutine/goroutine.go"

    %% Styling
    classDef core fill:#2374f7,stroke:#000,stroke-width:2px
    classDef client fill:#ff6b6b,stroke:#000,stroke-width:2px
    classDef network fill:#4ecdc4,stroke:#000,stroke-width:2px
    classDef platform fill:#95d195,stroke:#000,stroke-width:2px
    classDef protocol fill:#ba68c8,stroke:#000,stroke-width:2px
    classDef resource fill:#ffd93d,stroke:#000,stroke-width:2px

    %% Legend
    subgraph Legend
        L1[Core Components]:::core
        L2[Client Layer]:::client
        L3[Network Components]:::network
        L4[Platform Specific]:::platform
        L5[Protocol Support]:::protocol
        L6[Resource Management]:::resource
    end
```
