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);
5
6#[derive(Debug, Clone)]
7pub struct Config {
8    /// WebSocket server URL
9    pub url: String,
10
11    /// Maximum number of pending commands (backpressure)
12    pub command_queue_size: usize,
13
14    /// Maximum number of buffered events (backpressure)
15    pub event_queue_size: usize,
16
17    /// Maximum reconnect attempts (0 = no reconnect)
18    pub max_reconnect_attempts: u32,
19
20    /// Base delay between reconnect attempts
21    pub reconnect_base_delay: Duration,
22
23    /// Maximum delay cap for exponential back-off
24    pub reconnect_max_delay: Duration,
25
26    /// How long to wait for a clean close handshake
27    pub close_timeout: Duration,
28
29    /// How often to send a Ping frame to the server.
30    /// `None` disables the heartbeat entirely.
31    pub ping_interval: Option<Duration>,
32
33    /// How long to wait for a Pong after sending a Ping before treating the
34    /// connection as dead and triggering a reconnect.
35    /// Ignored when `ping_interval` is `None`.
36    pub pong_timeout: Duration,
37}
38
39impl Default for Config {
40    fn default() -> Self {
41        Self {
42            url: String::new(),
43            command_queue_size: 64,
44            event_queue_size: 256,
45            max_reconnect_attempts: 5,
46            reconnect_base_delay: Duration::from_millis(500),
47            reconnect_max_delay: Duration::from_secs(30),
48            close_timeout: Duration::from_secs(5),
49            ping_interval: Some(DEFAULT_PING_INTERVAL),
50            pong_timeout: DEFAULT_PONG_TIMEOUT,
51        }
52    }
53}
54
55impl Config {
56    pub fn new(url: impl Into<String>) -> Self {
57        Self {
58            url: url.into(),
59            ..Default::default()
60        }
61    }
62
63    pub fn command_queue_size(mut self, n: usize) -> Self {
64        self.command_queue_size = n;
65        self
66    }
67
68    pub fn event_queue_size(mut self, n: usize) -> Self {
69        self.event_queue_size = n;
70        self
71    }
72
73    pub fn max_reconnect_attempts(mut self, n: u32) -> Self {
74        self.max_reconnect_attempts = n;
75        self
76    }
77
78    pub fn reconnect_base_delay(mut self, d: Duration) -> Self {
79        self.reconnect_base_delay = d;
80        self
81    }
82
83    pub fn reconnect_max_delay(mut self, d: Duration) -> Self {
84        self.reconnect_max_delay = d;
85        self
86    }
87
88    pub fn close_timeout(mut self, d: Duration) -> Self {
89        self.close_timeout = d;
90        self
91    }
92
93    pub fn ping_interval(mut self, d: Option<Duration>) -> Self {
94        self.ping_interval = d;
95        self
96    }
97
98    pub fn pong_timeout(mut self, d: Duration) -> Self {
99        self.pong_timeout = d;
100        self
101    }
102}