Skip to main content

bybit/ws/
config.rs

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    /// WebSocket server URL
10    pub url: String,
11
12    /// Maximum number of pending commands (backpressure)
13    pub command_queue_size: usize,
14
15    /// Maximum number of buffered events (backpressure)
16    pub event_queue_size: usize,
17
18    /// Maximum number of consecutive reconnect attempts without a successful
19    /// connection (0 = no reconnect). The counter resets once a connection is
20    /// established.
21    pub max_reconnect_attempts: u32,
22
23    /// Base delay between reconnect attempts
24    pub reconnect_base_delay: Duration,
25
26    /// Maximum delay cap for exponential back-off
27    pub reconnect_max_delay: Duration,
28
29    /// How long to wait for a clean close handshake
30    pub close_timeout: Duration,
31
32    /// How long to wait for the TCP/TLS/WebSocket handshake to complete.
33    pub connect_timeout: Duration,
34
35    /// How often to send a Ping frame to the server.
36    /// `None` disables the heartbeat entirely.
37    pub ping_interval: Option<Duration>,
38
39    /// How long to wait for a Pong after sending a Ping before treating the
40    /// connection as dead and triggering a reconnect.
41    /// Ignored when `ping_interval` is `None`.
42    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}