mod.rs

Overview

This file implements the core logic for managing and synchronizing a proxy configuration with an external blockchain proxy list. It provides functionalities to:

It serves as the main runtime entry point for the proxy manager component, orchestrating configuration synchronization and proxy reloading in a continuous loop.

The file depends on several modules and external crates:


Modules

Both modules are imported as submodules and utilized within this file to perform their respective roles.


Constants


Functions

pub fn run() -> Result<(), std::io::Error>

Purpose:
Entry point for starting the proxy manager. This function initializes environment variables, tracing/logging, and spawns the Tokio asynchronous runtime in a separate thread named "tokio_main". It handles any panics or errors from the async runtime and exits the process accordingly.

Parameters:
None.

Returns:

Usage Example:

fn main() {
    if let Err(e) = run() {
        eprintln!("Failed to start proxy manager: {}", e);
    }
}

async fn tokio_main() -> anyhow::Result<()>

Purpose:
Asynchronous main function that parses CLI arguments and invokes the core proxy manager task. It handles any errors by logging and exiting the process.

Parameters:
None.

Returns:

Implementation Details:


pub async fn proxy_manager(args: cli::CliArgs) -> anyhow::Result<()>

Purpose:
Main loop function that continuously monitors the proxy list from the blockchain and updates the local proxy configuration file if discrepancies are found. It also triggers proxy reloads to apply updated configurations.

Parameters:

Returns:

Workflow:

  1. Loads the proxy configuration from the specified file.

  2. Fetches the current proxy list from the blockchain asynchronously.

  3. Converts both proxy lists into HashSet<SocketAddr> for efficient comparison.

  4. Compares the sets for any differences:

    • If the blockchain proxy list contains addresses not in the configuration's subscribe list, logs an error and exits.

    • Otherwise, updates the configuration to retain only proxies present in the blockchain list.

    • Saves the updated configuration file.

    • Calls reload_proxy to signal the proxy service to reload.

  5. Sleeps for CONFIG_CHECK_INTERVAL before repeating.

Important Implementation Details:

Usage Example:

let args = cli::CliArgs::parse();
proxy_manager(args).await?;

async fn reload_proxy(settings: &cli::Command) -> anyhow::Result<()>

Purpose:
Reloads the proxy service based on the specified reload command method.

Parameters:

Returns:

Implementation Details:

Usage Example:

let cmd = cli::Command::Pid { pid: 1234 };
reload_proxy(&cmd).await?;

Interaction with Other Parts of the System


Implementation Details and Algorithms


Visual Diagram

flowchart TD
A["run()"] --> B[Start tokio_main thread]
B --> C["tokio_main()"]
C --> D[Parse CLI args]
D --> E["proxy_manager(args)"]
E --> F{Infinite Loop}
F --> G[Load ProxyConfig from file]
F --> H[Fetch proxy list from blockchain]
F --> I[Compare proxy sets]
I -->|Mismatch detected| J[Update config subscribe list]
J --> K[Save updated config]
K --> L["reload_proxy(command)"]
L --> M{Reload method?}
M -->|Docker| N[Send SIGHUP to container]
M -->|Pid| O[Send SIGHUP via kill command]
M -->|PidPath| P[Unimplemented panic]
I -->|No mismatch| F
F -->|Sleep 5s| F

This diagram illustrates the primary control flow of the proxy manager from program start to continuous proxy configuration monitoring and reload signaling.