diff options
Diffstat (limited to 'crates/tor-guardmgr/src/vanguards')
| -rw-r--r-- | crates/tor-guardmgr/src/vanguards/set.rs | 36 |
1 files changed, 27 insertions, 9 deletions
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. |
