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
//! SPEC ยง2's `operation_deadline`: one bound over the whole of a call โ credentials,
//! every attempt, every wait, the body read โ applied at each await rather than around
//! the call, so a request the deadline cuts short still reports its end to the hooks.
use std::time::Duration;
use tokio::time::Instant;
use crate::error::Error;
#[derive(Debug, Clone, Copy)]
pub(crate) struct Deadline {
configured: Option<Duration>,
at: Option<Instant>,
}
impl Deadline {
/// The configured bound, counted from now; `None` bounds nothing.
pub(crate) fn starting_now(configured: Option<Duration>) -> Deadline {
// A bound the clock cannot represent is no bound.
Deadline {
configured,
at: configured.and_then(|after| Instant::now().checked_add(after)),
}
}
/// Runs `work`, unless the deadline passes first.
pub(crate) async fn bound<T>(
&self,
work: impl Future<Output = Result<T, Error>>,
) -> Result<T, Error> {
match self.at {
None => work.await,
Some(at) => match tokio::time::timeout_at(at, work).await {
Ok(outcome) => outcome,
Err(_) => Err(self.exceeded()),
},
}
}
/// Waits `delay` before a resend, unless the deadline would pass first: a resend that
/// could not go out in time is not begun, and the deadline is exceeded now rather than
/// after the wait โ and a wait that was admitted still ends at the deadline.
pub(crate) async fn wait(&self, delay: Duration) -> Result<(), Error> {
match self.at {
Some(at) if at.saturating_duration_since(Instant::now()) < delay => {
Err(self.exceeded())
}
_ => {
self.bound(async {
tokio::time::sleep(delay).await;
Ok(())
})
.await
}
}
}
fn exceeded(&self) -> Error {
Error::deadline_exceeded(self.configured.unwrap_or_default())
}
}