nautilus_network/socket/config.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3// https://nautechsystems.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Socket configuration.
17//!
18//! # Reconnection Strategy
19//!
20//! The default configuration uses unlimited reconnection attempts (`reconnect_max_attempts: None`).
21//! This is intentional for trading systems because:
22//! - Venues may be down for extended periods but eventually recover.
23//! - Exponential backoff already prevents resource waste.
24//! - Automatic recovery can be useful when manual intervention is not desirable.
25//!
26//! Use `Some(n)` primarily for testing, development, or non-critical connections.
27
28use std::fmt::Debug;
29
30use tokio_tungstenite::tungstenite::stream::Mode;
31
32use super::types::TcpMessageHandler;
33
34/// Configuration for TCP socket connection.
35#[cfg_attr(
36 feature = "python",
37 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.network")
38)]
39pub struct SocketConfig {
40 /// The URL to connect to.
41 pub url: String,
42 /// The connection mode {Plain, TLS}.
43 pub mode: Mode,
44 /// The sequence of bytes which separates lines.
45 pub suffix: Vec<u8>,
46 /// The optional function to handle incoming messages.
47 pub message_handler: Option<TcpMessageHandler>,
48 /// The optional heartbeat with period and beat message.
49 pub heartbeat: Option<(u64, Vec<u8>)>,
50 /// The timeout (milliseconds) for reconnection attempts.
51 pub reconnect_timeout_ms: Option<u64>,
52 /// The initial reconnection delay (milliseconds) for reconnects.
53 pub reconnect_delay_initial_ms: Option<u64>,
54 /// The maximum reconnect delay (milliseconds) for exponential backoff.
55 pub reconnect_delay_max_ms: Option<u64>,
56 /// The exponential backoff factor for reconnection delays.
57 pub reconnect_backoff_factor: Option<f64>,
58 /// The maximum jitter (milliseconds) added to reconnection delays.
59 pub reconnect_jitter_ms: Option<u64>,
60 /// The maximum number of initial connection attempts (default: 5).
61 pub connection_max_retries: Option<u32>,
62 /// The maximum number of reconnection attempts before giving up.
63 /// - `None`: Unlimited reconnection attempts (default, recommended for production).
64 /// - `Some(n)`: After n failed attempts, transition to CLOSED state.
65 pub reconnect_max_attempts: Option<u32>,
66 /// The path to the certificates directory.
67 pub certs_dir: Option<String>,
68}
69
70impl Debug for SocketConfig {
71 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72 f.debug_struct(stringify!(SocketConfig))
73 .field("url", &self.url)
74 .field("mode", &self.mode)
75 .field("suffix", &self.suffix)
76 .field(
77 "message_handler",
78 &self.message_handler.as_ref().map(|_| "<function>"),
79 )
80 .field("heartbeat", &self.heartbeat)
81 .field("reconnect_timeout_ms", &self.reconnect_timeout_ms)
82 .field(
83 "reconnect_delay_initial_ms",
84 &self.reconnect_delay_initial_ms,
85 )
86 .field("reconnect_delay_max_ms", &self.reconnect_delay_max_ms)
87 .field("reconnect_backoff_factor", &self.reconnect_backoff_factor)
88 .field("reconnect_jitter_ms", &self.reconnect_jitter_ms)
89 .field("connection_max_retries", &self.connection_max_retries)
90 .field("reconnect_max_attempts", &self.reconnect_max_attempts)
91 .field("certs_dir", &self.certs_dir)
92 .finish()
93 }
94}
95
96impl Clone for SocketConfig {
97 fn clone(&self) -> Self {
98 Self {
99 url: self.url.clone(),
100 mode: self.mode,
101 suffix: self.suffix.clone(),
102 message_handler: self.message_handler.clone(),
103 heartbeat: self.heartbeat.clone(),
104 reconnect_timeout_ms: self.reconnect_timeout_ms,
105 reconnect_delay_initial_ms: self.reconnect_delay_initial_ms,
106 reconnect_delay_max_ms: self.reconnect_delay_max_ms,
107 reconnect_backoff_factor: self.reconnect_backoff_factor,
108 reconnect_jitter_ms: self.reconnect_jitter_ms,
109 connection_max_retries: self.connection_max_retries,
110 reconnect_max_attempts: self.reconnect_max_attempts,
111 certs_dir: self.certs_dir.clone(),
112 }
113 }
114}