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
//! The four bounds every awaited network I/O of a protocol adapter carries:
//! connect, handshake, idle read and write.
//!
//! One type for the RTSP, RTMP and SRT adapters (and any later one) so a host
//! maps its own configuration onto timeouts once, and no adapter owns a
//! private copy of the same four fields.
use core::time::Duration;
/// Bounds on every awaited I/O of a protocol adapter: no wait is unbounded.
///
/// [`Default`] is the bound a TCP signalling protocol wants (10 s connect and
/// handshake, 30 s idle read, 10 s write); a protocol with other norms starts
/// from [`Timeouts::new`] and its own values.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Timeouts {
/// Resolving and connecting (or binding) the transport.
pub connect: Duration,
/// Completing the protocol handshake (TLS, a handshake exchange, or the
/// first complete request on a server connection).
pub handshake: Duration,
/// Longest wait for the next complete frame from a connected peer. One
/// deadline covers the whole frame, so a peer dripping bytes still times
/// out.
pub read_idle: Duration,
/// Longest wait for the transport to accept a write.
pub write: Duration,
}
impl Timeouts {
/// Timeouts with every bound given.
#[must_use]
pub const fn new(
connect: Duration,
handshake: Duration,
read_idle: Duration,
write: Duration,
) -> Self {
Timeouts {
connect,
handshake,
read_idle,
write,
}
}
/// Sets [`connect`](Self::connect).
#[must_use]
pub const fn with_connect(mut self, connect: Duration) -> Self {
self.connect = connect;
self
}
/// Sets [`handshake`](Self::handshake).
#[must_use]
pub const fn with_handshake(mut self, handshake: Duration) -> Self {
self.handshake = handshake;
self
}
/// Sets [`read_idle`](Self::read_idle).
#[must_use]
pub const fn with_read_idle(mut self, read_idle: Duration) -> Self {
self.read_idle = read_idle;
self
}
/// Sets [`write`](Self::write).
#[must_use]
pub const fn with_write(mut self, write: Duration) -> Self {
self.write = write;
self
}
}
impl Default for Timeouts {
fn default() -> Self {
Timeouts::new(
Duration::from_secs(10),
Duration::from_secs(10),
Duration::from_secs(30),
Duration::from_secs(10),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn each_with_sets_only_its_own_field() {
let base = Timeouts::default();
let one = Duration::from_secs(1);
assert_eq!(
base.with_connect(one),
Timeouts {
connect: one,
..base
}
);
assert_eq!(
base.with_handshake(one),
Timeouts {
handshake: one,
..base
}
);
assert_eq!(
base.with_read_idle(one),
Timeouts {
read_idle: one,
..base
}
);
assert_eq!(base.with_write(one), Timeouts { write: one, ..base });
}
}