From 7d305578a998156ad466fb3f036a4fe0ad53a73e Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 17 Aug 2020 12:37:28 +0200 Subject: [PATCH 1/2] Distribute the network future polling time more evenly --- client/network/src/service.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/client/network/src/service.rs b/client/network/src/service.rs index d42af16f1d22e..a18e1c5ddc489 100644 --- a/client/network/src/service.rs +++ b/client/network/src/service.rs @@ -1380,7 +1380,22 @@ impl Future for NetworkWorker { } } + // 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 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, @@ -1420,7 +1435,16 @@ impl Future for NetworkWorker { } } + // `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); From 87e6ecce51f6251389a74f5a36e256b875ce07a7 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 17 Aug 2020 13:02:41 +0200 Subject: [PATCH 2/2] Update client/network/src/service.rs --- client/network/src/service.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/network/src/service.rs b/client/network/src/service.rs index a18e1c5ddc489..a552807a9aff0 100644 --- a/client/network/src/service.rs +++ b/client/network/src/service.rs @@ -1384,7 +1384,7 @@ impl Future for NetworkWorker { // 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 scheduled again. + // 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.