Skip to content

Commit 77a34c0

Browse files
mxindenromanb
andauthored
protocols/kad/query/peers/closest: Make at_capacity use max (#1548)
When not making progress for `parallelism` time a `ClosestPeersIter` becomes `State::Stalled`. When stalled an iterator is allowed to make more parallel requests up to `num_results`. If `num_results` is smaller than `parallelism` make sure to still allow up to `parallelism` requests in-flight. Co-Authored-By: Roman Borschel <[email protected]>
1 parent aa71158 commit 77a34c0

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

protocols/kad/src/query/peers/closest.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,9 @@ impl ClosestPeersIter {
369369
/// k closest nodes it has not already queried".
370370
fn at_capacity(&self) -> bool {
371371
match self.state {
372-
State::Stalled => self.num_waiting >= self.config.num_results,
372+
State::Stalled => self.num_waiting >= usize::max(
373+
self.config.num_results, self.config.parallelism
374+
),
373375
State::Iterating { .. } => self.num_waiting >= self.config.parallelism,
374376
State::Finished => true
375377
}
@@ -691,4 +693,29 @@ mod tests {
691693

692694
QuickCheck::new().tests(10).quickcheck(prop as fn(_) -> _)
693695
}
696+
697+
#[test]
698+
fn stalled_at_capacity() {
699+
fn prop(mut iter: ClosestPeersIter) {
700+
iter.state = State::Stalled;
701+
702+
for i in 0..usize::max(iter.config.parallelism, iter.config.num_results) {
703+
iter.num_waiting = i;
704+
assert!(
705+
!iter.at_capacity(),
706+
"Iterator should not be at capacity if less than \
707+
`max(parallelism, num_results)` requests are waiting.",
708+
)
709+
}
710+
711+
iter.num_waiting = usize::max(iter.config.parallelism, iter.config.num_results);
712+
assert!(
713+
iter.at_capacity(),
714+
"Iterator should be at capacity if `max(parallelism, num_results)` requests are \
715+
waiting.",
716+
)
717+
}
718+
719+
QuickCheck::new().tests(10).quickcheck(prop as fn(_))
720+
}
694721
}

0 commit comments

Comments
 (0)