openzeppelin_monitor/services/blockchain/transports/ws/
config.rs1use crate::models::Network;
7use std::time::Duration;
8
9#[derive(Clone, Debug)]
11pub struct WsConfig {
12 pub heartbeat_interval: Duration,
15 pub reconnect_timeout: Duration,
18 pub max_reconnect_attempts: u32,
21 pub connection_timeout: Duration,
24 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 pub fn new() -> Self {
47 Self::default()
48 }
49
50 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 pub fn from_network(_network: &Network) -> Self {
73 Self::default()
74 }
75
76 pub fn with_heartbeat_interval(mut self, heartbeat_interval: Duration) -> Self {
84 self.heartbeat_interval = heartbeat_interval;
85 self
86 }
87
88 pub fn with_reconnect_timeout(mut self, reconnect_timeout: Duration) -> Self {
96 self.reconnect_timeout = reconnect_timeout;
97 self
98 }
99
100 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 pub fn with_connection_timeout(mut self, connection_timeout: Duration) -> Self {
120 self.connection_timeout = connection_timeout;
121 self
122 }
123
124 pub fn with_message_timeout(mut self, message_timeout: Duration) -> Self {
132 self.message_timeout = message_timeout;
133 self
134 }
135
136 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}