# autodoc-technology/streaming-based-config architecture

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

- Interactive diagram: https://gitdiagram.com/autodoc-technology/streaming-based-config
- Repository: https://github.com/autodoc-technology/streaming-based-config

## Overview

Based on your file tree and README, the project is a Go-based library that provides streaming-based configuration management. In essence, it is designed to subscribe to configuration changes through a pluggable transport mechanism (e.g., Consul, Nats, and a demo transport at present, with a future plan for Kafka), decode the configuration (using a JSON encoder by default or potentially others), and offer the updated configuration data to the consuming application via a subscription interface.

##### Project Type and Purpose
- This is a library aimed at applications that need to access and keep configuration data updated in real time.
- It abstracts away the details of various underlying transport mechanisms to deliver configuration changes via a subscription model.
- It emphasizes pluggable components for key building and data encoding to maintain flexibility.

##### Analyzing the File Structure
- Top-Level Files: Files like encoder.go, holder.go, key.go, subscriber.go, subscription.go, and transport.go represent the core logic.
- Modular Directories:
- sbcencoder: Contains the JSON encoder and its tests.
- sbckey: Houses key-generation logic (with support for different transport-related keys like Consul and Nats).
- sbctransport: Implements various transport mechanisms (demo, consul, nats, fixed, nothing) for accessing streaming configuration data.
- The examples directory provides sample applications that show how to use different transports (Consul, Nats, and a simple one).

##### Main Components and Their Relationships
- Subscriber Component:
- Acts as the entry point for subscribing to configuration updates.
- Uses a Transport instance and the Key Builder to start the subscription.
- Transport Layer:
- Abstracts different mechanisms to access configuration changes (implemented in sbctransport).
- Implementations include DemoTransport, ConsulTransport, NatsTransport, etc.
- Key Builder:
- Located in sbckey; builds the keys used to identify/configure different configuration sources.
- Data Encoder:
- Found in sbcencoder; responsible for encoding/decoding configuration data (with JSON encoding as a primary example).
- Additional Utility Components:
- Holder and Subscription: Manage the current configuration state and updates.
- Unit test files scattered across the project ensure these components work together properly.

Relationships:
- The Subscriber takes a Transport and Key Builder instance.
- The Transport receives raw configuration events/data that are then passed to the encoder.
- The encoder decodes the data into the proper configuration structure.
- The updated configuration is then handed back to the subscriber, which maintains it and offers a stream of updates.

##### Architectural Patterns and Design Principles
- Pluggable/Modular Architecture: Components like transports, encoders, and key builders can be easily substituted or extended.
- Dependency Injection: The Subscriber is decoupled from concrete implementations of Transports and Key Builders, making testing and extension simpler.
- Separation of Concerns: Encoding/decoding, key generation, and transport are handled independently.
- Event-Driven Design: The system is based on streaming updates, which are relayed via subscriptions.

## Diagram (Mermaid)

```mermaid
graph TD
    %% External Configuration Sources
    subgraph "External Configuration Sources"
        CONSUL_SRC["Consul Server"]:::external
        NATS_SRC["Nats Broker"]:::external
        OTHER_SRC["Other Source"]:::external
    end

    %% Transport Layer
    subgraph "Transport Layer"
        DEMO["Demo Transport"]:::transport
        CONSUL_TRANS["Consul Transport"]:::transport
        NATS_TRANS["Nats Transport"]:::transport
        FIXED_TRANS["Fixed Transport"]:::transport
        NOTHING_TRANS["Nothing Transport"]:::transport
    end

    %% Data Processing Modules
    subgraph "Data Processing Modules"
        JSON_ENCODER["JSON Encoder (Default)"]:::encoder
    end

    %% Key Management
    subgraph "Key Management"
        DEFAULT_KEY["Default Key Builder"]:::key
        CONSUL_KEY["Consul Key Builder"]:::key
        NATS_KEY["Nats Key Builder"]:::key
    end

    %% Core Library Components
    subgraph "Core Library"
        SUBSCRIBER["Subscriber"]:::core
        SUBSCRIPTION["Subscription Manager"]:::core
        HOLDER["Configuration Holder"]:::core
    end

    %% Connections from External Sources to Transports
    CONSUL_SRC -->|"streams"| CONSUL_TRANS
    NATS_SRC -->|"streams"| NATS_TRANS
    OTHER_SRC -->|"streams"| DEMO

    %% Connections from Transports to Data Encoder
    DEMO -->|"sendsConfig"| JSON_ENCODER
    CONSUL_TRANS -->|"sendsConfig"| JSON_ENCODER
    NATS_TRANS -->|"sendsConfig"| JSON_ENCODER
    FIXED_TRANS -->|"sendsConfig"| JSON_ENCODER
    NOTHING_TRANS -->|"sendsConfig"| JSON_ENCODER

    %% Connection from Data Encoder to Subscriber
    JSON_ENCODER -->|"decodedConfig"| SUBSCRIBER

    %% Connections from Key Management to Subscriber
    DEFAULT_KEY -->|"providesKeys"| SUBSCRIBER
    CONSUL_KEY -->|"providesKeys"| SUBSCRIBER
    NATS_KEY -->|"providesKeys"| SUBSCRIBER

    %% Core Library Internal Connections
    SUBSCRIBER -->|"notifiesUpdates"| SUBSCRIPTION
    SUBSCRIBER -->|"updates"| HOLDER

    %% Click Events for Subscriber Components
    click SUBSCRIBER "https://github.com/autodoc-technology/streaming-based-config/blob/main/subscriber.go"
    click SUBSCRIPTION "https://github.com/autodoc-technology/streaming-based-config/blob/main/subscription.go"
    click HOLDER "https://github.com/autodoc-technology/streaming-based-config/blob/main/holder.go"

    %% Click Events for Transport Layer Components
    click DEMO "https://github.com/autodoc-technology/streaming-based-config/blob/main/sbctransport/demo_transport.go"
    click CONSUL_TRANS "https://github.com/autodoc-technology/streaming-based-config/blob/main/sbctransport/consul_transport.go"
    click NATS_TRANS "https://github.com/autodoc-technology/streaming-based-config/blob/main/sbctransport/nats_transport.go"
    click FIXED_TRANS "https://github.com/autodoc-technology/streaming-based-config/blob/main/sbctransport/fixed_transport.go"
    click NOTHING_TRANS "https://github.com/autodoc-technology/streaming-based-config/blob/main/sbctransport/nothing_transport.go"

    %% Click Events for Key Management Components
    click DEFAULT_KEY "https://github.com/autodoc-technology/streaming-based-config/blob/main/sbckey/default.go"
    click CONSUL_KEY "https://github.com/autodoc-technology/streaming-based-config/blob/main/sbckey/consul.go"
    click NATS_KEY "https://github.com/autodoc-technology/streaming-based-config/blob/main/sbckey/nats.go"

    %% Click Event for Data Encoder
    click JSON_ENCODER "https://github.com/autodoc-technology/streaming-based-config/blob/main/sbcencoder/json_encoder.go"

%% Style Definitions
classDef external fill:#f4a261,stroke:#000,stroke-width:2px;
classDef transport fill:#2a9d8f,stroke:#000,stroke-width:2px;
classDef encoder fill:#e9c46a,stroke:#000,stroke-width:2px;
classDef key fill:#e76f51,stroke:#000,stroke-width:2px;
classDef core fill:#264653,stroke:#fff,stroke-width:2px,color:#fff;
```
