# rsocket/rsocket-go architecture

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

- Interactive diagram: https://gitdiagram.com/rsocket/rsocket-go
- Repository: https://github.com/rsocket/rsocket-go

## Overview

##### Main Components
- Core RSocket Engine – Represented by the “core” directory. This is where the heart of the protocol logic lives, including:
- Framing (message encoding/decoding): See core/framing and associated writeable_* files.
- Transport layer: Found in core/transport which handles TCP and Websocket connections.
- Header, error management, and versioning: Managed directly in core.
- Traffic counting and any low-level networking utilities.
- Reactive Streams Abstraction – Represented by the “rx” package which has sub-folders such as mono and flux. These modules implement the reactive streams paradigms (e.g., back-pressure) and are crucial for exposing a “reactive” API.
- Extensions – In the “extension” directory. These contain additional functionalities like authentication and routing as well as metadata composition.
- Load Balancer – Under the “balancer” directory. This component demonstrates strategies (like round robin) to distribute the load and manage groupings.
- CLI Application – In “cmd/rsocket-cli”. This is the front-end interface for users wanting to run RSocket commands (e.g., request/response mode) and get immediate feedback.
- Supporting Components – These include:
- “logger”: Provides logging facilities.
- “payload”: Handles payload construction, supporting varied formats.
- “internal”: Contains various utilities, buffers, session management, fragmentation logic (like splitting/joining of frames), and ancillary routines.
- “lease”: For lease management and flow control.

##### Relationships and Data Flows
- The CLI components interact with the RSocket core by invoking API calls (via client.go and cmd/rsocket-cli) which, in turn, use the core modules.
- The “core” module coordinates low-level communication by interfacing with the transport layer (found in core/transport) and handles message framing (via core/framing).
- Reactive stream abstractions (in “rx”) are integrated with these components to implement asynchronous, back-pressured communication.
- Extension modules hook into the core engine to enrich its capabilities (e.g., adding authentication or routing metadata).
- Internal utilities support sessions, buffers, and fragmentation – critical to ensuring data integrity and performance.
- The balancer module sits as a utility to help distribute requests if multiple endpoints or groups are involved.

##### Architectural Patterns and Design Principles
- Layered Architecture: Clear separation between protocol framing, transport (I/O), reactive abstraction, and extensions.
- Modularity & Extensibility: Each component (e.g., balancer, extensions, logger) is encapsulated in its own directory, supporting independent development and testing.
- Reactive and Asynchronous Communication: The design uses reactive streams (mono and flux) to handle asynchronous data flows.
- Test-Driven Development: A vast number of tests suggest a design that emphasizes testability and modular interactions (look for *_test.go across many directories).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top Layer
    CLI["CLI Application (cmd/rsocket-cli)"]:::cli
    CoreEngine["Core Engine"]:::core

    %% Subgraph for Core Engine Components
    subgraph "Core Engine Components"
        Framing["Framing (core/framing)"]:::core
        Transport["Transport (core/transport)"]:::core
        Header_Error["Header/Error/Version Management (core/header.go)"]:::core
        Traffic["Traffic Counters & Networking"]:::core
    end

    %% Subgraph for Reactive Streams Abstraction
    subgraph "Reactive Streams"
        Mono["Mono (rx/mono)"]:::rx
        Flux["Flux (rx/flux)"]:::rx
    end

    %% Other Supporting Components
    Extensions["Extensions (extension)"]:::extension
    LoadBalancer["Load Balancer (balancer)"]:::balancer
    InternalUtilities["Internal Utilities (internal)"]:::internal
    Logger["Logger (logger)"]:::logger
    Payload["Payload (payload)"]:::payload
    LeaseManagement["Lease Management (lease)"]:::lease

    %% Connections
    CLI -->|"invokes"| CoreEngine
    CoreEngine -->|"coordinates"| Framing
    CoreEngine -->|"delegates"| Transport
    CoreEngine -->|"manages"| Header_Error
    CoreEngine -->|"monitors"| Traffic
    CoreEngine -->|"integrates"| Mono
    CoreEngine -->|"integrates"| Flux
    CoreEngine -->|"hooks"| Extensions
    CoreEngine -->|"balances"| LoadBalancer
    CoreEngine -->|"utilizes"| InternalUtilities
    CoreEngine -->|"logs_to"| Logger
    CoreEngine -->|"constructs"| Payload
    CoreEngine -->|"applies"| LeaseManagement

    %% Click Events
    click CLI "https://github.com/rsocket/rsocket-go/tree/master/cmd/rsocket-cli"
    click CoreEngine "https://github.com/rsocket/rsocket-go/tree/master/core"
    click Framing "https://github.com/rsocket/rsocket-go/tree/master/core/framing"
    click Transport "https://github.com/rsocket/rsocket-go/tree/master/core/transport"
    click Header_Error "https://github.com/rsocket/rsocket-go/blob/master/core/header.go"
    click Mono "https://github.com/rsocket/rsocket-go/tree/master/rx/mono"
    click Flux "https://github.com/rsocket/rsocket-go/tree/master/rx/flux"
    click Extensions "https://github.com/rsocket/rsocket-go/tree/master/extension"
    click LoadBalancer "https://github.com/rsocket/rsocket-go/tree/master/balancer"
    click InternalUtilities "https://github.com/rsocket/rsocket-go/tree/master/internal"
    click Logger "https://github.com/rsocket/rsocket-go/tree/master/logger"
    click Payload "https://github.com/rsocket/rsocket-go/tree/master/payload"
    click LeaseManagement "https://github.com/rsocket/rsocket-go/tree/master/lease"

    %% Styles
    classDef cli fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef core fill:#87CEEB,stroke:#333,stroke-width:2px;
    classDef rx fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef extension fill:#FFB6C1,stroke:#333,stroke-width:2px;
    classDef balancer fill:#FFA07A,stroke:#333,stroke-width:2px;
    classDef internal fill:#D3D3D3,stroke:#333,stroke-width:2px;
    classDef logger fill:#E6E6FA,stroke:#333,stroke-width:2px;
    classDef payload fill:#F0E68C,stroke:#333,stroke-width:2px;
    classDef lease fill:#98FB98,stroke:#333,stroke-width:2px;
```
