# jeffutter/rediscovery architecture

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

- Interactive diagram: https://gitdiagram.com/jeffutter/rediscovery
- Repository: https://github.com/jeffutter/rediscovery

## Overview

##### Main Components
- Rediscovery Core Module: This is the heart of the project (see files such as lib/rediscovery.ex, lib/rediscovery/application.ex, and several internal modules like poller, pub_sub, listener, logger, etc.). It provides the service discovery functionality and interacts with Redis.
- Listener Modules: These are user-supplied modules (as demonstrated in the README with MyApp.Listener) that implement specific callbacks (list/0, add/1, remove/1) to react to node changes.
- External Redis Service: Since the project relies on a Redis backend, include a component representing the Redis server; remember to note configuration options (host, port, and prefix).

##### Relationships and Interactions
- The Rediscovery component communicates with the Redis backend (using a connection probably managed internally) to detect node information.
- When changes are observed (via polling or pub/sub mechanisms, as indicated by the presence of modules like poller and pub_sub), Rediscovery notifies the Listener modules.
- The Listener modules use the provided callbacks to update node connectivity (e.g., connecting or disconnecting nodes), and this communication is triggered by the Rediscovery core service.
- All these components are wired together and supervised as part of the OTP supervision tree to ensure fault tolerance.

##### Architectural Patterns and Design Principles
- OTP Supervision Tree: The proper use of supervisors is critical, ensuring that Rediscovery and the listeners are restarted if they crash.
- Callback/Listener Pattern: The design allows the integration of custom listener implementations to handle node addition and removal events.
- Decoupling: The architecture decouples the discovery logic (Redis discovery) from the application's reaction logic (listener callbacks), increasing flexibility.
- External Service Integration: The system is designed to work with an external Redis server for data storage and event propagation.

## Diagram (Mermaid)

```mermaid
graph TD
    E["External Redis Service<br/>(host, port, prefix)"]:::external

    subgraph "Supervisory Tree"
        S["Supervisor"]:::supervisor
        subgraph "Rediscovery Core"
            RC["Rediscovery Core"]:::core
            p["Poller"]:::internal
            ps["Pub/Sub"]:::internal
            pg["Process Group"]:::internal
            lg["Logger"]:::internal
            RC --> p
            RC --> ps
            RC --> pg
            RC --> lg
        end
        LM["Listener Modules"]:::listener
        S --> RC
        S --> LM
    end

    RC -->|"RedisCommands/Subscriptions"| E
    RC -->|"notifies"| LM

    click RC "https://github.com/jeffutter/rediscovery/blob/main/lib/rediscovery.ex"
    click RC "https://github.com/jeffutter/rediscovery/blob/main/lib/rediscovery/application.ex"
    click RC "https://github.com/jeffutter/rediscovery/blob/main/lib/rediscovery/state.ex"
    click RC "https://github.com/jeffutter/rediscovery/blob/main/lib/rediscovery/updater.ex"
    click p "https://github.com/jeffutter/rediscovery/blob/main/lib/rediscovery/poller.ex"
    click ps "https://github.com/jeffutter/rediscovery/blob/main/lib/rediscovery/pub_sub.ex"
    click pg "https://github.com/jeffutter/rediscovery/blob/main/lib/rediscovery/process_group.ex"
    click lg "https://github.com/jeffutter/rediscovery/blob/main/lib/rediscovery/logger.ex"
    click LM "https://github.com/jeffutter/rediscovery/blob/main/lib/rediscovery/listener.ex"
    click S "https://github.com/jeffutter/rediscovery/blob/main/lib/rediscovery/supervisor.ex"

    classDef external fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef supervisor fill:#ccffcc,stroke:#00aa00,stroke-width:2px;
    classDef core fill:#ccddff,stroke:#0000ff,stroke-width:2px;
    classDef internal fill:#e6ccff,stroke:#9900cc,stroke-width:2px;
    classDef listener fill:#ffffcc,stroke:#aaaa00,stroke-width:2px;
```
