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