nautilus_network/websocket/config.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2026 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//! Configuration for WebSocket client connections.
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
30/// Configuration for WebSocket client connections.
31///
32/// This struct contains only static configuration settings. Runtime callbacks
33/// (message handler, ping handler) are passed separately to `connect()`.
34///
35/// # Connection Modes
36///
37/// ## Handler Mode
38///
39/// - Use with [`crate::websocket::WebSocketClient::connect`].
40/// - Pass a message handler to `connect()` to receive messages via callback.
41/// - Client spawns internal task to read messages and call handler.
42/// - Supports automatic reconnection with exponential backoff.
43/// - Reconnection config fields (`reconnect_*`) are active.
44/// - Best for long-lived connections, Python bindings, callback-based APIs.
45///
46/// ## Stream Mode
47///
48/// - Use with [`crate::websocket::WebSocketClient::connect_stream`].
49/// - Returns a [`MessageReader`](super::types::MessageReader) stream for the caller to read from.
50/// - **Does NOT support automatic reconnection** (reader owned by caller).
51/// - Reconnection config fields are ignored.
52/// - On disconnect, client transitions to CLOSED state and caller must manually reconnect.
53#[cfg_attr(
54 feature = "python",
55 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.network")
56)]
57#[derive(Clone, Debug)]
58pub struct WebSocketConfig {
59 /// The URL to connect to.
60 pub url: String,
61 /// The default headers.
62 pub headers: Vec<(String, String)>,
63 /// The optional heartbeat interval (seconds).
64 pub heartbeat: Option<u64>,
65 /// The optional heartbeat message.
66 pub heartbeat_msg: Option<String>,
67 /// The timeout (milliseconds) for reconnection attempts.
68 /// **Note**: Only applies to handler mode. Ignored in stream mode.
69 pub reconnect_timeout_ms: Option<u64>,
70 /// The initial reconnection delay (milliseconds) for reconnects.
71 /// **Note**: Only applies to handler mode. Ignored in stream mode.
72 pub reconnect_delay_initial_ms: Option<u64>,
73 /// The maximum reconnect delay (milliseconds) for exponential backoff.
74 /// **Note**: Only applies to handler mode. Ignored in stream mode.
75 pub reconnect_delay_max_ms: Option<u64>,
76 /// The exponential backoff factor for reconnection delays.
77 /// **Note**: Only applies to handler mode. Ignored in stream mode.
78 pub reconnect_backoff_factor: Option<f64>,
79 /// The maximum jitter (milliseconds) added to reconnection delays.
80 /// **Note**: Only applies to handler mode. Ignored in stream mode.
81 pub reconnect_jitter_ms: Option<u64>,
82 /// The maximum number of reconnection attempts before giving up.
83 /// **Note**: Only applies to handler mode. Ignored in stream mode.
84 /// - `None`: Unlimited reconnection attempts (default, recommended for production).
85 /// - `Some(n)`: After n failed attempts, transition to CLOSED state.
86 pub reconnect_max_attempts: Option<u32>,
87}