Skip to content

Commit

Permalink
Throttle queries to active reps
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 5, 2024
1 parent 5cc8256 commit 3ddfd78
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions nano/node/repcrawler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ std::vector<std::shared_ptr<nano::transport::channel>> 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);

Expand All @@ -236,10 +237,22 @@ std::vector<std::shared_ptr<nano::transport::channel>> 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<nano::transport::channel> const & channel) {
return queries.get<tag_channel> ().count (channel) >= max_attempts;
auto should_query = [&, this] (std::shared_ptr<nano::transport::channel> const & channel) {
if (auto rep = reps.get<tag_channel> ().find (channel); rep != reps.get<tag_channel> ().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<tag_channel> ().count (channel) < max_attempts;
}
};

erase_if (random_peers, [&, this] (std::shared_ptr<nano::transport::channel> const & channel) {
return !should_query (channel);
});

return { random_peers.begin (), random_peers.end () };
Expand Down

0 comments on commit 3ddfd78

Please sign in to comment.