# name5566/leaf architecture

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

- Interactive diagram: https://gitdiagram.com/name5566/leaf
- Repository: https://github.com/name5566/leaf

## Overview

##### Main Components
- Leaf Core (leaf.go): Acts as the entry point and overall orchestrator.
- Channel RPC System (chanrpc directory): Provides a mechanism for asynchronous communication between different modules, which is essential for decoupling components.
- Network Layer (network directory): Contains submodules for handling different network protocols and message formats. This includes:
- TCP communication (tcp_client.go, tcp_conn.go, tcp_server.go, tcp_msg.go)
- WebSocket support (ws_client.go, ws_conn.go, ws_server.go)
- Data serialization formats (JSON and Protobuf subdirectories).
- Gate (gate directory): Acts as the network gateway managing client connections before passing messages deeper into the system.
- Cluster (cluster directory): Provides support for distributed server deployments, ensuring servers can work as a part of a bigger cluster.
- Database Integration (db directory): Although focused on MongoDB here, it highlights how the framework can interface with external persistence layers.
- Console (console directory): Likely used for command handling and administrative functions for monitoring or configuring the framework at runtime.
- Module System (module directory): Enabling the extension and custom feature integration, hinted by the presence of a “skeleton” for module development.
- Logging (log directory): For recording system events and diagnostics.
- Timer (timer directory): For scheduling time-based events, crucial for tasks like game logic timing or periodic maintenance.
- Utility Functions (util directory): Offers general-purpose tools (deep copy, random operations, map utilities, semaphores) that support other components.

##### Relationships and Interactions
- Incoming client connections first hit the Gate, which is part of the networking infrastructure.
- The Network module then manages the communication details, handling different protocols (TCP, WebSocket, JSON, Protobuf) that eventually convert into messages.
- These messages are dispatched to different game logic modules via the Channel RPC (chanrpc), ensuring loose coupling and asynchronous processing.
- The Module System is where game-specific logic is implemented, potentially interacting with the Timer for scheduled tasks.
- Database operations (using the MongoDB integration) are invoked where persistent storage is necessary.
- The Cluster component coordinates with the main server to distribute workloads across multiple instances, ensuring scalability.
- The Console module provides an interface for debugging or configuration, and the Log module captures runtime details.
- Utility functions help support all the modules with common operations.

##### Architectural Patterns and Design Principles
- Modularity: Components are clearly separated into distinct directories (e.g., network, gate, module, etc.), which promotes single responsibility and ease of maintenance.
- Messaging/Asynchronous Processing: The chanrpc system is a key element that decouples different parts of the framework.
- Extensibility: The presence of a module system (“module” and “skeleton”) means that new features or game logic can be integrated cleanly.
- Scalability: Multi-core and clustering support indicate that Leaf is designed with horizontal scaling in mind.

##### Relevant Technologies and Libraries
- Go (golang) forms the basis of this framework.
- Networking supporting both TCP and WebSocket protocols; serialization methods via JSON and Protobuf.
- MongoDB for database support.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Layer
    subgraph "Core"
        LC("Leaf Core"):::core
    end

    %% Communication & Network Layer
    subgraph "Communication & Network"
        GT("Gate"):::network
        NT("Network Layer"):::network
        CR("Channel RPC"):::messaging
    end

    %% Game Logic Layer
    subgraph "Game Logic"
        MS("Module System"):::module
    end

    %% Distributed & Persistence Layer
    subgraph "Distributed & Persistence"
        CL("Cluster"):::distributed
        DB("Database Integration"):::database
    end

    %% Administration & Monitoring Layer
    subgraph "Administration & Monitoring"
        CN("Console"):::admin
        LG("Logging"):::admin
    end

    %% Support Services Layer
    subgraph "Support Services"
        TM("Timer"):::support
        UT("Utility Functions"):::support
    end

    %% Connections
    GT -->|"handles"| NT
    NT -->|"processes"| CR
    CR -->|"dispatches"| MS
    MS -->|"schedules"| TM
    MS -->|"stores"| DB

    LC -->|"orchestrates"| NT
    LC -->|"controls"| CR
    LC -->|"scales"| CL
    LC -->|"interfaces"| CN
    CN -->|"monitors"| LG
    UT -->|"supports"| MS

    %% Class Definitions with Color
    classDef core fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef network fill:#a8dadc,stroke:#333,stroke-width:2px;
    classDef messaging fill:#a2d2ff,stroke:#333,stroke-width:2px;
    classDef module fill:#ffafcc,stroke:#333,stroke-width:2px;
    classDef distributed fill:#caffbf,stroke:#333,stroke-width:2px;
    classDef database fill:#ffd6a5,stroke:#333,stroke-width:2px;
    classDef admin fill:#ffc6ff,stroke:#333,stroke-width:2px;
    classDef support fill:#d0f4de,stroke:#333,stroke-width:2px;

    %% Click Events
    click LC "https://github.com/name5566/leaf/blob/master/leaf.go"
    click CR "https://github.com/name5566/leaf/blob/master/chanrpc/chanrpc.go"
    click NT "https://github.com/name5566/leaf/tree/master/network/"
    click GT "https://github.com/name5566/leaf/blob/master/gate/gate.go"
    click CL "https://github.com/name5566/leaf/blob/master/cluster/cluster.go"
    click DB "https://github.com/name5566/leaf/blob/master/db/mongodb/mongodb.go"
    click CN "https://github.com/name5566/leaf/blob/master/console/console.go"
    click MS "https://github.com/name5566/leaf/blob/master/module/module.go"
    click LG "https://github.com/name5566/leaf/blob/master/log/log.go"
    click TM "https://github.com/name5566/leaf/blob/master/timer/timer.go"
    click UT "https://github.com/name5566/leaf/tree/master/util/"
```
