moonpool_transport/peer/
config.rs

1//! Configuration structures for peer behavior.
2
3use std::time::Duration;
4
5/// Configuration for peer behavior and reconnection parameters.
6#[derive(Clone, Debug)]
7pub struct PeerConfig {
8    /// Initial delay before attempting reconnection
9    pub initial_reconnect_delay: Duration,
10
11    /// Maximum delay between reconnection attempts
12    pub max_reconnect_delay: Duration,
13
14    /// Maximum number of messages to queue when disconnected
15    pub max_queue_size: usize,
16
17    /// Timeout for connection attempts
18    pub connection_timeout: Duration,
19
20    /// Maximum number of consecutive connection failures before giving up
21    /// None means unlimited retries
22    pub max_connection_failures: Option<u32>,
23}
24
25impl Default for PeerConfig {
26    fn default() -> Self {
27        Self {
28            initial_reconnect_delay: Duration::from_millis(100),
29            max_reconnect_delay: Duration::from_secs(30),
30            max_queue_size: 1000,
31            connection_timeout: Duration::from_secs(5),
32            max_connection_failures: None, // Unlimited retries by default
33        }
34    }
35}
36
37impl PeerConfig {
38    /// Create a new configuration with specified parameters.
39    pub fn new(
40        max_queue_size: usize,
41        connection_timeout: Duration,
42        initial_reconnect_delay: Duration,
43        max_reconnect_delay: Duration,
44        max_connection_failures: Option<u32>,
45    ) -> Self {
46        Self {
47            initial_reconnect_delay,
48            max_reconnect_delay,
49            max_queue_size,
50            connection_timeout,
51            max_connection_failures,
52        }
53    }
54
55    /// Create a configuration for low-latency local networking.
56    pub fn local_network() -> Self {
57        Self {
58            initial_reconnect_delay: Duration::from_millis(10),
59            max_reconnect_delay: Duration::from_secs(1),
60            max_queue_size: 100,
61            connection_timeout: Duration::from_millis(500),
62            max_connection_failures: Some(10),
63        }
64    }
65
66    /// Create a configuration for high-latency WAN networking.
67    pub fn wan_network() -> Self {
68        Self {
69            initial_reconnect_delay: Duration::from_millis(500),
70            max_reconnect_delay: Duration::from_secs(60),
71            max_queue_size: 5000,
72            connection_timeout: Duration::from_secs(30),
73            max_connection_failures: None, // Unlimited retries for WAN
74        }
75    }
76}