Skip to main content

binance/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(60);
5pub const DEFAULT_CONNECTION_TTL: Duration = Duration::from_hours(24);
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 reconnect attempts (0 = no reconnect)
19    pub max_reconnect_attempts: u32,
20
21    /// Base delay between reconnect attempts
22    pub reconnect_base_delay: Duration,
23
24    /// Maximum delay cap for exponential back-off
25    pub reconnect_max_delay: Duration,
26
27    /// How long to wait for a clean close handshake
28    pub close_timeout: Duration,
29
30    /// Expected interval between heartbeat pings from the server.
31    /// Per Binance Spot docs, the server sends a ping every 20s. Used as the
32    /// initial deadline for the first server ping; if exceeded, the connection
33    /// is assumed dead and a reconnect is scheduled.
34    pub ping_interval: Duration,
35
36    /// Maximum time to wait for the next ping after we last responded with a
37    /// pong before declaring the connection dead. Per Binance Spot docs, the
38    /// server disconnects if no pong is received within 60s.
39    pub pong_timeout: Duration,
40
41    /// Maximum lifetime of a single websocket connection before a proactive
42    /// reconnect. Per Binance Spot docs, a connection is only valid for 24h.
43    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}