openzeppelin_monitor/services/blockchain/transports/ws/
config.rs

1//! WebSocket configuration for blockchain transports
2//!
3//! This module provides a configuration for WebSocket transports, including heartbeat intervals,
4//! reconnect timeouts, and message timeouts.
5
6use crate::models::Network;
7use std::time::Duration;
8
9/// WebSocket configuration for blockchain transports
10#[derive(Clone, Debug)]
11pub struct WsConfig {
12	/// Heartbeat interval for WebSocket connections
13	/// How often to send keep-alive pings
14	pub heartbeat_interval: Duration,
15	/// Reconnect timeout for WebSocket connections
16	/// How long to wait before reconnecting
17	pub reconnect_timeout: Duration,
18	/// Maximum number of reconnect attempts
19	/// How many times to try reconnecting
20	pub max_reconnect_attempts: u32,
21	/// Connection timeout for WebSocket connections
22	/// How long to wait for initial connection
23	pub connection_timeout: Duration,
24	/// Message timeout for WebSocket connections
25	/// How long to wait for message responses
26	pub message_timeout: Duration,
27}
28
29impl Default for WsConfig {
30	fn default() -> Self {
31		Self {
32			heartbeat_interval: Duration::from_secs(30),
33			reconnect_timeout: Duration::from_secs(5),
34			max_reconnect_attempts: 3,
35			connection_timeout: Duration::from_secs(10),
36			message_timeout: Duration::from_secs(5),
37		}
38	}
39}
40
41impl WsConfig {
42	/// Creates a new WebSocket configuration with default values
43	///
44	/// # Returns
45	/// * `WsConfig` - A new WebSocket configuration with default values
46	pub fn new() -> Self {
47		Self::default()
48	}
49
50	/// Creates a new WebSocket configuration with a single attempt
51	///
52	/// Mostly for testing purposes
53	///
54	/// # Returns
55	/// * `WsConfig` - A new WebSocket configuration with a single attempt
56	pub fn single_attempt() -> Self {
57		Self {
58			heartbeat_interval: Duration::from_secs(30),
59			reconnect_timeout: Duration::from_secs(1),
60			max_reconnect_attempts: 1,
61			connection_timeout: Duration::from_secs(1),
62			message_timeout: Duration::from_secs(1),
63		}
64	}
65	/// Creates a new WebSocket configuration from a network
66	///
67	/// # Arguments
68	/// * `network` - The network to create the configuration from
69	///
70	/// # Returns
71	/// * `WsConfig` - A new WebSocket configuration
72	pub fn from_network(_network: &Network) -> Self {
73		Self::default()
74	}
75
76	/// Sets the heartbeat interval for the WebSocket configuration
77	///
78	/// # Arguments
79	/// * `heartbeat_interval` - The heartbeat interval to set
80	///
81	/// # Returns
82	/// * `WsConfig` - A new WebSocket configuration with the updated heartbeat interval
83	pub fn with_heartbeat_interval(mut self, heartbeat_interval: Duration) -> Self {
84		self.heartbeat_interval = heartbeat_interval;
85		self
86	}
87
88	/// Sets the reconnect timeout for the WebSocket configuration
89	///
90	/// # Arguments
91	/// * `reconnect_timeout` - The reconnect timeout to set
92	///
93	/// # Returns
94	/// * `WsConfig` - A new WebSocket configuration with the updated reconnect timeout
95	pub fn with_reconnect_timeout(mut self, reconnect_timeout: Duration) -> Self {
96		self.reconnect_timeout = reconnect_timeout;
97		self
98	}
99
100	/// Sets the maximum number of reconnect attempts for the WebSocket configuration
101	///
102	/// # Arguments
103	/// * `max_reconnect_attempts` - The maximum number of reconnect attempts to set
104	///
105	/// # Returns
106	/// * `WsConfig` - A new WebSocket configuration with the updated maximum number of reconnect attempts
107	pub fn with_max_reconnect_attempts(mut self, max_reconnect_attempts: u32) -> Self {
108		self.max_reconnect_attempts = max_reconnect_attempts;
109		self
110	}
111
112	/// Sets the connection timeout for the WebSocket configuration
113	///
114	/// # Arguments
115	/// * `connection_timeout` - The connection timeout to set
116	///
117	/// # Returns
118	/// * `WsConfig` - A new WebSocket configuration with the updated connection timeout
119	pub fn with_connection_timeout(mut self, connection_timeout: Duration) -> Self {
120		self.connection_timeout = connection_timeout;
121		self
122	}
123
124	/// Sets the message timeout for the WebSocket configuration
125	///
126	/// # Arguments
127	/// * `message_timeout` - The message timeout to set
128	///
129	/// # Returns
130	/// * `WsConfig` - A new WebSocket configuration with the updated message timeout
131	pub fn with_message_timeout(mut self, message_timeout: Duration) -> Self {
132		self.message_timeout = message_timeout;
133		self
134	}
135
136	/// Builds the WebSocket configuration
137	///
138	/// # Returns
139	/// * `WsConfig` - A new WebSocket configuration
140	pub fn build(self) -> Self {
141		Self {
142			heartbeat_interval: self.heartbeat_interval,
143			reconnect_timeout: self.reconnect_timeout,
144			max_reconnect_attempts: self.max_reconnect_attempts,
145			connection_timeout: self.connection_timeout,
146			message_timeout: self.message_timeout,
147		}
148	}
149}
150
151#[cfg(test)]
152mod tests {
153	use super::*;
154
155	#[test]
156	fn test_default_config() {
157		let config = WsConfig::default();
158		assert_eq!(config.heartbeat_interval, Duration::from_secs(30));
159		assert_eq!(config.reconnect_timeout, Duration::from_secs(5));
160		assert_eq!(config.max_reconnect_attempts, 3);
161		assert_eq!(config.connection_timeout, Duration::from_secs(10));
162		assert_eq!(config.message_timeout, Duration::from_secs(5));
163	}
164
165	#[test]
166	fn test_single_attempt_config() {
167		let config = WsConfig::single_attempt();
168		assert_eq!(config.heartbeat_interval, Duration::from_secs(30));
169		assert_eq!(config.reconnect_timeout, Duration::from_secs(1));
170		assert_eq!(config.max_reconnect_attempts, 1);
171		assert_eq!(config.connection_timeout, Duration::from_secs(1));
172		assert_eq!(config.message_timeout, Duration::from_secs(1));
173	}
174
175	#[test]
176	fn test_builder_methods() {
177		let config = WsConfig::new()
178			.with_heartbeat_interval(Duration::from_secs(60))
179			.with_reconnect_timeout(Duration::from_secs(10))
180			.with_max_reconnect_attempts(5)
181			.with_connection_timeout(Duration::from_secs(20))
182			.with_message_timeout(Duration::from_secs(15))
183			.build();
184
185		assert_eq!(config.heartbeat_interval, Duration::from_secs(60));
186		assert_eq!(config.reconnect_timeout, Duration::from_secs(10));
187		assert_eq!(config.max_reconnect_attempts, 5);
188		assert_eq!(config.connection_timeout, Duration::from_secs(20));
189		assert_eq!(config.message_timeout, Duration::from_secs(15));
190	}
191}