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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! HTTP-specific retry classification and request overrides.
use crate::api::error::{ErrorKind, ErrorStage, NetError};
use crate::api::http::{HttpRequestId, HttpResponse, HttpTimeouts};
use crate::api::retry::{
    RetryConfigError, RetryDecision, RetryObserver, RetryObserverHandle, RetrySchedule,
    RetrySetting,
};
use http::{Method, StatusCode};
use std::fmt;
use std::sync::Arc;
use std::time::Duration;

/// How HTTP retry delays incorporate a server-provided lower bound.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum RetryAfterMode {
    #[default]
    Ignore,
    Respect,
}
impl RetryAfterMode {
    /// Apply the server hint after the schedule has applied jitter.
    pub fn select(self, server_delay: Option<Duration>, backoff_delay: Duration) -> Duration {
        match (self, server_delay) {
            (Self::Respect, Some(delay)) => delay.max(backoff_delay),
            _ => backoff_delay,
        }
    }
}

/// Safety declaration required before a request may be replayed.
#[derive(Clone, Eq, PartialEq)]
pub enum RequestSafety {
    /// Never replay this operation.
    Never,
    /// Replay only methods that are defined as idempotent by HTTP.
    Automatic,
    /// The endpoint contract guarantees that the operation is idempotent.
    IdempotentByContract,
    /// The endpoint accepts this key and can deduplicate a replay. The key is
    /// a safety declaration only; callers still provide the corresponding
    /// request header or body field themselves.
    IdempotencyKey(String),
}

impl fmt::Debug for RequestSafety {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Never => formatter.write_str("Never"),
            Self::Automatic => formatter.write_str("Automatic"),
            Self::IdempotentByContract => formatter.write_str("IdempotentByContract"),
            Self::IdempotencyKey(_) => formatter.write_str("IdempotencyKey(<redacted>)"),
        }
    }
}

impl Default for RequestSafety {
    fn default() -> Self {
        Self::Automatic
    }
}

/// Broad reason used to select HTTP retries.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RetryReason {
    /// The peer returned a status that may be transient.
    HttpStatus(StatusCode),
    /// A transport operation failed before a complete response was delivered.
    Transport(TransportErrorKind),
    /// Reading a buffered response body failed.
    BodyRead,
    /// A caller-provided application classifier classified the response as transient.
    Application,
    /// The server supplied a `Retry-After` hint.
    RetryAfter,
}

/// Transport failure categories used by retry policy classification.
///
/// A category does not establish whether the peer performed the operation and
/// never overrides request safety or body replayability. The reqwest backend
/// uses `DeliveryUnknown` when transport execution fails without reliable
/// evidence of the request's send/receive phase; it does not infer delivery from
/// `ErrorStage`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TransportErrorKind {
    Dns,
    Connect,
    Tls,
    /// A timeout positively established to occur before sending the request.
    /// The current reqwest backend does not emit this category for execution
    /// failures. A timeout observed during execution is not sufficient evidence.
    TimeoutBeforeSend,
    /// A failure positively attributed to writing the request.
    /// The current reqwest backend does not reliably expose this distinction;
    /// it does not emit this category for execution failures. Ambiguous failures
    /// use `DeliveryUnknown` instead.
    Write,
    ReadReset,
    BodyRead,
    Protocol,
    /// Execution failed and the request may have reached the peer partially or
    /// completely. This also covers transport timeouts with unknown delivery.
    /// The error retains its broad `Io` or `TimedOut` kind; this category does not
    /// imply `ErrorKind::DeliveryUnknown` or prove a server-side outcome.
    DeliveryUnknown,
}

/// Configured reason set used by a policy.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RetryOn {
    statuses: Vec<StatusCode>,
    transport: Vec<TransportErrorKind>,
    body_read: bool,
    timeout_phases: Vec<crate::api::http::TimeoutPhase>,
}

impl RetryOn {
    /// The conservative transient HTTP and transport defaults.
    ///
    /// Does not select `DeliveryUnknown` or `Write`. With built-in classification,
    /// ambiguous execution failures therefore require an explicit transport
    /// selection, in addition to request safety and body replayability, to retry.
    pub fn standard() -> Self {
        Self {
            statuses: default_retryable_statuses(),
            transport: default_retryable_transport_reasons(),
            body_read: true,
            timeout_phases: Vec::new(),
        }
    }

    /// An empty set that retries no classified reason.
    pub fn none() -> Self {
        Self {
            statuses: Vec::new(),
            transport: Vec::new(),
            body_read: false,
            timeout_phases: Vec::new(),
        }
    }

    /// Replace the status set while retaining other reason categories.
    pub fn with_statuses(mut self, statuses: impl IntoIterator<Item = StatusCode>) -> Self {
        self.statuses = statuses.into_iter().collect();
        self
    }

    /// Replace the transport reason set while retaining other reason categories.
    ///
    /// Selecting a category does not guarantee the backend can identify it;
    /// see [`TransportErrorKind`] for the current execution evidence limits.
    pub fn with_transport(mut self, reasons: impl IntoIterator<Item = TransportErrorKind>) -> Self {
        self.transport = reasons.into_iter().collect();
        self
    }

    /// Enable or disable retries classified as body-read failures.
    pub fn with_body_read(mut self, enabled: bool) -> Self {
        self.body_read = enabled;
        self
    }

    /// Select retryable phase deadlines. Admission and Total remain terminal.
    pub fn with_timeout_phases(
        mut self,
        phases: impl IntoIterator<Item = crate::api::http::TimeoutPhase>,
    ) -> Self {
        self.timeout_phases = phases
            .into_iter()
            .filter(|phase| {
                !matches!(
                    phase,
                    crate::api::http::TimeoutPhase::Admission
                        | crate::api::http::TimeoutPhase::Total
                )
            })
            .collect();
        self
    }
    pub fn timeout_phases(&self) -> &[crate::api::http::TimeoutPhase] {
        &self.timeout_phases
    }
    /// Return whether this set accepts a classified reason.
    pub fn allows(&self, reason: RetryReason) -> bool {
        match reason {
            RetryReason::HttpStatus(status) => self.statuses.contains(&status),
            RetryReason::Transport(kind) => self.transport.contains(&kind),
            RetryReason::BodyRead => self.body_read,
            RetryReason::Application => false,
            RetryReason::RetryAfter => false,
        }
    }

    /// Return the configured HTTP statuses.
    pub fn statuses(&self) -> &[StatusCode] {
        &self.statuses
    }

    /// Return the configured transport categories.
    pub fn transport(&self) -> &[TransportErrorKind] {
        &self.transport
    }

    /// Report whether body-read failures are enabled.
    pub fn retries_body_read(&self) -> bool {
        self.body_read
    }
}

/// Read-only outcome of a buffered HTTP attempt. The classifier never owns the final callback.
pub struct HttpRetryContext<'a> {
    pub outcome: Result<&'a HttpResponse, &'a NetError>,
    pub attempt: u32,
    pub elapsed: Duration,
    pub remaining: Option<Duration>,
}

type Classifier = dyn for<'a> Fn(HttpRetryContext<'a>) -> RetryDecision + Send + Sync + 'static;

/// Validated HTTP rules composed with a protocol-independent retry schedule.
#[derive(Clone)]
pub struct HttpRetryPolicy {
    schedule: RetrySchedule,
    retry_on: RetryOn,
    retry_after: RetryAfterMode,
    safety: RequestSafety,
    observer: Option<RetryObserverHandle>,
    classifier: Option<Arc<Classifier>>,
}
impl fmt::Debug for HttpRetryPolicy {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("HttpRetryPolicy")
            .field("schedule", &self.schedule)
            .field("retry_on", &self.retry_on)
            .field("retry_after", &self.retry_after)
            .field("safety", &self.safety)
            .field("has_observer", &self.observer.is_some())
            .field("has_classifier", &self.classifier.is_some())
            .finish()
    }
}
impl Default for HttpRetryPolicy {
    fn default() -> Self {
        Self::no_retry()
    }
}
impl HttpRetryPolicy {
    pub fn no_retry() -> Self {
        Self {
            schedule: RetrySchedule::no_retry(),
            retry_on: RetryOn::none(),
            retry_after: RetryAfterMode::Ignore,
            safety: RequestSafety::Automatic,
            observer: None,
            classifier: None,
        }
    }
    pub fn builder() -> HttpRetryPolicyBuilder {
        HttpRetryPolicyBuilder {
            policy: Self::no_retry(),
        }
    }
    pub fn schedule(&self) -> &RetrySchedule {
        &self.schedule
    }
    pub fn retry_on(&self) -> &RetryOn {
        &self.retry_on
    }
    pub fn retry_after(&self) -> RetryAfterMode {
        self.retry_after
    }
    pub fn safety(&self) -> &RequestSafety {
        &self.safety
    }
    pub fn observer(&self) -> Option<RetryObserverHandle> {
        self.observer.clone()
    }
    pub(crate) fn classify(
        &self,
        context: HttpRetryContext<'_>,
    ) -> Result<RetryDecision, NetError> {
        match &self.classifier {
            None => Ok(RetryDecision::Default),
            Some(classifier) => {
                match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| classifier(context)))
                {
                    Ok(decision) => Ok(decision),
                    Err(payload) => {
                        if let Err(second) =
                            std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| drop(payload)))
                        {
                            std::mem::forget(second);
                        }
                        Err(NetError::from(ErrorKind::ProviderFailed)
                            .with_stage(ErrorStage::Provider))
                    }
                }
            }
        }
    }
    pub(crate) fn allows_method(&self, method: &Method, replayable_body: bool) -> bool {
        replayable_body
            && match self.safety {
                RequestSafety::Never => false,
                RequestSafety::Automatic => is_idempotent(method),
                RequestSafety::IdempotentByContract | RequestSafety::IdempotencyKey(_) => true,
            }
    }
    pub(crate) fn should_retry_status(&self, status: StatusCode) -> bool {
        self.retry_on.statuses.contains(&status)
    }
    pub(crate) fn should_retry_error(&self, error: &NetError) -> bool {
        if let Some(phase) = error.timeout_phase() {
            if matches!(
                phase,
                crate::api::http::TimeoutPhase::Admission | crate::api::http::TimeoutPhase::Total
            ) {
                return false;
            }
            return self.retry_on.timeout_phases.contains(&phase);
        }
        use crate::module::http::http_failure::{fallback_retry_reason, trusted_retry_reason};
        trusted_retry_reason(error)
            .or_else(|| fallback_retry_reason(error))
            .is_some_and(|reason| {
                self.retry_on.allows(reason)
                    || (reason == RetryReason::BodyRead
                        && self
                            .retry_on
                            .transport
                            .contains(&TransportErrorKind::BodyRead))
            })
    }
}

#[derive(Clone, Debug)]
pub struct HttpRetryPolicyBuilder {
    policy: HttpRetryPolicy,
}
impl Default for HttpRetryPolicyBuilder {
    fn default() -> Self {
        HttpRetryPolicy::builder()
    }
}
impl PartialEq for HttpRetryPolicy {
    fn eq(&self, other: &Self) -> bool {
        self.schedule == other.schedule
            && self.retry_on == other.retry_on
            && self.retry_after == other.retry_after
            && self.safety == other.safety
            && self.observer == other.observer
            && match (&self.classifier, &other.classifier) {
                (None, None) => true,
                (Some(a), Some(b)) => Arc::ptr_eq(a, b),
                _ => false,
            }
    }
}
impl HttpRetryPolicyBuilder {
    pub fn schedule(mut self, schedule: RetrySchedule) -> Self {
        self.policy.schedule = schedule;
        self
    }
    pub fn retry_on(mut self, retry_on: RetryOn) -> Self {
        self.policy.retry_on = retry_on;
        self
    }
    pub fn retry_after(mut self, mode: RetryAfterMode) -> Self {
        self.policy.retry_after = mode;
        self
    }
    pub fn safety(mut self, safety: RequestSafety) -> Self {
        self.policy.safety = safety;
        self
    }
    pub fn observer(mut self, observer: impl RetryObserver) -> Self {
        self.policy.observer = Some(RetryObserverHandle::new(observer));
        self
    }
    /// Classify buffered responses and attempt errors synchronously without side effects.
    /// Streaming response bodies are never buffered for this hook.
    pub fn classifier<F>(mut self, classifier: F) -> Self
    where
        F: for<'a> Fn(HttpRetryContext<'a>) -> RetryDecision + Send + Sync + 'static,
    {
        self.policy.classifier = Some(Arc::new(classifier));
        self
    }
    pub fn build(self) -> Result<HttpRetryPolicy, RetryConfigError> {
        if matches!(&self.policy.safety, RequestSafety::IdempotencyKey(key) if key.is_empty()) {
            return Err(RetryConfigError::new(
                "http.retry.safety",
                "idempotency key must not be empty",
            ));
        }
        Ok(self.policy)
    }
}

/// Per-operation HTTP settings. Retry selection has one authoritative source.
#[derive(Clone, Debug, PartialEq)]
pub struct HttpRequestOptions {
    pub replayable_body: bool,
    pub request_id: Option<HttpRequestId>,
    retry_setting: RetrySetting<HttpRetryPolicy>,
    timeouts: HttpTimeouts,
}
impl Default for HttpRequestOptions {
    fn default() -> Self {
        Self {
            replayable_body: true,
            request_id: None,
            retry_setting: RetrySetting::Inherit,
            timeouts: HttpTimeouts::new(),
        }
    }
}
impl HttpRequestOptions {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn with_timeouts(mut self, timeouts: HttpTimeouts) -> Result<Self, NetError> {
        timeouts.validate_request()?;
        self.timeouts = timeouts;
        Ok(self)
    }
    pub fn timeouts(&self) -> &HttpTimeouts {
        &self.timeouts
    }
    pub fn with_retry_setting(mut self, setting: RetrySetting<HttpRetryPolicy>) -> Self {
        self.retry_setting = setting;
        self
    }
    pub fn retry_setting(&self) -> RetrySetting<HttpRetryPolicy> {
        self.retry_setting.clone()
    }
    pub fn with_replayable_body(mut self, replayable: bool) -> Self {
        self.replayable_body = replayable;
        self
    }
    pub fn with_request_id(mut self, id: HttpRequestId) -> Self {
        self.request_id = Some(id);
        self
    }
}

fn is_idempotent(method: &Method) -> bool {
    matches!(
        *method,
        Method::GET | Method::HEAD | Method::OPTIONS | Method::PUT | Method::DELETE
    )
}

fn default_retryable_statuses() -> Vec<StatusCode> {
    vec![
        StatusCode::REQUEST_TIMEOUT,
        StatusCode::TOO_EARLY,
        StatusCode::TOO_MANY_REQUESTS,
        StatusCode::INTERNAL_SERVER_ERROR,
        StatusCode::BAD_GATEWAY,
        StatusCode::SERVICE_UNAVAILABLE,
        StatusCode::GATEWAY_TIMEOUT,
    ]
}

fn default_retryable_transport_reasons() -> Vec<TransportErrorKind> {
    vec![
        TransportErrorKind::Dns,
        TransportErrorKind::Connect,
        TransportErrorKind::TimeoutBeforeSend,
        TransportErrorKind::ReadReset,
        TransportErrorKind::BodyRead,
    ]
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::http::TimeoutPhase;
    type TestResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
    fn check(value: bool, message: &str) -> TestResult {
        if value {
            Ok(())
        } else {
            Err(std::io::Error::other(message).into())
        }
    }
    #[test]
    fn timeout_phase_selection_cannot_be_bypassed_by_transport_defaults() -> TestResult {
        let policy = HttpRetryPolicy::builder()
            .retry_on(RetryOn::standard().with_timeout_phases([
                TimeoutPhase::Headers,
                TimeoutPhase::IdleRead,
                TimeoutPhase::Admission,
                TimeoutPhase::Total,
            ]))
            .build()?;
        for (phase, allowed) in [
            (TimeoutPhase::Headers, true),
            (TimeoutPhase::IdleRead, true),
            (TimeoutPhase::Attempt, false),
            (TimeoutPhase::Connect, false),
            (TimeoutPhase::Admission, false),
            (TimeoutPhase::Total, false),
        ] {
            let error = NetError::from(ErrorKind::TimedOut).with_timeout_phase(phase);
            check(
                policy.should_retry_error(&error) == allowed,
                "phase selection was bypassed",
            )?;
        }
        Ok(())
    }
    #[test]
    fn retry_after_is_a_lower_bound_and_does_not_truncate_the_schedule() -> TestResult {
        for (server, backoff, expected) in [(2, 5, 5), (8, 3, 8)] {
            check(
                RetryAfterMode::Respect.select(
                    Some(Duration::from_secs(server)),
                    Duration::from_secs(backoff),
                ) == Duration::from_secs(expected),
                "server lower bound was not respected",
            )?;
        }
        check(
            RetryAfterMode::Ignore.select(Some(Duration::from_secs(8)), Duration::from_secs(3))
                == Duration::from_secs(3),
            "ignore used the server hint",
        )
    }
    #[test]
    fn classifier_reads_complete_response_and_value_clones_share_identity() -> TestResult {
        let policy = HttpRetryPolicy::builder()
            .classifier(|context| match context.outcome {
                Ok(response) if response.body.as_ref() == b"retry-me" => RetryDecision::Retry,
                _ => RetryDecision::Stop,
            })
            .build()?;
        let response = HttpResponse::new(
            StatusCode::OK,
            http::HeaderMap::new(),
            bytes::Bytes::from_static(b"retry-me"),
            1,
            HttpRequestId(9),
        );
        let context = HttpRetryContext {
            outcome: Ok(&response),
            attempt: 1,
            elapsed: Duration::ZERO,
            remaining: None,
        };
        check(
            policy.classify(context)? == RetryDecision::Retry,
            "classifier could not inspect response bytes",
        )?;
        check(
            policy == policy.clone(),
            "policy clone lost classifier identity",
        )?;
        check(
            HttpRetryPolicy::builder()
                .safety(RequestSafety::IdempotencyKey(String::new()))
                .build()
                .is_err(),
            "empty idempotency key accepted",
        )
    }
}