Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Distribute the network future polling time more evenly #6903

Merged
2 commits merged into from
Aug 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,22 @@ impl<B: BlockT + 'static, H: ExHashT> Future for NetworkWorker<B, H> {
}
}

// At the time of writing of this comment, due to a high volume of messages, the network
// worker sometimes takes a long time to process the loop below. When that happens, the
// rest of the polling is frozen. In order to avoid negative side-effects caused by this
// freeze, a limit to the number of iterations is enforced below. If the limit is reached,
// the task is interrupted then scheduled again.
//
// This allows for a more even distribution in the time taken by each sub-part of the
// polling.
let mut num_iterations = 0;
loop {
num_iterations += 1;
if num_iterations >= 100 {
cx.waker().wake_by_ref();
break;
}

// Process the next message coming from the `NetworkService`.
let msg = match this.from_service.poll_next_unpin(cx) {
Poll::Ready(Some(msg)) => msg,
Expand Down Expand Up @@ -1420,7 +1435,16 @@ impl<B: BlockT + 'static, H: ExHashT> Future for NetworkWorker<B, H> {
}
}

// `num_iterations` serves the same purpose as in the previous loop.
// See the previous loop for explanations.
let mut num_iterations = 0;
loop {
num_iterations += 1;
if num_iterations >= 1000 {
cx.waker().wake_by_ref();
break;
}

// Process the next action coming from the network.
let next_event = this.network_service.next_event();
futures::pin_mut!(next_event);
Expand Down