1use std::time::Duration;
2
3pub const DEFAULT_PING_INTERVAL: Duration = Duration::from_secs(20);
4pub const DEFAULT_PONG_TIMEOUT: Duration = Duration::from_secs(10);
5pub const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
6
7#[derive(Debug, Clone)]
8pub struct Config {
9 pub url: String,
11
12 pub command_queue_size: usize,
14
15 pub event_queue_size: usize,
17
18 pub max_reconnect_attempts: u32,
22
23 pub reconnect_base_delay: Duration,
25
26 pub reconnect_max_delay: Duration,
28
29 pub close_timeout: Duration,
31
32 pub connect_timeout: Duration,
34
35 pub ping_interval: Option<Duration>,
38
39 pub pong_timeout: Duration,
43}
44
45impl Default for Config {
46 fn default() -> Self {
47 Self {
48 url: String::new(),
49 command_queue_size: 64,
50 event_queue_size: 256,
51 max_reconnect_attempts: 5,
52 reconnect_base_delay: Duration::from_millis(500),
53 reconnect_max_delay: Duration::from_secs(30),
54 close_timeout: Duration::from_secs(5),
55 connect_timeout: DEFAULT_CONNECT_TIMEOUT,
56 ping_interval: Some(DEFAULT_PING_INTERVAL),
57 pong_timeout: DEFAULT_PONG_TIMEOUT,
58 }
59 }
60}
61
62impl Config {
63 pub fn new(url: impl Into<String>) -> Self {
64 Self {
65 url: url.into(),
66 ..Default::default()
67 }
68 }
69
70 pub fn command_queue_size(mut self, n: usize) -> Self {
71 self.command_queue_size = n;
72 self
73 }
74
75 pub fn event_queue_size(mut self, n: usize) -> Self {
76 self.event_queue_size = n;
77 self
78 }
79
80 pub fn max_reconnect_attempts(mut self, n: u32) -> Self {
81 self.max_reconnect_attempts = n;
82 self
83 }
84
85 pub fn reconnect_base_delay(mut self, d: Duration) -> Self {
86 self.reconnect_base_delay = d;
87 self
88 }
89
90 pub fn reconnect_max_delay(mut self, d: Duration) -> Self {
91 self.reconnect_max_delay = d;
92 self
93 }
94
95 pub fn close_timeout(mut self, d: Duration) -> Self {
96 self.close_timeout = d;
97 self
98 }
99
100 pub fn connect_timeout(mut self, d: Duration) -> Self {
101 self.connect_timeout = d;
102 self
103 }
104
105 pub fn ping_interval(mut self, d: Option<Duration>) -> Self {
106 self.ping_interval = d;
107 self
108 }
109
110 pub fn pong_timeout(mut self, d: Duration) -> Self {
111 self.pong_timeout = d;
112 self
113 }
114}