# mosn/mosn architecture

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

- Interactive diagram: https://gitdiagram.com/mosn/mosn
- Repository: https://github.com/mosn/mosn

## Overview

##### And delineate the main logical components
- Client/Edge – The entry point for external requests.
- Inbound Listener & Connection Handler – Accepts and demultiplexes incoming traffic.
- Filter Chain/Routing Engine – Processes inbound requests through a flexible series of filters including header manipulation, fault injection, mirroring, retry and redirect components.
- Protocol Modules – Multiple protocol support (HTTP/1.1, HTTP/2, gRPC, XProtocol, etc.) implemented across various “pkg/protocol” directories.
- Load Balancing / Upstream Cluster Manager – Manages connection pools, health checks, circuit breakers, and multiple load balancing policies (round-robin, weighted RR, EDF, etc.).
- TLS and Security Module – Handles certificate management, TLS inspection, and SDS integration (using pkg/mtls and related crypto packages).
- Administration & Configuration Management – Administration API, dynamic config reloading, logging, and metrics (as seen with pkg/admin, pkg/configmanager, and pkg/metrics).
- Extension Framework – Plugin systems with support for go-plugins, WASM-based extensions, and process-based modules.
- Observability and Tracing – Integration of tracing (Jaeger, Skywalking), access/logging, and metrics reporting (via Prometheus sinks and pkg/log, pkg/trace).

##### Relationships and interactions
- How the routing engine uses configuration and dynamic policies (supplied by the Configuration Manager) to select an upstream cluster.
- That the Upstream Cluster Manager (load balancing and health checking) passes traffic to the backend services.
- Auxiliary control paths for administrative APIs, real-time metrics collection, and TLS/SDS updates.
- Extension hooks where custom filters or protocol transformations can plug into the filter chain or protocol processing modules.

##### Important architectural patterns
- Modularity: Each major function (routing, security, metrics, protocol handling) is decoupled into separate modules.
- Dynamic configuration: Emphasize how the configuration is continuously reloaded and how administrative APIs (or even integration with Istio’s dynamic resources) drive behavior.
- Pluggability: Clearly show extension points that support go-plugin, WASM, and custom compiled modules.
- High-performance networking: Underline the event-driven, asynchronous design with efficient connection pooling (as evident in pkg/network and pkg/stream).

##### Relevant technologies and libraries
- Go as the core programming language.
- Integration with Istio, Envoy libraries and CNCF ecosystem components (like Prometheus, Jaeger, Skywalking).
- Use of Docker (via .devcontainer and build/) for containerized deployment.
- CGo-based cipher suites for SM3/SM4 support.
- WASM runtime integration.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Plane Components
    subgraph "Data Plane"
        A1["Client/Edge"]:::network
        A2["Inbound Listener & Connection Handler"]:::network
        A3["Filter Chain (HTTP/1.1, HTTP/2, XProtocol)"]:::network
        A4["Protocol Modules"]:::network
        A5["Routing Engine"]:::network
        A6["Upstream Cluster Manager"]:::network
    end

    %% Control Plane Components
    subgraph "Control Plane"
        B1["Administration & Configuration Management"]:::control
        B2["TLS and Security Module"]:::control
        B3["Extension Framework"]:::extension
        B4["Observability and Tracing"]:::observability
    end

    %% Data flow arrows in Data Plane
    A1 -->|"processes"| A2
    A2 -->|"demuxes"| A3
    A3 -->|"handsOff"| A4
    A4 -->|"routesTo"| A5
    A5 -->|"balances"| A6

    %% Control/Configuration interactions (dashed arrows)
    B1 ---|"configures"| A3
    B1 ---|"configures"| A5
    B1 ---|"configures"| A6
    B2 -.->|"secures"| A3
    B2 -.->|"appliesTLS"| A4
    B3 -.->|"hooksInto"| A3
    B3 -.->|"extends"| A4
    A3 -->|"reportsMetrics"| B4
    A4 -->|"reportsMetrics"| B4
    A5 -->|"reportsMetrics"| B4

    %% Click Events
    click A1 "https://github.com/mosn/mosn/blob/master/cmd/mosn/main/mosn.go"
    click A2 "https://github.com/mosn/mosn/blob/master/pkg/network/listener.go"
    click A3 "https://github.com/mosn/mosn/blob/master/pkg/streamfilter/chain.go"
    click A4 "https://github.com/mosn/mosn/tree/master/pkg/protocol"
    click A5 "https://github.com/mosn/mosn/blob/master/pkg/router/routers_manager.go"
    click A6 "https://github.com/mosn/mosn/blob/master/pkg/upstream/cluster/cluster_manager.go"
    click B1 "https://github.com/mosn/mosn/blob/master/pkg/admin/server/server.go"
    click B1 "https://github.com/mosn/mosn/blob/master/pkg/configmanager/configmanager.go"
    click B2 "https://github.com/mosn/mosn/tree/master/pkg/mtls"
    click B3 "https://github.com/mosn/mosn/blob/master/pkg/plugin/plugin.go"
    click B4 "https://github.com/mosn/mosn/blob/master/pkg/trace/driver.go"

    %% Styles
    classDef network fill:#c7f7b2,stroke:#2f7d32,stroke-width:2px;
    classDef control fill:#add8e6,stroke:#00008b,stroke-width:2px;
    classDef extension fill:#dda0dd,stroke:#800080,stroke-width:2px;
    classDef observability fill:#ffdab9,stroke:#ff8c00,stroke-width:2px;
```
