summaryrefslogtreecommitdiff
path: root/crates/tor-guardmgr/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/tor-guardmgr/src')
-rw-r--r--crates/tor-guardmgr/src/vanguards.rs10
-rw-r--r--crates/tor-guardmgr/src/vanguards/set.rs36
2 files changed, 33 insertions, 13 deletions
diff --git a/crates/tor-guardmgr/src/vanguards.rs b/crates/tor-guardmgr/src/vanguards.rs
index 12925686f..5d8f5d401 100644
--- a/crates/tor-guardmgr/src/vanguards.rs
+++ b/crates/tor-guardmgr/src/vanguards.rs
@@ -460,13 +460,15 @@ impl<R: Runtime> VanguardMgr<R> {
netdir_provider: &Arc<dyn NetDirProvider>,
now: SystemTime,
) -> Result<Option<Duration>, VanguardMgrError> {
- info!("Rotating vanguards");
-
let mut inner = self.inner.write().expect("poisoned lock");
let inner = &mut *inner;
let vanguard_sets = &mut inner.vanguard_sets;
- vanguard_sets.remove_expired(now);
+ let expired_count = vanguard_sets.remove_expired(now);
+
+ if expired_count > 0 {
+ info!("Rotating vanguards");
+ }
if let Some(netdir) = Self::timely_netdir(netdir_provider)? {
// If we have a NetDir, replenish the vanguard sets that don't have enough vanguards.
@@ -548,7 +550,7 @@ impl Inner {
match self.mode {
VanguardMode::Lite | VanguardMode::Disabled => Ok(()),
VanguardMode::Full => {
- debug!("The vanguards have changed; flushing vanguards to vanguard state file");
+ debug!("The vanguards may have changed; flushing to vanguard state file");
Ok(storage.store(&self.vanguard_sets)?)
}
}
diff --git a/crates/tor-guardmgr/src/vanguards/set.rs b/crates/tor-guardmgr/src/vanguards/set.rs
index f650d70cf..8170f9b51 100644
--- a/crates/tor-guardmgr/src/vanguards/set.rs
+++ b/crates/tor-guardmgr/src/vanguards/set.rs
@@ -106,9 +106,13 @@ impl VanguardSets {
}
/// Remove the vanguards that are expired at the specified timestamp.
- pub(super) fn remove_expired(&mut self, now: SystemTime) {
- self.l2_vanguards.remove_expired(now);
- self.l3_vanguards.remove_expired(now);
+ ///
+ /// Returns the number of vanguards that were removed.
+ pub(super) fn remove_expired(&mut self, now: SystemTime) -> usize {
+ let l2_expired = self.l2_vanguards.remove_expired(now);
+ let l3_expired = self.l3_vanguards.remove_expired(now);
+
+ l2_expired + l3_expired
}
/// Remove the vanguards that are no longer listed in `netdir`.
@@ -305,8 +309,10 @@ impl VanguardSet {
}
/// Remove the vanguards that are no longer listed in `netdir`
- fn remove_unlisted(&mut self, netdir: &NetDir) {
- self.vanguards.retain(|v| {
+ ///
+ /// Returns the number of vanguards that were unlisted.
+ fn remove_unlisted(&mut self, netdir: &NetDir) -> usize {
+ self.retain(|v| {
let cond = netdir.ids_listed(&v.id) != Some(false);
if !cond {
@@ -314,12 +320,14 @@ impl VanguardSet {
}
cond
- });
+ })
}
/// Remove the vanguards that are expired at the specified timestamp.
- fn remove_expired(&mut self, now: SystemTime) {
- self.vanguards.retain(|v| {
+ ///
+ /// Returns the number of vanguards that expired.
+ fn remove_expired(&mut self, now: SystemTime) -> usize {
+ self.retain(|v| {
let cond = v.when > now;
if !cond {
@@ -327,7 +335,17 @@ impl VanguardSet {
}
cond
- });
+ })
+ }
+
+ /// A wrapper around [`Vec::retain`] that returns the number of discarded elements.
+ fn retain<F>(&mut self, f: F) -> usize
+ where
+ F: FnMut(&TimeBoundVanguard) -> bool,
+ {
+ let old_len = self.vanguards.len();
+ self.vanguards.retain(f);
+ old_len - self.vanguards.len()
}
/// Find the timestamp of the vanguard that is due to expire next.