diff options
Diffstat (limited to 'crates/tor-guardmgr')
| -rw-r--r-- | crates/tor-guardmgr/Cargo.toml | 7 | ||||
| -rw-r--r-- | crates/tor-guardmgr/src/vanguards.rs | 10 | ||||
| -rw-r--r-- | crates/tor-guardmgr/src/vanguards/set.rs | 36 |
3 files changed, 37 insertions, 16 deletions
diff --git a/crates/tor-guardmgr/Cargo.toml b/crates/tor-guardmgr/Cargo.toml index 683f792e2..573912145 100644 --- a/crates/tor-guardmgr/Cargo.toml +++ b/crates/tor-guardmgr/Cargo.toml @@ -16,6 +16,7 @@ default = [] full = [ "bridge-client", "pt-client", + "vanguards", "safelog/full", "tor-basic-utils/full", "tor-config/full", @@ -31,7 +32,7 @@ full = [ "tor-units/full", "tor-async-utils/full", "tor-relay-selection/full", ] -experimental = ["testing", "vanguards"] +experimental = ["testing"] # Support for using bridges as a client. Note that this is not the same as # the pt-client feature, since here we are not concerned with @@ -39,12 +40,12 @@ experimental = ["testing", "vanguards"] bridge-client = ["tor-netdoc/routerdesc", "tor-protover"] # Support for pluggable transports. pt-client = ["bridge-client", "tor-linkspec/pt-client"] +# Vanguards support +vanguards = ["tor-relay-selection/vanguards"] # Enable testing-only APIs. APIs under this feature are not # covered by semver. testing = ["tor-netdir/testing", "__is_experimental"] -# Vanguards support -vanguards = ["tor-relay-selection/vanguards", "__is_experimental"] __is_experimental = [] 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. |
