server.rs

Overview

This file implements the UDP-based server component for the Chitchat gossip protocol, facilitating peer-to-peer communication within a distributed system cluster. It provides the core logic to send and receive gossip messages, manage node liveness, and perform periodic gossip exchanges to synchronize cluster state. The server also handles DNS resolution for seed nodes with dynamic hostnames, ensuring cluster resilience and partition tolerance.

Key functionalities include:


Public Types and Functions

ChitchatHandle

A handle representing the running Chitchat server instance. It encapsulates control channels and the running task handle.

Fields

Methods

Usage Example

let handle: ChitchatHandle = spawn_chitchat(config, initial_kvs, &transport).await?;
handle.gossip(peer_addr)?;
handle.shutdown().await?;

spawn_chitchat

pub async fn spawn_chitchat(
    config: ChitchatConfig,
    initial_key_values: Vec<(String, String)>,
    transport: &dyn Transport,
) -> anyhow::Result<ChitchatHandle>

Starts a new Chitchat UDP server as a Tokio background task.


ChitchatRef

A concurrency-safe wrapper around Chitchat that provides mutex-guarded access.


Internal Types and Functions

Server

The core UDP server struct that handles message reception, processing, and gossiping.

Fields

Methods


Command Enum

enum Command {
    Gossip(SocketAddr),
    Shutdown,
}

Commands sent to the server task for control operations:


DNS Seed Resolution


Gossip Peer Selection


Implementation Details and Algorithms


Interaction with Other Components


Visual Diagram

classDiagram
class ChitchatHandle {
+chitchat_id: ChitchatId
+command_tx: UnboundedSender<Command>
+chitchat: ChitchatRef
+join_handle: JoinHandle<Result<(), anyhow::Error>>
+abort()
+chitchat_id()
+chitchat()
+with_chitchat()
+shutdown()
+gossip()
}
class Server {
-command_rx: UnboundedReceiver<Command>
-chitchat: ChitchatRef
-socket: Box<dyn Socket>
-rng: SmallRng
+new()
+run()
+handle_message()
+gossip_multiple()
+gossip()
}
class ChitchatRef {
-inner: Arc<Mutex<Chitchat>>
+new()
+lock()
}
class Command {
<<enum>>
Gossip
Shutdown
}
ChitchatHandle "1" o-- "1" ChitchatRef
Server "1" o-- "1" ChitchatRef
Server "1" ..> Command : receives
ChitchatHandle "1" --> Command : sends

Testing

The module includes extensive tests validating:

Tests use in-memory channel transport and simulated cluster configurations to verify behavior without real network dependencies.


Constants


Related Topics