# mtgnorton/ws-cluster architecture

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

- Interactive diagram: https://gitdiagram.com/mtgnorton/ws-cluster
- Repository: https://github.com/mtgnorton/ws-cluster

## Overview

##### Main Components
- Client Layer: WebSocket clients connecting to the service via endpoints such as /ws/connect.
- API Gateway / Router: Supporting HTTP endpoints for actions like push (/ws/push) or handshake/initiation.
- WebSocket Server: The core server handling WS connections (e.g., components in the “ws” and “http/server” directories).
- Message Queue System: The message dispatch system (using Redis and Kafka implementations found under core/queue, along with separate handlers for different message types).
- Core Business Logic/Manager: Modules like core/manager, core/client, and core/checking that encapsulate connection management, validations, and operations.
- Shared Utilities: Common libraries implemented in shared/ (auth, jwtws, kit, and utils) that support token authentication, JWT, common algorithms, and other helper functions.
- Monitoring & Profiling: pprof integrated with the HTTP server plus tools for Prometheus and Grafana configuration (check directories tools/wspprof, tools/wsprometheus, and k8s/prometheus) to capture performance and metric data.
- External Services & Deployment: Kubernetes deployment manifests (in k8s/), Dockerfiles for containerization, and continuous integration (Jenkins) pointers from the README hinting at build and deployment automation.

##### Out Relationships and Interactions
- It then shows how business systems call the push API to send data, which is enqueued within the message queue (with separate implementations for Redis and Kafka) located in the “core/queue” directory.
- Supporting modules such as error logging (logger), and shared libraries that provide helper functions seamlessly used across the codebase.

##### Architectural Patterns and Design Principles
- The system follows a modular design with clear separation of concerns: networking (ws, http), business logic (core), message queuing (queue), and shared utilities.
- It incorporates a microservices style or component-based architecture with components loosely coupled; especially evident by separating concerns (e.g., connection handlers in “ws/handler” vs. API endpoints in “http/server”).
- Use of dependency injection and configuration management (through the “config” and “conf” directories) to provide environment-specific settings.

##### Relevant Technologies, Frameworks, and Libraries
- Go language as the primary implementation language.
- Docker (evidenced by Dockerfile) for containerization.
- Kubernetes and Istio (from k8s/ files) for orchestration and intelligent load balancing.
- Redis and Kafka (core/queue/queue_redis.go, core/queue/kafka) for message queuing.
- pprof, Prometheus, and Grafana for performance analysis and monitoring.
- Swagger for API documentation (tools/swagger and docs/ directory).
- Jenkins for automated builds as noted in the README.
- Error logging via Sentry integration (tools/wssentry).

##### Recommended Detail Level
- To detail each subsystem and inter-connection, as this project uses multiple layers and external services.
- Load-balancing and clustering behaviors explicitly (e.g., mention how Istio and Kubernetes facilitate connection routing based on active sessions).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Clients
    WebSocketClient("WebSocket Client"):::client
    BusinessSystem("Business System"):::client

    %% Networking Layer Subgraph
    subgraph "Networking Layer"
        APIGateway("API Gateway / Router"):::gateway
        WSHandler("WebSocket Handler"):::ws
        WSServer("WebSocket Server"):::ws
    end

    %% Message Queue Subgraph
    subgraph "Message Queue System"
        MessageQueue("Message Queue (Redis/Kafka)"):::queue
        KafkaModule("Kafka Module"):::queue
    end

    %% Core Business Logic Subgraph
    subgraph "Core Business Logic/Manager"
        BusinessManager("Business Manager"):::business
        ClientManager("Client Manager"):::business
        ConnectionChecker("Connection Checker"):::business
    end

    %% Shared Utilities Subgraph
    subgraph "Shared Utilities"
        AuthUtility("Auth Utility"):::utility
        JWTUtility("JWT Utility"):::utility
        KitUtility("Kit Utility"):::utility
        UtilsUtility("Utils Utility"):::utility
    end

    %% Monitoring & Profiling Subgraph
    subgraph "Monitoring & Profiling"
        PprofMonitor("Pprof Monitor"):::monitoring
        PrometheusMonitor("Prometheus Monitor"):::monitoring
        K8sPrometheus("K8s Prometheus"):::monitoring
    end

    %% Documentation Subgraph
    subgraph "Documentation (Swagger)"
        SwaggerDocs("Swagger Docs"):::documentation
        SwaggerTool("Swagger Tool"):::documentation
    end

    %% Error Logging Subgraph
    subgraph "Error Logging"
        Logger("Logger"):::logging
        SentryLogger("Sentry Logger"):::logging
    end

    %% Deployment Subgraph
    subgraph "External Services & Deployment"
        K8sDeployment("Kubernetes Deployment"):::deployment
        Dockerfile("Dockerfile"):::deployment
    end

    %% Connections
    WebSocketClient -->|"establishes"| WSHandler
    BusinessSystem -->|"HTTP_push"| APIGateway

    APIGateway -->|"enqueue"| MessageQueue
    MessageQueue -->|"distributes"| WSServer

    WSHandler -->|"manages"| BusinessManager
    WSServer -->|"dispatches"| BusinessManager

    BusinessManager -->|"handles"| ClientManager
    BusinessManager -->|"validates"| ConnectionChecker

    BusinessManager -->|"uses"| AuthUtility
    BusinessManager -->|"uses"| JWTUtility
    BusinessManager -->|"uses"| KitUtility
    BusinessManager -->|"uses"| UtilsUtility

    APIGateway -->|"uses"| AuthUtility
    APIGateway -->|"uses"| JWTUtility

    WSServer -->|"reports"| PprofMonitor
    APIGateway -->|"reports"| PrometheusMonitor
    WSServer -->|"reports"| K8sPrometheus

    WSServer -->|"logs"| Logger
    BusinessManager -->|"logs"| Logger
    Logger -->|"sends to"| SentryLogger

    K8sDeployment -->|"deploys"| APIGateway
    K8sDeployment -->|"deploys"| WSHandler
    K8sDeployment -->|"deploys"| WSServer
    K8sDeployment -->|"deploys"| MessageQueue
    K8sDeployment -->|"deploys"| BusinessManager

    Dockerfile -->|"containerizes"| K8sDeployment

    MessageQueue ---|"integrates"| KafkaModule

    %% Click Events
    click WSHandler "https://github.com/mtgnorton/ws-cluster/tree/main/ws/handler"
    click WSServer "https://github.com/mtgnorton/ws-cluster/tree/main/ws/server"
    click APIGateway "https://github.com/mtgnorton/ws-cluster/tree/main/http/server"
    click MessageQueue "https://github.com/mtgnorton/ws-cluster/tree/main/core/queue"
    click KafkaModule "https://github.com/mtgnorton/ws-cluster/tree/main/core/queue/kafka"
    click BusinessManager "https://github.com/mtgnorton/ws-cluster/tree/main/core/manager"
    click ClientManager "https://github.com/mtgnorton/ws-cluster/tree/main/core/client"
    click ConnectionChecker "https://github.com/mtgnorton/ws-cluster/tree/main/core/checking"
    click AuthUtility "https://github.com/mtgnorton/ws-cluster/tree/main/shared/auth"
    click JWTUtility "https://github.com/mtgnorton/ws-cluster/tree/main/shared/jwtws"
    click KitUtility "https://github.com/mtgnorton/ws-cluster/tree/main/shared/kit"
    click UtilsUtility "https://github.com/mtgnorton/ws-cluster/tree/main/shared/utils"
    click PprofMonitor "https://github.com/mtgnorton/ws-cluster/tree/main/tools/wspprof"
    click PrometheusMonitor "https://github.com/mtgnorton/ws-cluster/tree/main/tools/wsprometheus"
    click K8sPrometheus "https://github.com/mtgnorton/ws-cluster/tree/main/k8s/prometheus"
    click SwaggerDocs "https://github.com/mtgnorton/ws-cluster/tree/main/docs"
    click SwaggerTool "https://github.com/mtgnorton/ws-cluster/tree/main/tools/swagger"
    click Logger "https://github.com/mtgnorton/ws-cluster/tree/main/logger"
    click SentryLogger "https://github.com/mtgnorton/ws-cluster/tree/main/tools/wssentry"
    click K8sDeployment "https://github.com/mtgnorton/ws-cluster/tree/main/k8s"
    click Dockerfile "https://github.com/mtgnorton/ws-cluster/tree/main/Dockerfile"

    %% Styles
    classDef client fill:#FAD7A0,stroke:#000,stroke-width:2px;
    classDef gateway fill:#AED6F1,stroke:#000,stroke-width:2px;
    classDef ws fill:#A9DFBF,stroke:#000,stroke-width:2px;
    classDef queue fill:#D7BDE2,stroke:#000,stroke-width:2px;
    classDef business fill:#F9E79F,stroke:#000,stroke-width:2px;
    classDef utility fill:#D5F5E3,stroke:#000,stroke-width:2px;
    classDef monitoring fill:#F5B7B1,stroke:#000,stroke-width:2px;
    classDef documentation fill:#FADBD8,stroke:#000,stroke-width:2px;
    classDef logging fill:#D2B4DE,stroke:#000,stroke-width:2px;
    classDef deployment fill:#AED6F1,stroke:#000,stroke-width:2px;
```
