open-net 0.3.5

A cross-platform async networking library for Rust with HTTP and WebSocket clients, network status monitoring, configurable retries, WebSocket reconnection and heartbeats, and managed client lifecycles. Reuse common networking infrastructure across applications to avoid repeated implementation and debugging, helping save development time and AI coding tokens.
Documentation
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Independently inherited HTTP timeout settings.

use crate::api::error::NetError;
use std::time::Duration;
use tokio::time::Instant;

const DEFAULT_HTTP_ADMISSION_TIMEOUT: Duration = Duration::from_secs(30);

/// The budget that expired, independently of the stage executing at that time.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TimeoutPhase {
    /// Waiting for both admission capacity and an available queue slot.
    Admission,
    /// Establishing a new connection, including DNS, proxy negotiation, and TLS.
    Connect,
    /// Executing the transport until the final response headers are available.
    Headers,
    /// Waiting for nonempty body bytes to be delivered, including consumption pauses.
    IdleRead,
    /// Preparing and executing one attempt, through completion of its response body.
    Attempt,
    /// Completing the logical operation, including admission, middleware, and retries.
    Total,
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
enum TimeoutSetting {
    #[default]
    Inherit,
    Disabled,
    After(Duration),
}

impl TimeoutSetting {
    fn resolve(self, client: Self, fallback: Option<Duration>) -> Option<Duration> {
        match self {
            Self::After(duration) => Some(duration),
            Self::Disabled => None,
            Self::Inherit => match client {
                Self::After(duration) => Some(duration),
                Self::Disabled => None,
                Self::Inherit => fallback,
            },
        }
    }

    fn explicit(duration: Option<Duration>) -> Self {
        match duration {
            Some(duration) => Self::After(duration),
            None => Self::Disabled,
        }
    }
}

/// Reusable per-phase timeout overrides for a client or one request.
///
/// Each setting initially inherits its parent. Explicit request settings override
/// client settings; otherwise attempt and total fall back to the legacy client
/// timeout and resolved retry policy. Disabling a setting also overrides those
/// legacy values. Replacing this object replaces the entire layer of overrides.
/// Admission independently falls back to 30 seconds when both layers inherit;
/// this fallback also applies to callback `send_wait`. Explicitly disabling
/// Admission removes that fallback while preserving any Total budget.
/// Durations must be nonzero and fit the monotonic clock; validation occurs when
/// the value is installed on a client/options value or a request is built.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct HttpTimeouts {
    admission: TimeoutSetting,
    headers: TimeoutSetting,
    idle_read: TimeoutSetting,
    attempt: TimeoutSetting,
    total: TimeoutSetting,
}

impl HttpTimeouts {
    /// Create settings that inherit every budget.
    pub fn new() -> Self {
        Self::default()
    }

    /// Limit the complete asynchronous admission wait, including queue reservation.
    /// Immediate admission APIs still fail immediately when capacity is unavailable.
    pub fn with_admission_timeout(mut self, timeout: Duration) -> Self {
        self.admission = TimeoutSetting::After(timeout);
        self
    }

    /// Limit transport execution through final response headers, including redirects.
    pub fn with_headers_timeout(mut self, timeout: Duration) -> Self {
        self.headers = TimeoutSetting::After(timeout);
        self
    }

    /// Limit pauses between delivery of nonempty body bytes, including the first byte.
    /// A retained stream continues consuming this budget while the caller is not polling.
    pub fn with_idle_read_timeout(mut self, timeout: Duration) -> Self {
        self.idle_read = TimeoutSetting::After(timeout);
        self
    }

    /// Limit one attempt from request preparation through response body completion.
    pub fn with_attempt_timeout(mut self, timeout: Duration) -> Self {
        self.attempt = TimeoutSetting::After(timeout);
        self
    }

    /// Limit the entire operation, including admission, middleware, retries, and body.
    pub fn with_total_timeout(mut self, timeout: Duration) -> Self {
        self.total = TimeoutSetting::After(timeout);
        self
    }

    /// Disable only the admission budget, including its 30 second default.
    /// A configured Total budget continues to limit admission waiting.
    pub fn without_admission_timeout(mut self) -> Self {
        self.admission = TimeoutSetting::Disabled;
        self
    }

    /// Disable only the response headers budget.
    pub fn without_headers_timeout(mut self) -> Self {
        self.headers = TimeoutSetting::Disabled;
        self
    }

    /// Disable only the body progress budget.
    pub fn without_idle_read_timeout(mut self) -> Self {
        self.idle_read = TimeoutSetting::Disabled;
        self
    }

    /// Disable the attempt budget, including legacy client and retry policy limits.
    pub fn without_attempt_timeout(mut self) -> Self {
        self.attempt = TimeoutSetting::Disabled;
        self
    }

    /// Disable the total budget, including the legacy retry policy deadline.
    pub fn without_total_timeout(mut self) -> Self {
        self.total = TimeoutSetting::Disabled;
        self
    }

    pub(crate) fn validate(&self) -> Result<(), NetError> {
        self.validate_with(false)
    }

    pub(crate) fn validate_request(&self) -> Result<(), NetError> {
        self.validate_with(true)
    }

    fn validate_with(&self, input: bool) -> Result<(), NetError> {
        for (field, setting) in [
            ("http.timeouts.admission", self.admission),
            ("http.timeouts.headers", self.headers),
            ("http.timeouts.idle_read", self.idle_read),
            ("http.timeouts.attempt", self.attempt),
            ("http.timeouts.total", self.total),
        ] {
            if let TimeoutSetting::After(duration) = setting {
                validate_duration(duration, field, input)?;
            }
        }
        Ok(())
    }

    pub(crate) fn resolve(
        &self,
        request: &Self,
        legacy_timeout: Duration,
    ) -> Result<ResolvedTimeouts, NetError> {
        self.validate()?;
        request.validate_request()?;
        validate_duration(legacy_timeout, "http.timeout", false)?;
        let legacy_attempt = Some(legacy_timeout);
        let resolved = ResolvedTimeouts {
            admission: request
                .admission
                .resolve(self.admission, Some(DEFAULT_HTTP_ADMISSION_TIMEOUT)),
            headers: request.headers.resolve(self.headers, None),
            idle_read: request.idle_read.resolve(self.idle_read, None),
            attempt: request.attempt.resolve(self.attempt, legacy_attempt),
            total: request.total.resolve(self.total, None),
        };
        resolved.as_overrides().validate()?;
        Ok(resolved)
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct ResolvedTimeouts {
    pub(crate) admission: Option<Duration>,
    pub(crate) headers: Option<Duration>,
    pub(crate) idle_read: Option<Duration>,
    pub(crate) attempt: Option<Duration>,
    pub(crate) total: Option<Duration>,
}

impl ResolvedTimeouts {
    pub(crate) fn as_overrides(self) -> HttpTimeouts {
        HttpTimeouts {
            admission: TimeoutSetting::explicit(self.admission),
            headers: TimeoutSetting::explicit(self.headers),
            idle_read: TimeoutSetting::explicit(self.idle_read),
            attempt: TimeoutSetting::explicit(self.attempt),
            total: TimeoutSetting::explicit(self.total),
        }
    }
}

pub(crate) fn validate_duration(
    duration: Duration,
    field: &'static str,
    input: bool,
) -> Result<(), NetError> {
    let reason = if duration.is_zero() {
        Some("timeout must be nonzero")
    } else if Instant::now().checked_add(duration).is_none() {
        Some("timeout exceeds the monotonic clock range")
    } else {
        None
    };
    match reason {
        Some(reason) if input => Err(NetError::input(field, reason)),
        Some(reason) => Err(NetError::config(field, reason)),
        None => Ok(()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::error::{ErrorKind, ErrorStage};
    use crate::api::http::{HttpClientConfig, HttpRequest, HttpRequestOptions};

    type TestResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;

    fn check(condition: bool, message: &str) -> TestResult {
        if condition {
            Ok(())
        } else {
            Err(std::io::Error::other(message).into())
        }
    }

    #[test]
    fn inheritance_is_per_field_and_explicit_disable_replaces_legacy() -> TestResult {
        let client = HttpTimeouts::new()
            .with_attempt_timeout(Duration::from_secs(7))
            .with_headers_timeout(Duration::from_secs(5))
            .with_total_timeout(Duration::from_secs(30));
        let request = HttpTimeouts::new().with_total_timeout(Duration::from_secs(8));
        let resolved = client.resolve(&request, Duration::from_secs(30))?;
        check(
            resolved
                == ResolvedTimeouts {
                    admission: Some(Duration::from_secs(30)),
                    headers: Some(Duration::from_secs(5)),
                    idle_read: None,
                    attempt: Some(Duration::from_secs(7)),
                    total: Some(Duration::from_secs(8)),
                },
            "request overrides must preserve unrelated inherited settings",
        )?;
        let disabled = client.resolve(
            &HttpTimeouts::new()
                .without_attempt_timeout()
                .without_total_timeout(),
            Duration::from_secs(30),
        )?;
        check(
            disabled.attempt.is_none()
                && disabled.total.is_none()
                && disabled.headers == Some(Duration::from_secs(5)),
            "explicit disable must replace legacy limits and preserve other budgets",
        )?;
        let frozen = HttpTimeouts::new()
            .with_admission_timeout(Duration::from_secs(1))
            .resolve(&resolved.as_overrides(), Duration::from_secs(2))?;
        check(
            frozen == resolved,
            "resolved overrides must freeze inherited and disabled values",
        )
    }

    #[test]
    fn admission_fallback_is_independent_of_attempt_and_timeout_replacement() -> TestResult {
        for attempt in [Duration::from_millis(1), Duration::from_secs(90)] {
            let client = HttpClientConfig::new("http://127.0.0.1")?
                .with_timeout(attempt)?
                .with_timeouts(HttpTimeouts::new().with_headers_timeout(Duration::from_secs(2)))?;
            let resolved = client
                .timeouts()
                .resolve(&HttpTimeouts::new(), client.timeout)?;
            check(
                resolved.admission == Some(Duration::from_secs(30))
                    && resolved.attempt == Some(attempt),
                "admission fallback must remain 30 seconds independently of attempt timeout",
            )?;
        }
        Ok(())
    }

    #[test]
    fn admission_request_override_and_disable_replace_client_defaults() -> TestResult {
        let client = HttpTimeouts::new().with_admission_timeout(Duration::from_secs(4));
        for (request, expected) in [
            (HttpTimeouts::new(), Some(Duration::from_secs(4))),
            (
                HttpTimeouts::new().with_admission_timeout(Duration::from_secs(1)),
                Some(Duration::from_secs(1)),
            ),
            (HttpTimeouts::new().without_admission_timeout(), None),
        ] {
            let resolved = client.resolve(&request, Duration::from_secs(8))?;
            check(
                resolved.admission == expected,
                "request admission override must take precedence over client settings",
            )?;
        }
        Ok(())
    }

    #[test]
    fn disabled_admission_fallback_preserves_total_and_accepts_request_override() -> TestResult {
        let client = HttpTimeouts::new()
            .without_admission_timeout()
            .with_total_timeout(Duration::from_secs(2));
        let inherited = client.resolve(&HttpTimeouts::new(), Duration::from_secs(8))?;
        check(
            inherited.admission.is_none() && inherited.total == Some(Duration::from_secs(2)),
            "disabling admission must bypass its fallback without disabling total",
        )?;
        let explicit = client.resolve(
            &HttpTimeouts::new().with_admission_timeout(Duration::from_secs(1)),
            Duration::from_secs(8),
        )?;
        check(
            explicit.admission == Some(Duration::from_secs(1)),
            "an explicit request timeout must replace a disabled client admission budget",
        )
    }

    #[test]
    fn resolved_admission_fallback_can_be_frozen_without_reinheriting() -> TestResult {
        let resolved = HttpTimeouts::new().resolve(&HttpTimeouts::new(), Duration::from_secs(8))?;
        let frozen = HttpTimeouts::new()
            .with_admission_timeout(Duration::from_secs(90))
            .resolve(&resolved.as_overrides(), Duration::from_secs(8))?;
        check(
            resolved.admission == Some(Duration::from_secs(30)) && frozen == resolved,
            "freezing resolved timeout overrides must retain the default admission budget",
        )
    }

    #[test]
    fn zero_admission_timeout_remains_invalid_at_both_configuration_layers() -> TestResult {
        let invalid = HttpTimeouts::new().with_admission_timeout(Duration::ZERO);
        check(
            HttpClientConfig::new("http://127.0.0.1")?
                .with_timeouts(invalid.clone())
                .is_err(),
            "a zero client admission timeout must be rejected",
        )?;
        check(
            HttpRequest::builder()
                .path("/")
                .timeouts(invalid)
                .build()
                .is_err(),
            "a zero request admission timeout must be rejected",
        )
    }

    #[test]
    fn legacy_fallback_and_repeated_installation_are_order_independent() -> TestResult {
        let inherited =
            HttpTimeouts::new().resolve(&HttpTimeouts::new(), Duration::from_secs(30))?;
        check(
            inherited.attempt == Some(Duration::from_secs(30)) && inherited.total.is_none(),
            "client timeout is the only legacy fallback and does not create a total limit",
        )?;
        let client = HttpClientConfig::new("http://127.0.0.1")?
            .with_timeouts(HttpTimeouts::new().with_headers_timeout(Duration::from_secs(1)))?
            .with_timeouts(HttpTimeouts::new().without_attempt_timeout())?
            .with_timeout(Duration::from_secs(2))?;
        let resolved = client
            .timeouts()
            .resolve(&HttpTimeouts::new(), client.timeout)?;
        check(
            resolved.headers.is_none() && resolved.attempt.is_none(),
            "installing a replacement must reset other fields and override later legacy changes",
        )?;
        let request = HttpRequest::builder()
            .path("/")
            .timeouts(HttpTimeouts::new().with_headers_timeout(Duration::from_secs(2)))
            .timeouts(HttpTimeouts::new().with_total_timeout(Duration::from_secs(3)))
            .build()?;
        let options = HttpRequestOptions::new()
            .with_timeouts(HttpTimeouts::new().without_attempt_timeout())?
            .with_timeouts(HttpTimeouts::new())?;
        let resolved = HttpTimeouts::new().resolve(request.timeouts(), client.timeout)?;
        check(
            resolved.headers.is_none(),
            "request replacement must not merge old fields",
        )?;
        check(
            options.timeouts() == &HttpTimeouts::new(),
            "options replacement must reset to inherit",
        )?;
        let repeated = HttpTimeouts::new()
            .with_total_timeout(Duration::from_secs(1))
            .without_total_timeout()
            .with_total_timeout(Duration::from_secs(4))
            .resolve(&HttpTimeouts::new(), Duration::from_secs(2))?;
        check(
            repeated.total == Some(Duration::from_secs(4)),
            "last setting must win",
        )
    }

    #[test]
    fn invalid_durations_have_precise_fields_at_every_installation_boundary() -> TestResult {
        for duration in [Duration::ZERO, Duration::MAX] {
            let invalid = HttpTimeouts::new().with_total_timeout(duration);
            let config = HttpClientConfig::new("http://127.0.0.1")?.with_timeouts(invalid.clone());
            let options = HttpRequestOptions::new().with_timeouts(invalid.clone());
            let request = HttpRequest::builder().path("/").timeouts(invalid).build();
            for (error, kind, stage) in [
                (
                    config.err(),
                    ErrorKind::InvalidConfig,
                    ErrorStage::Configuration,
                ),
                (
                    options.err(),
                    ErrorKind::InvalidInput,
                    ErrorStage::RequestBuild,
                ),
                (
                    request.err(),
                    ErrorKind::InvalidInput,
                    ErrorStage::RequestBuild,
                ),
            ] {
                let error =
                    error.ok_or_else(|| std::io::Error::other("invalid timeout accepted"))?;
                check(error.kind() == kind, "wrong validation error kind")?;
                check(
                    error.context().stage == Some(stage),
                    "wrong validation stage",
                )?;
                check(
                    error.config_error().map(|value| value.field()) == Some("http.timeouts.total"),
                    "validation must identify the exact phase field",
                )?;
            }
            let connect = HttpClientConfig::new("http://127.0.0.1")?.with_connect_timeout(duration);
            check(connect.is_err(), "invalid connect timeout accepted")?;
        }
        let config = HttpClientConfig::new("http://127.0.0.1")?
            .with_connect_timeout(Duration::from_secs(2))?
            .without_connect_timeout();
        check(
            config.connect_timeout().is_none(),
            "connect timeout did not disable",
        )
    }
}