1use std::time::Duration;
2
3pub const DEFAULT_PING_INTERVAL: Duration = Duration::from_secs(20);
4pub const DEFAULT_PONG_TIMEOUT: Duration = Duration::from_secs(60);
5pub const DEFAULT_CONNECTION_TTL: Duration = Duration::from_hours(24);
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,
20
21 pub reconnect_base_delay: Duration,
23
24 pub reconnect_max_delay: Duration,
26
27 pub close_timeout: Duration,
29
30 pub ping_interval: Duration,
35
36 pub pong_timeout: Duration,
40
41 pub connection_ttl: Duration,
44}
45
46impl Default for Config {
47 fn default() -> Self {
48 Self {
49 url: String::new(),
50 command_queue_size: 64,
51 event_queue_size: 256,
52 max_reconnect_attempts: 5,
53 reconnect_base_delay: Duration::from_millis(500),
54 reconnect_max_delay: Duration::from_secs(30),
55 close_timeout: Duration::from_secs(5),
56 ping_interval: DEFAULT_PING_INTERVAL,
57 pong_timeout: DEFAULT_PONG_TIMEOUT,
58 connection_ttl: DEFAULT_CONNECTION_TTL,
59 }
60 }
61}
62
63impl Config {
64 pub fn new(url: impl Into<String>) -> Self {
65 Self {
66 url: url.into(),
67 ..Default::default()
68 }
69 }
70
71 pub fn command_queue_size(mut self, n: usize) -> Self {
72 self.command_queue_size = n;
73 self
74 }
75
76 pub fn event_queue_size(mut self, n: usize) -> Self {
77 self.event_queue_size = n;
78 self
79 }
80
81 pub fn max_reconnect_attempts(mut self, n: u32) -> Self {
82 self.max_reconnect_attempts = n;
83 self
84 }
85
86 pub fn reconnect_base_delay(mut self, d: Duration) -> Self {
87 self.reconnect_base_delay = d;
88 self
89 }
90
91 pub fn reconnect_max_delay(mut self, d: Duration) -> Self {
92 self.reconnect_max_delay = d;
93 self
94 }
95
96 pub fn close_timeout(mut self, d: Duration) -> Self {
97 self.close_timeout = d;
98 self
99 }
100
101 pub fn ping_interval(mut self, d: Duration) -> Self {
102 self.ping_interval = d;
103 self
104 }
105
106 pub fn pong_timeout(mut self, d: Duration) -> Self {
107 self.pong_timeout = d;
108 self
109 }
110
111 pub fn connection_ttl(mut self, d: Duration) -> Self {
112 self.connection_ttl = d;
113 self
114 }
115}