jevcrates.git / jev-http / src / runtime.rs
1use std::future::Future;
2use std::time::{Duration, Instant, SystemTime};
3
4use jev_client::Runtime;

jev-client's [Runtime] on tokio's clock and timers.

7#[derive(Clone, Copy, Debug, Default)]
8pub struct TokioRuntime;
10impl Runtime for TokioRuntime {
11    fn now(&self) -> Instant {
12        Instant::now()
13    }
14
15    fn wall_clock(&self) -> SystemTime {
16        SystemTime::now()
17    }
18
19    fn sleep(&self, duration: Duration) -> impl Future<Output = ()> {
20        tokio::time::sleep(duration)
21    }
22
23    fn timeout<F: Future>(&self, duration: Duration, future: F) -> impl Future<Output = Option<F::Output>> {
24        async move { tokio::time::timeout(duration, future).await.ok() }
25    }

The jitter fraction only spreads retries. Nanoseconds of the clock are random enough for that and need no dependency (as jevsnes' own retry loop drew it).

30    fn jitter(&self) -> f64 {
31        let nanos = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).map_or(0, |d| d.subsec_nanos());
32        f64::from(nanos) / 1e9
33    }
34}