Skip to main content

nautilus_network/
lib.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//! Network clients and connection policy for [NautilusTrader](https://nautilustrader.io).
17//!
18//! The crate provides asynchronous HTTP, reconnecting WebSocket, and suffix-framed TCP clients,
19//! together with rate limiting, retry, backoff, proxy, and TLS support.
20//!
21//! # NautilusTrader
22//!
23//! [NautilusTrader](https://nautilustrader.io) is an open-source, production-grade, Rust-native
24//! engine for multi-asset, multi-venue trading systems.
25//!
26//! The system spans research, deterministic simulation, and live execution within a single
27//! event-driven architecture, providing research-to-live semantic parity.
28//!
29//! # Feature Flags
30//!
31//! This crate provides feature flags to control source code inclusion during compilation:
32//!
33//! - `extension-module`: Builds as a Python extension module.
34//! - `python`: Exposes the `TransportBackend` enum through [PyO3](https://pyo3.rs).
35//! - `simulation`: Enables deterministic simulation testing with
36//!   [MadSim](https://crates.io/crates/madsim).
37//! - `transport-sockudo` (default): Adds the [sockudo-ws](https://crates.io/crates/sockudo-ws)
38//!   WebSocket backend, selectable through `WebSocketConfig.backend`.
39//! - `turmoil`: Enables deterministic network simulation testing with
40//!   [turmoil](https://crates.io/crates/turmoil).
41//!
42//! # Testing
43//!
44//! The crate includes standard integration tests and deterministic failure-path tests using
45//! `turmoil`.
46//!
47//! To run standard tests:
48//! ```bash
49//! cargo nextest run -p nautilus-network
50//! ```
51//!
52//! To run turmoil network simulation tests:
53//! ```bash
54//! cargo nextest run -p nautilus-network --features turmoil
55//! ```
56//!
57//! The `turmoil` tests cover reconnections, partitions, and related network failures without
58//! relying on wall-clock timing.
59
60#![warn(rustc::all)]
61#![warn(clippy::pedantic)]
62#![deny(unsafe_code)]
63#![deny(unsafe_op_in_unsafe_fn)]
64#![deny(nonstandard_style)]
65#![deny(missing_debug_implementations)]
66#![deny(clippy::missing_errors_doc)]
67#![deny(clippy::missing_panics_doc)]
68#![deny(rustdoc::broken_intra_doc_links)]
69#![allow(
70    clippy::inline_always,
71    reason = "hot-path functions use #[inline(always)] intentionally for constant-folding"
72)]
73#![allow(
74    clippy::manual_let_else,
75    reason = "match can be clearer than let-else for some patterns"
76)]
77#![allow(
78    clippy::cast_possible_truncation,
79    clippy::cast_precision_loss,
80    clippy::cast_sign_loss,
81    reason = "rate limiter and backoff arithmetic requires intentional narrowing casts"
82)]
83#![allow(
84    clippy::too_many_lines,
85    reason = "network client functions with connection management are complex by nature"
86)]
87#![allow(
88    clippy::assert_is_empty,
89    reason = "`assert!(x.is_empty())` is clearer than comparing against an empty value"
90)]
91// pyo3's `from_py_object` generates `.clone()` on `Copy` fields that clippy flags from the
92// macro expansion; an item-level `allow` cannot reach the expansion
93#![allow(clippy::clone_on_copy)]
94
95#[cfg(all(feature = "simulation", madsim, feature = "turmoil"))]
96compile_error!("madsim simulation and turmoil must run in separate builds");
97
98pub mod backoff;
99pub mod dst;
100pub mod error;
101pub mod http;
102pub mod mode;
103pub mod net;
104pub mod ratelimiter;
105pub mod retry;
106pub mod socket;
107pub mod transport;
108pub mod websocket;
109
110mod heartbeat;
111mod logging;
112mod sink;
113mod tls;
114
115#[cfg(feature = "python")]
116pub mod python;
117
118pub use sink::{SocketState, SocketStateSink};
119pub use transport::{Message, TransportError};
120
121/// Sentinel message indicating that a WebSocket reconnection completed.
122pub const RECONNECTED: &str = "__RECONNECTED__";