server.rs

Overview

This file implements the main server application for a proxy service that integrates gossip protocols, network messaging, and configuration hot reloading. It orchestrates network transport using MsQuic and UDP transports, manages configuration and certificate reloading, handles gossip communication, and multiplexes incoming and outgoing messages. The server also sets up telemetry and metrics collection for observability.

Key functionalities include:


Structs and Types

CliArgs

Command-line arguments for the proxy server, implemented using clap::Parser.

Fields:

Field

Type

Description

config

PathBuf

Path to the configuration YAML file (default: config.yaml).

max_connections

usize

Maximum allowed concurrent connections (default: 1000).

Attributes:

Methods:

Usage Example:

let args = CliArgs::parse();
args.run(Some(net_metrics_instance)).await?;

NodeId

A simple wrapper struct for node identifiers used in gossip subscriptions.

Fields:

Traits Implemented:


Constants

Constant

Type

Description

BK_SET_WATCH_INTERVAL_SECS

u64

Interval (in seconds) between bk_set watcher updates (5 seconds).

BK_SET_REQUEST_TIMEOUT_SECS

u64

Timeout (in seconds) for bk_set HTTP requests (1 second).


Static Variables

LONG_VERSION

A lazily initialized string containing detailed build and version information, including git branch, commit, date, and build time.


Functions

run() -> anyhow::Result<()>

Entrypoint function that initializes the environment, installs crypto providers, and creates a Tokio runtime for asynchronous execution. Calls tokio_main() inside the runtime.


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

Async main function executed inside Tokio runtime.


dispatch_hot_reload(...)

Asynchronously listens for changes in configuration, trusted keys, and shutdown signals. Updates network, gossip, and watch gossip configurations and broadcasts changes downstream via watch channels.

Parameters:

Parameter

Type

Description

tls_cert_cache

Option<TlsCertCache>

Optional TLS certificate cache.

shutdown_rx

tokio::sync::watch::Receiver<bool>

Shutdown signal receiver.

proxy_config_rx

tokio::sync::watch::Receiver<ProxyConfig>

Proxy configuration receiver.

bk_set_rx

tokio::sync::watch::Receiver<HashSet<VerifyingKey>>

Trusted public keys receiver.

network_config_tx

tokio::sync::watch::Sender<NetworkConfig>

Sender to update network configuration.

gossip_config_tx

tokio::sync::watch::Sender<GossipConfig>

Sender to update gossip configuration.

watch_gossip_config_tx

tokio::sync::watch::Sender<WatchGossipConfig>

Sender to update watch gossip configuration.

Algorithm:


dispatch_configs(...) -> Option<(NetworkConfig, GossipConfig, WatchGossipConfig)>

Helper function to produce updated network, gossip, and watch gossip configurations.

Parameters:

Parameter

Type

Description

proxy_config_rx

&tokio::sync::watch::Receiver<ProxyConfig>

Current proxy configuration.

bk_set_rx

&tokio::sync::watch::Receiver<HashSet<VerifyingKey>>

Current trusted verifier keys.

tls_cert_cache

&Option<TlsCertCache>

Optional TLS certificate cache.

Returns:

Implementation Details:


message_multiplexor(...) -> anyhow::Result<()>

An async task that bridges incoming network messages to outgoing broadcast channels.

Parameters:

Parameter

Type

Description

metrics

Option<NetMetrics>

Optional metrics instance for observability.

incoming_messages

tokio::sync::mpsc::UnboundedReceiver<IncomingMessage>

Receiver of incoming messages from network.

outgoing_messages

tokio::sync::broadcast::Sender<OutgoingMessage>

Broadcast channel for outgoing messages.

Behavior:


CliArgs::run Method Detailed Workflow

  1. Loads TLS certificate cache.

  2. Loads proxy configuration from file.

  3. Initializes multiple Tokio watch channels for config propagation:

    • Proxy config

    • Network config

    • Gossip config

    • Watch gossip config

    • Subscriptions and peers

  4. Starts gossip subsystem using UDP transport.

  5. Spawns critical tasks:

    • Gossip watcher task (watch_gossip).

    • Configuration reload handler (config_reload_handler).

    • Message multiplexor (message_multiplexor).

    • Network pub-sub handler (network::pub_sub::run).

    • BK set watcher task (bk_set_watcher::run).

    • Hot reload dispatcher.

  6. Runs tokio select on spawned tasks to detect any critical failure and exit.


Interactions with Other Modules


Important Implementation Details


Visual Diagram

flowchart TD
A["run()"] --> B["tokio_main()"]
B --> C["CliArgs::run()"]
C --> D[Load Config & TLS Cache]
C --> E[Spawn Gossip Tasks]
C --> F[Spawn Config Reload Handler]
C --> G[Spawn Message Multiplexor]
C --> H[Spawn Network PubSub Task]
C --> I[Spawn BK Set Watcher]
C --> J[Spawn Hot Reload Dispatcher]
G --> K[Forward IncomingMessage]
K --> L[Broadcast OutgoingMessage]
J --> M["dispatch_configs()"]
J --> N[Update Config Channels]
style A fill:#f9f,stroke:#333,stroke-width:1px
style B fill:#ccf,stroke:#333,stroke-width:1px
style C fill:#cfc,stroke:#333,stroke-width:1px
style G fill:#fcf,stroke:#333,stroke-width:1px
style H fill:#cff,stroke:#333,stroke-width:1px
style J fill:#ffc,stroke:#333,stroke-width:1px

Usage Example of Server Launch

fn main() -> anyhow::Result<()> {
    server::run()
}

This will start the server process, parse command-line arguments, load configuration, and run all necessary background tasks.


References to Relevant Topics