1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#[cfg(feature = "ws-client")]
use crate::api::network_config::NetworkConfig;
use crate::error::NetError;
use tokio::sync::Semaphore;
/// Internal runtime, queue capacities, and default network policy used when
/// constructing [`crate::OpenNet`]. Queue capacities count pending tasks, not
/// bytes or concurrency, default to 128, must be in
/// `1..=tokio::sync::Semaphore::MAX_PERMITS`, and are fixed after engine
/// creation. [`crate::OpenNet::new_with_config`] validates them before creating
/// channels or workers and reports invalid values as
/// [`crate::error::ErrorKind::InvalidConfig`].
#[cfg_attr(
feature = "ws-client",
doc = "
When `ws-client` is enabled, configure a provider in the nested
[`NetworkConfig`](crate::network::NetworkConfig) before constructing [`crate::OpenNet`],
unless the host application has already installed a process-wide rustls provider."
)]
#[derive(Clone, Debug)]
pub struct OpenNetConfig {
pub(crate) runtime_worker_threads: Option<usize>,
pub(crate) async_queue_capacity: usize,
pub(crate) sync_queue_capacity: usize,
#[cfg(feature = "ws-client")]
pub(crate) network_config: NetworkConfig,
}
impl Default for OpenNetConfig {
fn default() -> Self {
Self {
runtime_worker_threads: None,
async_queue_capacity: 128,
sync_queue_capacity: 128,
#[cfg(feature = "ws-client")]
network_config: NetworkConfig::default(),
}
}
}
impl OpenNetConfig {
/// Sets the management runtime worker count (`1..=256`). If omitted, Tokio
/// defaults apply, including the `TOKIO_WORKER_THREADS` environment
/// variable. The explicit value is validated before creation and does not
/// cap per-client I/O, callback, management, or blocking threads.
///
/// ```no_run
/// use open_net::{NetError, OpenNet, OpenNetConfig};
///
/// #[cfg(feature = "ws-client")]
/// use open_net::network::{NetworkConfig, TlsConfig};
/// #[cfg(feature = "ws-client")]
/// use std::sync::Arc;
/// # fn example() -> Result<(), NetError> {
/// let config = OpenNetConfig::default().with_runtime_worker_threads(2);
/// #[cfg(feature = "ws-client")]
/// let config = config.with_network_config(
/// NetworkConfig::default().with_tls(
/// TlsConfig::default().with_crypto_provider(Arc::new(
/// rustls_openssl::default_provider(),
/// )),
/// ),
/// );
/// let net = OpenNet::new_with_config(config)?;
/// # Ok(())
/// # }
/// ```
pub fn with_runtime_worker_threads(mut self, threads: usize) -> Self {
self.runtime_worker_threads = Some(threads);
self
}
/// Sets the asynchronous submission queue capacity (default: 128).
/// The value is validated during engine creation and must be within the
/// semaphore-supported range.
pub fn with_async_queue_capacity(mut self, capacity: usize) -> Self {
self.async_queue_capacity = capacity;
self
}
/// Sets the synchronous-call queue capacity (default: 128). Synchronous
/// means the caller waits for a result; the underlying channel is still
/// MPSC. The value is validated during engine creation and must be within
/// the semaphore-supported range.
pub fn with_sync_queue_capacity(mut self, capacity: usize) -> Self {
self.sync_queue_capacity = capacity;
self
}
/// Sets the default WebSocket network policy; clients may override it at creation.
#[cfg(feature = "ws-client")]
pub fn with_network_config(mut self, config: NetworkConfig) -> Self {
self.network_config = config;
self
}
/// Returns the configured management-runtime worker count, if explicitly set.
pub fn runtime_worker_threads(&self) -> Option<usize> {
self.runtime_worker_threads
}
/// Returns the bounded asynchronous submission queue capacity.
pub fn async_queue_capacity(&self) -> usize {
self.async_queue_capacity
}
/// Returns the bounded synchronous submission queue capacity.
pub fn sync_queue_capacity(&self) -> usize {
self.sync_queue_capacity
}
#[cfg(feature = "ws-client")]
/// Returns the default WebSocket network policy used for new clients.
pub fn network_config(&self) -> &NetworkConfig {
&self.network_config
}
/// Validates runtime worker, queue, and nested network configuration limits.
pub fn validate(&self) -> Result<(), NetError> {
if self.runtime_worker_threads.is_some_and(|threads| {
threads == 0 || threads > crate::common::common_engine::MAX_RUNTIME_WORKER_THREADS
}) {
return Err(NetError::config(
"runtime_worker_threads",
"must be between 1 and 256",
));
}
for (field, capacity) in [
("async_queue_capacity", self.async_queue_capacity),
("sync_queue_capacity", self.sync_queue_capacity),
] {
if capacity == 0 || capacity > Semaphore::MAX_PERMITS {
return Err(NetError::config(
field,
"must be between 1 and Semaphore::MAX_PERMITS",
));
}
}
#[cfg(feature = "ws-client")]
self.network_config.validate()?;
Ok(())
}
}
#[cfg(all(test, feature = "ws-client"))]
mod validation_tests {
use super::*;
use crate::network::RootCertificateMode;
#[test]
fn engine_validation_includes_nested_network_configuration() -> Result<(), NetError> {
let mut network = NetworkConfig::default();
network.tls.root_mode = RootCertificateMode::Replace;
let config = OpenNetConfig::default().with_network_config(network);
if !matches!(config.validate(), Err(ref __classified_error_0) if matches!(__classified_error_0.kind(), crate::error::ErrorKind::InvalidConfig))
{
return Err(NetError::from(crate::error::ErrorKind::Internal));
}
Ok(())
}
}