From f8333e1fc14f0a3017e33710564cdd54b8147e66 Mon Sep 17 00:00:00 2001 From: Alfredo Garcia Date: Thu, 28 Oct 2021 15:48:35 -0300 Subject: [PATCH 1/2] reduce demand --- zebra-network/src/peer_set/initialize.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/zebra-network/src/peer_set/initialize.rs b/zebra-network/src/peer_set/initialize.rs index ab355b6fcbb..0f21a4b7fe0 100644 --- a/zebra-network/src/peer_set/initialize.rs +++ b/zebra-network/src/peer_set/initialize.rs @@ -199,8 +199,16 @@ where ); let _ = candidates.update_initial(active_initial_peer_count).await; - // TODO: reduce demand by `active_outbound_connections.update_count()` (#2902) - for _ in 0..config.peerset_initial_target_size { + // Reduce demand by the last `active_outbound_connections.update_count()` output. + let demand_count = match config + .peerset_initial_target_size + .checked_sub(active_outbound_connections.update_count()) + { + Some(count) => count, + None => active_initial_peer_count, + }; + + for _ in 0..demand_count { let _ = demand_tx.try_send(MorePeers); } From 1571d71f81dd77f78bab267844dd173d32933b38 Mon Sep 17 00:00:00 2001 From: Alfredo Garcia Date: Thu, 28 Oct 2021 20:59:25 -0300 Subject: [PATCH 2/2] use `saturating_sub` --- zebra-network/src/peer_set/initialize.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/zebra-network/src/peer_set/initialize.rs b/zebra-network/src/peer_set/initialize.rs index 0f21a4b7fe0..ece01b72bfb 100644 --- a/zebra-network/src/peer_set/initialize.rs +++ b/zebra-network/src/peer_set/initialize.rs @@ -199,14 +199,10 @@ where ); let _ = candidates.update_initial(active_initial_peer_count).await; - // Reduce demand by the last `active_outbound_connections.update_count()` output. - let demand_count = match config + // Compute remaining connections to open. + let demand_count = config .peerset_initial_target_size - .checked_sub(active_outbound_connections.update_count()) - { - Some(count) => count, - None => active_initial_peer_count, - }; + .saturating_sub(active_outbound_connections.update_count()); for _ in 0..demand_count { let _ = demand_tx.try_send(MorePeers);