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#![warn(clippy::clone_on_ref_ptr)]
63#![deny(unsafe_code)]
64#![deny(unsafe_op_in_unsafe_fn)]
65#![deny(nonstandard_style)]
66#![deny(missing_debug_implementations)]
67#![deny(clippy::missing_errors_doc)]
68#![deny(clippy::missing_panics_doc)]
69#![deny(rustdoc::broken_intra_doc_links)]
70#![allow(
71 clippy::inline_always,
72 reason = "hot-path functions use #[inline(always)] intentionally for constant-folding"
73)]
74#![allow(
75 clippy::manual_let_else,
76 reason = "match can be clearer than let-else for some patterns"
77)]
78#![allow(
79 clippy::cast_possible_truncation,
80 clippy::cast_precision_loss,
81 clippy::cast_sign_loss,
82 reason = "rate limiter and backoff arithmetic requires intentional narrowing casts"
83)]
84#![allow(
85 clippy::too_many_lines,
86 reason = "network client functions with connection management are complex by nature"
87)]
88#![allow(
89 clippy::assert_is_empty,
90 reason = "`assert!(x.is_empty())` is clearer than comparing against an empty value"
91)]
92// pyo3's `from_py_object` generates `.clone()` on `Copy` fields that clippy flags from the
93// macro expansion; an item-level `allow` cannot reach the expansion
94#![allow(clippy::clone_on_copy)]
95
96#[cfg(all(feature = "simulation", madsim, feature = "turmoil"))]
97compile_error!("madsim simulation and turmoil must run in separate builds");
98
99pub mod backoff;
100pub mod dst;
101pub mod error;
102pub mod http;
103pub mod mode;
104pub mod net;
105pub mod ratelimiter;
106pub mod retry;
107pub mod socket;
108pub mod transport;
109pub mod websocket;
110
111mod heartbeat;
112mod logging;
113mod sink;
114mod tls;
115mod writer;
116
117#[cfg(feature = "python")]
118pub mod python;
119
120pub use logging::{ConnectionUrl, url_origin};
121pub use sink::{SocketState, SocketStateSink};
122pub use transport::{Message, TransportError};
123pub use writer::WriterSender;
124
125/// Sentinel message indicating that a WebSocket reconnection completed.
126pub const RECONNECTED: &str = "__RECONNECTED__";