summaryrefslogtreecommitdiff
path: root/crates/tor-guardmgr
diff options
context:
space:
mode:
Diffstat (limited to 'crates/tor-guardmgr')
-rw-r--r--crates/tor-guardmgr/Cargo.toml1
-rw-r--r--crates/tor-guardmgr/src/bridge/descs.rs6
-rw-r--r--crates/tor-guardmgr/src/dirstatus.rs5
-rw-r--r--crates/tor-guardmgr/src/err.rs4
-rw-r--r--crates/tor-guardmgr/src/fallback.rs7
-rw-r--r--crates/tor-guardmgr/src/guard.rs39
-rw-r--r--crates/tor-guardmgr/src/lib.rs2
-rw-r--r--crates/tor-guardmgr/src/pending.rs2
-rw-r--r--crates/tor-guardmgr/src/sample.rs51
-rw-r--r--crates/tor-guardmgr/src/skew.rs11
-rw-r--r--crates/tor-guardmgr/src/util.rs3
11 files changed, 69 insertions, 62 deletions
diff --git a/crates/tor-guardmgr/Cargo.toml b/crates/tor-guardmgr/Cargo.toml
index 60468a9ec..097eefaf6 100644
--- a/crates/tor-guardmgr/Cargo.toml
+++ b/crates/tor-guardmgr/Cargo.toml
@@ -96,6 +96,7 @@ tor-rtcompat = { path = "../tor-rtcompat", version = "0.40.0" }
tor-rtmock = { path = "../tor-rtmock", version = "0.40.0", optional = true }
tor-units = { path = "../tor-units", version = "0.40.0" }
tracing = "0.1.36"
+web-time-compat = { path = "../web-time-compat", version = "0.1.0" }
[dev-dependencies]
float_eq = "1.0.0"
diff --git a/crates/tor-guardmgr/src/bridge/descs.rs b/crates/tor-guardmgr/src/bridge/descs.rs
index 7eee3cf25..4dd1a0c14 100644
--- a/crates/tor-guardmgr/src/bridge/descs.rs
+++ b/crates/tor-guardmgr/src/bridge/descs.rs
@@ -5,7 +5,6 @@
use std::collections::HashMap;
use std::sync::Arc;
-use std::time::SystemTime;
use crate::{
bridge::BridgeConfig,
@@ -20,6 +19,7 @@ use tor_linkspec::{ChanTarget, HasChanMethod, HasRelayIds, OwnedChanTarget};
use tor_llcrypto::pk::{ed25519::Ed25519Identity, rsa::RsaIdentity};
use tor_netdir::RelayWeight;
use tor_netdoc::doc::routerdesc::RouterDesc;
+use web_time_compat::{SystemTime, SystemTimeExt};
use super::BridgeRelay;
@@ -239,13 +239,13 @@ impl Universe for BridgeSet {
}
}
- fn timestamp(&self) -> std::time::SystemTime {
+ fn timestamp(&self) -> SystemTime {
// We just use the current time as the timestamp of this BridgeSet.
// This makes the guard code treat a BridgeSet as _continuously updated_:
// anything listed in the guard set is treated as listed right up to this
// moment, and anything unlisted is treated as unlisted right up to this
// moment.
- SystemTime::now()
+ SystemTime::get()
}
/// Note that for a BridgeSet, we always treat the current weight as 0 and
diff --git a/crates/tor-guardmgr/src/dirstatus.rs b/crates/tor-guardmgr/src/dirstatus.rs
index 7b3736e40..d3329da93 100644
--- a/crates/tor-guardmgr/src/dirstatus.rs
+++ b/crates/tor-guardmgr/src/dirstatus.rs
@@ -1,7 +1,7 @@
//! Types and code to track the readiness status of a directory cache.
-use std::time::{Duration, Instant};
use tor_basic_utils::retry::RetryDelay;
+use web_time_compat::{Duration, Instant};
/// Status information about whether a
/// [`FallbackDir`](tor_dircommon::fallback::FallbackDir) or
@@ -78,10 +78,11 @@ mod test {
#![allow(clippy::needless_pass_by_value)]
//! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
use super::*;
+ use web_time_compat::InstantExt;
#[test]
fn status_basics() {
- let now = Instant::now();
+ let now = Instant::get();
/// floor to use for testing.
const FLOOR: Duration = Duration::from_secs(99);
diff --git a/crates/tor-guardmgr/src/err.rs b/crates/tor-guardmgr/src/err.rs
index a93183e5e..f59544c45 100644
--- a/crates/tor-guardmgr/src/err.rs
+++ b/crates/tor-guardmgr/src/err.rs
@@ -2,9 +2,9 @@
use futures::task::SpawnError;
use std::sync::Arc;
-use std::time::Instant;
use tor_basic_utils::iter::FilterCount;
use tor_error::{Bug, ErrorKind, HasKind};
+use web_time_compat::{Instant, InstantExt};
/// A error caused by a failure to pick a guard.
#[derive(Clone, Debug, thiserror::Error)]
@@ -19,7 +19,7 @@ pub enum PickGuardError {
suitable.display_frac_rejected(),
filtered.display_frac_rejected(),
if let Some(retry_at) = retry_at {
- format!(" Retrying in {}.", humantime::format_duration(*retry_at - Instant::now()))
+ format!(" Retrying in {}.", humantime::format_duration(*retry_at - Instant::get()))
} else {
"".to_string()
},
diff --git a/crates/tor-guardmgr/src/fallback.rs b/crates/tor-guardmgr/src/fallback.rs
index 323870d29..8047254c8 100644
--- a/crates/tor-guardmgr/src/fallback.rs
+++ b/crates/tor-guardmgr/src/fallback.rs
@@ -6,9 +6,9 @@
use crate::{dirstatus::DirStatus, skew::SkewObservation};
use rand::seq::IteratorRandom;
-use std::time::{Duration, Instant};
use tor_dircommon::fallback::{FallbackDir, FallbackList};
use tor_linkspec::HasRelayIds;
+use web_time_compat::{Duration, Instant};
use crate::{PickGuardError, ids::FallbackId};
use tor_basic_utils::iter::{FilterCount, IteratorExt as _};
@@ -201,6 +201,7 @@ mod test {
use super::*;
use rand::Rng;
use tor_basic_utils::test_rng::testing_rng;
+ use web_time_compat::InstantExt;
/// Construct a `FallbackDir` with random identity keys and addresses.
///
@@ -297,7 +298,7 @@ mod test {
let filter = crate::GuardFilter::unfiltered();
let mut counts = [0_usize; 4];
- let now = Instant::now();
+ let now = Instant::get();
dbg!("A");
fn lookup_idx(set: &FallbackState, id: &impl HasRelayIds) -> Option<usize> {
set.fallbacks
@@ -365,7 +366,7 @@ mod test {
.map(|ent| FallbackId::from_relay_ids(&ent.fallback))
.collect();
- let now = Instant::now();
+ let now = Instant::get();
// There's no "next retry time" when everybody's up.
assert!(set.next_retry().is_none());
diff --git a/crates/tor-guardmgr/src/guard.rs b/crates/tor-guardmgr/src/guard.rs
index 6e876340b..1fa6b2d52 100644
--- a/crates/tor-guardmgr/src/guard.rs
+++ b/crates/tor-guardmgr/src/guard.rs
@@ -6,8 +6,8 @@ use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::SocketAddr;
-use std::time::{Duration, Instant, SystemTime};
use tracing::{info, trace, warn};
+use web_time_compat::{Duration, Instant, InstantExt, SystemTime};
use crate::dirstatus::DirStatus;
use crate::sample::Candidate;
@@ -448,7 +448,7 @@ impl Guard {
Some(retry_at) => warn!(
"Could not connect to guard {}. Retrying in {}.",
self,
- humantime::format_duration(retry_at - Instant::now()),
+ humantime::format_duration(retry_at - Instant::get()),
),
None => warn!(
"Could not connect to guard {}. Next retry time unknown.",
@@ -951,6 +951,7 @@ mod test {
use crate::ids::FirstHopId;
use tor_linkspec::{HasRelayIds, RelayId};
use tor_llcrypto::pk::ed25519::Ed25519Identity;
+ use web_time_compat::SystemTimeExt;
#[test]
fn crate_id() {
@@ -965,7 +966,7 @@ mod test {
fn basic_guard() -> Guard {
let id = basic_id();
let ports = vec!["127.0.0.7:7777".parse().unwrap()];
- let added = SystemTime::now();
+ let added = SystemTime::get();
Guard::new(id, ports, None, added)
}
@@ -1055,9 +1056,9 @@ mod test {
#[test]
fn record_attempt() {
- let t1 = Instant::now() - Duration::from_secs(10);
- let t2 = Instant::now() - Duration::from_secs(5);
- let t3 = Instant::now();
+ let t1 = Instant::get() - Duration::from_secs(10);
+ let t2 = Instant::get() - Duration::from_secs(5);
+ let t3 = Instant::get();
let mut g = basic_guard();
@@ -1072,8 +1073,8 @@ mod test {
#[test]
fn record_failure() {
- let t1 = Instant::now() - Duration::from_secs(10);
- let t2 = Instant::now();
+ let t1 = Instant::get() - Duration::from_secs(10);
+ let t2 = Instant::get();
let mut g = basic_guard();
g.record_failure(t1, true);
@@ -1090,11 +1091,11 @@ mod test {
#[test]
fn record_success() {
- let t1 = Instant::now() - Duration::from_secs(10);
+ let t1 = Instant::get() - Duration::from_secs(10);
// has to be in the future, since the guard's "added_at" time is based on now.
- let now = SystemTime::now();
+ let now = SystemTime::get();
let t2 = now + Duration::from_secs(300 * 86400);
- let t3 = Instant::now() + Duration::from_secs(310 * 86400);
+ let t3 = Instant::get() + Duration::from_secs(310 * 86400);
let t4 = now + Duration::from_secs(320 * 86400);
let mut g = basic_guard();
@@ -1121,7 +1122,7 @@ mod test {
#[test]
fn retry() {
- let t1 = Instant::now();
+ let t1 = Instant::get();
let mut g = basic_guard();
g.record_failure(t1, true);
@@ -1148,7 +1149,7 @@ mod test {
fn expiration() {
const DAY: Duration = Duration::from_secs(24 * 60 * 60);
let params = GuardParams::default();
- let now = SystemTime::now();
+ let now = SystemTime::get();
let g = basic_guard();
assert!(!g.is_expired(&params, now));
@@ -1176,7 +1177,7 @@ mod test {
use tor_netdir::testnet;
let netdir = testnet::construct_netdir().unwrap_if_sufficient().unwrap();
let params = GuardParams::default();
- let now = SystemTime::now();
+ let now = SystemTime::get();
// Construct a guard from a relay from the netdir.
let relay22 = netdir.by_id(&Ed25519Identity::from([22; 32])).unwrap();
@@ -1226,7 +1227,7 @@ mod test {
.unwrap();
//let params = GuardParams::default();
- let now = SystemTime::now();
+ let now = SystemTime::get();
// Try a guard that isn't in the netdir at all.
let mut guard255 = Guard::new(
@@ -1283,7 +1284,7 @@ mod test {
#[test]
fn pending() {
let mut g = basic_guard();
- let t1 = Instant::now();
+ let t1 = Instant::get();
let t2 = t1 + Duration::from_secs(100);
let t3 = t1 + Duration::from_secs(200);
@@ -1320,7 +1321,7 @@ mod test {
let mut g = basic_guard();
let params = GuardParams::default();
- let now = SystemTime::now();
+ let now = SystemTime::get();
let _ignore = g.record_success(now, &params);
for _ in 0..13 {
@@ -1374,8 +1375,8 @@ mod test {
use crate::GuardUsageBuilder;
let mut g = basic_guard();
- let inst = Instant::now();
- let st = SystemTime::now();
+ let inst = Instant::get();
+ let st = SystemTime::get();
let sec = Duration::from_secs(1);
let params = GuardParams::default();
let dir_usage = GuardUsageBuilder::new()
diff --git a/crates/tor-guardmgr/src/lib.rs b/crates/tor-guardmgr/src/lib.rs
index 6f54146d4..9e0534eae 100644
--- a/crates/tor-guardmgr/src/lib.rs
+++ b/crates/tor-guardmgr/src/lib.rs
@@ -62,7 +62,6 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex, Weak};
-use std::time::{Duration, Instant, SystemTime};
#[cfg(feature = "bridge-client")]
use tor_error::internal;
use tor_linkspec::{OwnedChanTarget, OwnedCircTarget, RelayId, RelayIdSet};
@@ -71,6 +70,7 @@ use tor_proto::ClockSkew;
use tor_rtcompat::SpawnExt;
use tor_units::BoundedInt32;
use tracing::{debug, info, instrument, trace, warn};
+use web_time_compat::{Duration, Instant, SystemTime};
use tor_config::derive::prelude::*;
use tor_config::{ExplicitOrAuto, impl_standard_builder};
diff --git a/crates/tor-guardmgr/src/pending.rs b/crates/tor-guardmgr/src/pending.rs
index 051291e85..f9bef8536 100644
--- a/crates/tor-guardmgr/src/pending.rs
+++ b/crates/tor-guardmgr/src/pending.rs
@@ -17,8 +17,8 @@ use std::fmt::Debug;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::task::{Context, Poll};
-use std::time::Instant;
use tor_proto::ClockSkew;
+use web_time_compat::Instant;
use tor_basic_utils::skip_fmt;
diff --git a/crates/tor-guardmgr/src/sample.rs b/crates/tor-guardmgr/src/sample.rs
index b0c2eae80..524f45556 100644
--- a/crates/tor-guardmgr/src/sample.rs
+++ b/crates/tor-guardmgr/src/sample.rs
@@ -18,8 +18,8 @@ use rand::seq::IndexedRandom;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
-use std::time::{Instant, SystemTime};
use tracing::{debug, info};
+use web_time_compat::{Instant, SystemTime};
#[allow(unused_imports)]
pub(crate) use candidate::{Candidate, CandidateStatus, Universe, UniverseRef, WeightThreshold};
@@ -1033,6 +1033,7 @@ mod test {
use tor_netdir::NetDir;
use tor_netdoc::doc::netstatus::RelayWeight;
use tor_netdoc::types::relay_flags::RelayFlag;
+ use web_time_compat::{InstantExt, SystemTimeExt};
use super::*;
use crate::FirstHopId;
@@ -1093,7 +1094,7 @@ mod test {
let mut samples: Vec<HashSet<GuardId>> = Vec::new();
for _ in 0..3 {
let mut guards = GuardSet::default();
- guards.extend_sample_as_needed(SystemTime::now(), &params, &netdir);
+ guards.extend_sample_as_needed(SystemTime::get(), &params, &netdir);
assert_eq!(guards.guards.len(), params.min_filtered_sample_size);
assert_eq!(guards.confirmed.len(), 0);
assert_eq!(guards.primary.len(), 0);
@@ -1107,12 +1108,12 @@ mod test {
assert!(relay.low_level_details().is_dir_cache());
assert!(guards.guards.by_all_ids(&relay).is_some());
{
- assert!(!guard.is_expired(&params, SystemTime::now()));
+ assert!(!guard.is_expired(&params, SystemTime::get()));
}
}
// Make sure that the sample doesn't expand any further.
- guards.extend_sample_as_needed(SystemTime::now(), &params, &netdir);
+ guards.extend_sample_as_needed(SystemTime::get(), &params, &netdir);
assert_eq!(guards.guards.len(), params.min_filtered_sample_size);
guards.assert_consistency();
@@ -1132,7 +1133,7 @@ mod test {
..GuardParams::default()
};
- let t1 = SystemTime::now();
+ let t1 = SystemTime::get();
let t2 = t1 + Duration::from_secs(20);
let mut guards = GuardSet::default();
@@ -1176,7 +1177,7 @@ mod test {
n_primary: 4,
..GuardParams::default()
};
- let t1 = SystemTime::now();
+ let t1 = SystemTime::get();
let t2 = t1 + Duration::from_secs(20);
let t3 = t2 + Duration::from_secs(30);
@@ -1222,7 +1223,7 @@ mod test {
fn expiration() {
let netdir = netdir();
let params = GuardParams::default();
- let t1 = SystemTime::now();
+ let t1 = SystemTime::get();
let mut guards = GuardSet::default();
guards.extend_sample_as_needed(t1, &params, &netdir);
@@ -1256,8 +1257,8 @@ mod test {
n_primary: 2,
..GuardParams::default()
};
- let st1 = SystemTime::now();
- let i1 = Instant::now();
+ let st1 = SystemTime::get();
+ let i1 = Instant::get();
let sec = Duration::from_secs(1);
let mut guards = GuardSet::default();
@@ -1383,8 +1384,8 @@ mod test {
max_sample_bw_fraction: 1.0,
..GuardParams::default()
};
- let mut st = SystemTime::now();
- let mut inst = Instant::now();
+ let mut st = SystemTime::get();
+ let mut inst = Instant::get();
let sec = Duration::from_secs(1);
let usage = crate::GuardUsageBuilder::default().build().unwrap();
@@ -1425,7 +1426,7 @@ mod test {
let mut guards = GuardSet::default();
- guards.extend_sample_as_needed(SystemTime::now(), &params, &netdir);
+ guards.extend_sample_as_needed(SystemTime::get(), &params, &netdir);
guards.select_primary_guards(&params);
assert_eq!(guards.primary.len(), 2);
@@ -1433,25 +1434,25 @@ mod test {
// Let one primary guard fail.
let (kind, p_id1) = guards
- .pick_guard_id(&usage, &params, Instant::now())
+ .pick_guard_id(&usage, &params, Instant::get())
.unwrap();
assert_eq!(kind, ListKind::Primary);
- guards.record_failure(&p_id1, None, Instant::now());
+ guards.record_failure(&p_id1, None, Instant::get());
assert!(!guards.all_primary_guards_are_unreachable());
// Now let the other one fail.
let (kind, p_id2) = guards
- .pick_guard_id(&usage, &params, Instant::now())
+ .pick_guard_id(&usage, &params, Instant::get())
.unwrap();
assert_eq!(kind, ListKind::Primary);
- guards.record_failure(&p_id2, None, Instant::now());
+ guards.record_failure(&p_id2, None, Instant::get());
assert!(guards.all_primary_guards_are_unreachable());
// Now mark the guards retriable.
guards.mark_primary_guards_retriable();
assert!(!guards.all_primary_guards_are_unreachable());
let (kind, p_id3) = guards
- .pick_guard_id(&usage, &params, Instant::now())
+ .pick_guard_id(&usage, &params, Instant::get())
.unwrap();
assert_eq!(kind, ListKind::Primary);
assert_eq!(p_id3, p_id1);
@@ -1468,14 +1469,14 @@ mod test {
};
let usage = crate::GuardUsageBuilder::default().build().unwrap();
let mut guards = GuardSet::default();
- guards.extend_sample_as_needed(SystemTime::now(), &params, &netdir);
+ guards.extend_sample_as_needed(SystemTime::get(), &params, &netdir);
guards.select_primary_guards(&params);
assert_eq!(guards.primary.len(), 2);
let (_kind, p_id1) = guards
- .pick_guard_id(&usage, &params, Instant::now())
+ .pick_guard_id(&usage, &params, Instant::get())
.unwrap();
- guards.record_success(&p_id1, &params, None, SystemTime::now());
+ guards.record_success(&p_id1, &params, None, SystemTime::get());
assert_eq!(guards.n_primary_without_id_info_in(&netdir), 0);
use tor_netdir::testnet;
@@ -1502,17 +1503,17 @@ mod test {
..GuardParams::default()
};
let mut guards1 = GuardSet::default();
- guards1.extend_sample_as_needed(SystemTime::now(), &params, &netdir);
+ guards1.extend_sample_as_needed(SystemTime::get(), &params, &netdir);
guards1.select_primary_guards(&params);
let mut guards2 = guards1.clone();
// Make a persistent change in guards1, and a different persistent change in guards2.
let id1 = guards1.primary[0].clone();
let id2 = guards1.primary[1].clone();
- guards1.record_success(&id1, &params, None, SystemTime::now());
- guards2.record_success(&id2, &params, None, SystemTime::now());
+ guards1.record_success(&id1, &params, None, SystemTime::get());
+ guards2.record_success(&id2, &params, None, SystemTime::get());
// Make a non-persistent change in guards2.
- guards2.record_failure(&id2, None, Instant::now());
+ guards2.record_failure(&id2, None, Instant::get());
// Copy status: make sure non-persistent status changed, and persistent didn't.
guards1.copy_ephemeral_status_into_newly_loaded_state(guards2);
@@ -1537,7 +1538,7 @@ mod test {
for _ in 0..4 {
// There is roughly a 1-in-5000 chance of getting the same set
// twice, so we loop until that doesn't happen.
- guards3.extend_sample_as_needed(SystemTime::now(), &params, &netdir);
+ guards3.extend_sample_as_needed(SystemTime::get(), &params, &netdir);
guards3.select_primary_guards(&params);
g3_set = guards3
.guards
diff --git a/crates/tor-guardmgr/src/skew.rs b/crates/tor-guardmgr/src/skew.rs
index a1ed084ed..99090f07d 100644
--- a/crates/tor-guardmgr/src/skew.rs
+++ b/crates/tor-guardmgr/src/skew.rs
@@ -7,7 +7,7 @@
// of bridges is very small, see if we can still use that to make a
// low-confidence value.
-use std::time::{Duration, Instant};
+use web_time_compat::{Duration, Instant};
use tor_proto::ClockSkew;
@@ -245,6 +245,7 @@ mod test {
//! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
use super::*;
use float_eq::assert_float_eq;
+ use web_time_compat::InstantExt;
/// Tolerance for float comparison.
const TOL: f64 = 0.00001;
@@ -327,7 +328,7 @@ mod test {
#[test]
fn estimate_with_no_data() {
// zero inputs -> output is none.
- let now = Instant::now();
+ let now = Instant::get();
let est = SkewEstimate::estimate_skew([].iter(), now);
assert!(est.is_none());
@@ -365,7 +366,7 @@ mod test {
mins.iter()
.map(|m| SkewObservation {
skew: ClockSkew::from_secs_f64(m * 60.0).unwrap(),
- when: Instant::now(),
+ when: Instant::get(),
})
.collect()
}
@@ -383,7 +384,7 @@ mod test {
// confidence.
let obs = from_minutes(&[-20.0, -10.0, -20.0, -25.0, 0.0, -18.0, -22.0, -22.0]);
- let est = SkewEstimate::estimate_skew(obs.iter(), Instant::now()).unwrap();
+ let est = SkewEstimate::estimate_skew(obs.iter(), Instant::get()).unwrap();
assert_eq!(
est.to_string(),
"slow by around 17m 7s (based on 8 recent observations, with some confidence)"
@@ -401,7 +402,7 @@ mod test {
-100.0, 100.0, -3.0, -2.0, 0.0, 1.0, 0.5, 6.0, 3.0, 0.5, 99.0,
]);
- let est = SkewEstimate::estimate_skew(obs.iter(), Instant::now()).unwrap();
+ let est = SkewEstimate::estimate_skew(obs.iter(), Instant::get()).unwrap();
assert_eq!(
est.to_string(),
"not skewed by more than 15m (based on 8 recent observations, with high confidence)"
diff --git a/crates/tor-guardmgr/src/util.rs b/crates/tor-guardmgr/src/util.rs
index bc27314fd..eea47b091 100644
--- a/crates/tor-guardmgr/src/util.rs
+++ b/crates/tor-guardmgr/src/util.rs
@@ -77,10 +77,11 @@ mod test {
//! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
use super::*;
use tor_basic_utils::test_rng::testing_rng;
+ use web_time_compat::SystemTimeExt;
#[test]
fn test_randomize_time() {
- let now = SystemTime::now();
+ let now = SystemTime::get();
let one_hour = humantime::parse_duration("1hr").unwrap();
let ten_sec = humantime::parse_duration("10s").unwrap();
let mut rng = testing_rng();