forked from algesten/ureq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimings.rs
224 lines (188 loc) · 6.49 KB
/
timings.rs
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
use std::fmt;
use std::sync::Arc;
use crate::config::Timeouts;
use crate::transport::time::{Duration, Instant};
/// The various timeouts.
///
/// Each enum corresponds to a value in
/// [`ConfigBuilder::timeout_xxx`][crate::config::ConfigBuilder::timeout_global].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Timeout {
/// Timeout for entire operation.
Global,
/// Timeout for the current call (when redirected).
PerCall,
/// Timeout in the resolver.
Resolve,
/// Timeout while opening the connection.
Connect,
/// Timeout while sending the request headers.
SendRequest,
/// Internal value never seen outside ureq (since awaiting 100 is expected
/// to timeout).
#[doc(hidden)]
Await100,
/// Timeout when sending then request body.
SendBody,
/// Timeout while receiving the response headers.
RecvResponse,
/// Timeout while receiving the response body.
RecvBody,
}
impl Timeout {
/// Give the immediate preceeding Timeout
fn preceeding(&self) -> impl Iterator<Item = Timeout> {
let prev: &[Timeout] = match self {
Timeout::Resolve => &[Timeout::PerCall],
Timeout::Connect => &[Timeout::Resolve],
Timeout::SendRequest => &[Timeout::Connect],
Timeout::Await100 => &[Timeout::SendRequest],
Timeout::SendBody => &[Timeout::SendRequest, Timeout::Await100],
Timeout::RecvResponse => &[Timeout::SendRequest, Timeout::SendBody],
Timeout::RecvBody => &[Timeout::RecvResponse],
_ => &[],
};
prev.iter().copied()
}
/// All timeouts to check
fn timeouts_to_check(&self) -> impl Iterator<Item = Timeout> {
// Always check Global and PerCall
self.preceeding().chain([Timeout::Global, Timeout::PerCall])
}
/// Get the corresponding configured timeout
fn configured_timeout(&self, timeouts: &Timeouts) -> Option<Duration> {
match self {
Timeout::Global => timeouts.global,
Timeout::PerCall => timeouts.per_call,
Timeout::Resolve => timeouts.resolve,
Timeout::Connect => timeouts.connect,
Timeout::SendRequest => timeouts.send_request,
Timeout::Await100 => timeouts.await_100,
Timeout::SendBody => timeouts.send_body,
Timeout::RecvResponse => timeouts.recv_response,
Timeout::RecvBody => timeouts.recv_body,
}
.map(Into::into)
}
}
#[derive(Default, Debug)]
pub(crate) struct CallTimings {
timeouts: Box<Timeouts>,
current_time: CurrentTime,
times: Vec<(Timeout, Instant)>,
}
impl CallTimings {
pub(crate) fn new(timeouts: Timeouts, current_time: CurrentTime) -> Self {
let mut times = Vec::with_capacity(8);
let now = current_time.now();
times.push((Timeout::Global, now));
times.push((Timeout::PerCall, now));
CallTimings {
timeouts: Box::new(timeouts),
current_time,
times,
}
}
pub(crate) fn new_call(mut self) -> CallTimings {
self.times.truncate(1); // Global is in position 0.
self.times.push((Timeout::PerCall, self.current_time.now()));
CallTimings {
timeouts: self.timeouts,
current_time: self.current_time,
times: self.times,
}
}
pub(crate) fn now(&self) -> Instant {
self.current_time.now()
}
pub(crate) fn record_time(&mut self, timeout: Timeout) {
// Each time should only be recorded once
assert!(
self.time_of(timeout).is_none(),
"{:?} recorded more than once",
timeout
);
// There need to be at least one preceeding time recorded
// since it follows a graph/call tree.
let any_preceeding = timeout
.preceeding()
.filter_map(|to_check| self.time_of(to_check))
.any(|_| true);
assert!(any_preceeding, "{:?} has no preceeding", timeout);
// Record the time
self.times.push((timeout, self.current_time.now()));
}
fn time_of(&self, timeout: Timeout) -> Option<Instant> {
self.times.iter().find(|x| x.0 == timeout).map(|x| x.1)
}
pub(crate) fn next_timeout(&self, timeout: Timeout) -> NextTimeout {
let (reason, at) = timeout
.timeouts_to_check()
.filter_map(|to_check| {
let time = self.time_of(to_check)?;
let timeout = to_check.configured_timeout(&self.timeouts)?;
Some((to_check, time + timeout))
})
.min_by(|a, b| a.1.cmp(&b.1))
.unwrap_or((Timeout::Global, Instant::NotHappening));
let now = self.now();
let after = at.duration_since(now);
NextTimeout { after, reason }
}
}
#[derive(Clone)]
pub(crate) struct CurrentTime(Arc<dyn Fn() -> Instant + Send + Sync + 'static>);
impl CurrentTime {
pub(crate) fn now(&self) -> Instant {
self.0()
}
}
/// A pair of [`Duration`] and [`Timeout`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NextTimeout {
/// Duration until next timeout.
pub after: Duration,
/// The name of the next timeout.s
pub reason: Timeout,
}
impl NextTimeout {
/// Returns the duration of the timeout if the timeout must happen, but avoid instant timeouts
///
/// If the timeout must happen but is zero, returns 1 second
pub fn not_zero(&self) -> Option<Duration> {
if self.after.is_not_happening() {
None
} else if self.after.is_zero() {
Some(Duration::from_secs(1))
} else {
Some(self.after)
}
}
}
impl fmt::Debug for CurrentTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("CurrentTime").finish()
}
}
impl Default for CurrentTime {
fn default() -> Self {
Self(Arc::new(Instant::now))
}
}
impl fmt::Display for Timeout {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let r = match self {
Timeout::Global => "global",
Timeout::PerCall => "per call",
Timeout::Resolve => "resolve",
Timeout::Connect => "connect",
Timeout::SendRequest => "send request",
Timeout::SendBody => "send body",
Timeout::Await100 => "await 100",
Timeout::RecvResponse => "receive response",
Timeout::RecvBody => "receive body",
};
write!(f, "{}", r)
}
}