API-fix for extend_sample_as_needed.

Previously, the API said "you need to call this in a loop till it
returns false".  We did that in one place, but not another.

With the introduction of filters, forgetting to loop here becomes a
bug: so instead, change the behavior of extend_sample_as_needed so
it handles looping itself.
This commit is contained in:
Nick Mathewson 2022-06-10 10:07:45 -04:00
parent a92dfa0b2a
commit 96dfa97473
2 changed files with 20 additions and 19 deletions

View File

@ -766,16 +766,9 @@ impl GuardMgrInner {
self.guards self.guards
.active_guards_mut() .active_guards_mut()
.update_status_from_netdir(netdir); .update_status_from_netdir(netdir);
loop { self.guards
let added_any = self.guards.active_guards_mut().extend_sample_as_needed( .active_guards_mut()
now, .extend_sample_as_needed(now, &self.params, netdir);
&self.params,
netdir,
);
if !added_any {
break;
}
}
} }
self.guards self.guards

View File

@ -274,21 +274,29 @@ impl GuardSet {
/// Guards always start out un-confirmed. /// Guards always start out un-confirmed.
/// ///
/// Return true if any guards were added. /// Return true if any guards were added.
///
/// # Complications
///
/// For spec conformance, we only consider our filter when
/// selecting new guards if the filter is "very restrictive".
/// That makes it possible that this will add fewer
/// filter-permitted guards than we had wanted. Because of that,
/// it's advisable to run this function in a loop until it returns
/// false.
pub(crate) fn extend_sample_as_needed( pub(crate) fn extend_sample_as_needed(
&mut self, &mut self,
now: SystemTime, now: SystemTime,
params: &GuardParams, params: &GuardParams,
dir: &NetDir, dir: &NetDir,
) -> bool { ) -> bool {
let mut any_added = false;
while self.extend_sample_inner(now, params, dir) {
any_added = true;
}
any_added
}
/// Implementation helper for extend_sample_as_needed.
///
/// # Complications
///
/// For spec conformance, we only consider our filter when selecting new
/// guards if the filter is "very restrictive". That makes it possible that
/// this function will add fewer filter-permitted guards than we had wanted.
/// Because of that, this is a separate function, and
/// extend_sample_as_needed runs it in a loop until it returns false.
fn extend_sample_inner(&mut self, now: SystemTime, params: &GuardParams, dir: &NetDir) -> bool {
self.assert_consistency(); self.assert_consistency();
let n_filtered_usable = self let n_filtered_usable = self
.guards .guards