From 3ddfd780e4a479a6b33f374b036bfb11eb8498c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Tue, 5 Mar 2024 13:11:17 +0100 Subject: [PATCH] Throttle queries to active reps --- nano/node/repcrawler.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/nano/node/repcrawler.cpp b/nano/node/repcrawler.cpp index c89f142185..7934370b2a 100644 --- a/nano/node/repcrawler.cpp +++ b/nano/node/repcrawler.cpp @@ -228,6 +228,7 @@ std::vector> nano::rep_crawler::prepar constexpr std::size_t aggressive_count = 160; constexpr std::size_t conservative_max_attempts = 4; constexpr std::size_t aggressive_max_attempts = 8; + constexpr std::chrono::seconds rep_query_interval = std::chrono::seconds{ 60 }; stats.inc (nano::stat::type::rep_crawler, sufficient_weight ? nano::stat::detail::crawl_normal : nano::stat::detail::crawl_aggressive); @@ -236,10 +237,22 @@ std::vector> nano::rep_crawler::prepar auto random_peers = node.network.random_set (required_peer_count, 0, /* include channels with ephemeral remote ports */ true); - // Avoid querying the same peer multiple times when rep crawler is warmed up - auto const max_attempts = sufficient_weight ? conservative_max_attempts : aggressive_max_attempts; - erase_if (random_peers, [this, max_attempts] (std::shared_ptr const & channel) { - return queries.get ().count (channel) >= max_attempts; + auto should_query = [&, this] (std::shared_ptr const & channel) { + if (auto rep = reps.get ().find (channel); rep != reps.get ().end ()) + { + // Throttle queries to active reps + return elapsed (rep->last_request, rep_query_interval); + } + else + { + // Avoid querying the same peer multiple times when rep crawler is warmed up + auto const max_attempts = sufficient_weight ? conservative_max_attempts : aggressive_max_attempts; + return queries.get ().count (channel) < max_attempts; + } + }; + + erase_if (random_peers, [&, this] (std::shared_ptr const & channel) { + return !should_query (channel); }); return { random_peers.begin (), random_peers.end () };