Skip to main content

samod_core/network/
connector_config.rs

1use std::time::Duration;
2
3use url::Url;
4
5/// Configuration for a new dialer.
6///
7/// A dialer actively establishes outgoing connections and automatically
8/// reconnects with exponential backoff when a connection is lost.
9#[derive(Debug, Clone)]
10pub struct DialerConfig {
11    /// The URL to connect to (e.g. "wss://sync.example.com/automerge").
12    pub url: Url,
13    /// Backoff configuration for reconnection attempts.
14    pub backoff: BackoffConfig,
15}
16
17/// Configuration for a new listener.
18///
19/// A listener passively accepts inbound connections. It never initiates
20/// connections and has no retry logic.
21#[derive(Debug, Clone)]
22pub struct ListenerConfig {
23    /// URL identifying this listener endpoint (e.g. "ws://0.0.0.0:8080").
24    /// Used for logging and identifying the endpoint in debugging output.
25    pub url: Url,
26}
27
28/// Configuration for exponential backoff with jitter.
29///
30/// Used by dialers to control reconnection timing after a connection
31/// is lost or a transport establishment fails.
32///
33/// The backoff formula is:
34/// ```text
35/// delay = min(initial_delay * 2^attempts, max_delay)
36/// jittered_delay = delay * random(0.5, 1.0)
37/// ```
38#[derive(Debug, Clone)]
39pub struct BackoffConfig {
40    /// Delay before the first reconnection attempt.
41    pub initial_delay: Duration,
42    /// Maximum delay between reconnection attempts.
43    pub max_delay: Duration,
44    /// Maximum number of reconnection attempts before giving up.
45    /// `None` means retry forever.
46    pub max_retries: Option<u32>,
47}
48
49impl Default for BackoffConfig {
50    fn default() -> Self {
51        Self {
52            initial_delay: Duration::from_millis(100),
53            max_delay: Duration::from_secs(30),
54            max_retries: None,
55        }
56    }
57}