Skip to content

Commit

Permalink
Reach out to cached peers
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Apr 17, 2024
1 parent 2093bbb commit 3f9551a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
2 changes: 2 additions & 0 deletions nano/lib/stats_enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ enum class detail : uint8_t
loop_keepalive,
loop_reachout,
merge_peer,
reachout_live,
reachout_cached,

// tcp
tcp_accept_success,
Expand Down
59 changes: 47 additions & 12 deletions nano/node/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ nano::network::~network ()
debug_assert (processing_threads.empty ());
debug_assert (!cleanup_thread.joinable ());
debug_assert (!keepalive_thread.joinable ());
debug_assert (!reachout_thread.joinable ());
debug_assert (!reachout_cached_thread.joinable ());
}

void nano::network::start ()
Expand All @@ -53,6 +55,11 @@ void nano::network::start ()
run_reachout ();
});

reachout_cached_thread = std::thread ([this] () {
nano::thread_role::set (nano::thread_role::name::network_reachout);
run_reachout_cached ();
});

if (!node.flags.disable_tcp_realtime)
{
tcp_channels.start ();
Expand Down Expand Up @@ -84,18 +91,10 @@ void nano::network::stop ()
}
processing_threads.clear ();

if (keepalive_thread.joinable ())
{
keepalive_thread.join ();
}
if (cleanup_thread.joinable ())
{
cleanup_thread.join ();
}
if (reachout_thread.joinable ())
{
reachout_thread.join ();
}
join_or_pass (keepalive_thread);
join_or_pass (cleanup_thread);
join_or_pass (reachout_thread);
join_or_pass (reachout_cached_thread);

port = 0;
}
Expand Down Expand Up @@ -203,6 +202,8 @@ void nano::network::run_reachout ()
return;
}

node.stats.inc (nano::stat::type::network, nano::stat::detail::reachout_live);

merge_peer (peer);

// Throttle reachout attempts
Expand All @@ -214,6 +215,40 @@ void nano::network::run_reachout ()
}
}

void nano::network::run_reachout_cached ()
{
nano::unique_lock<nano::mutex> lock{ mutex };
while (!stopped)
{
condition.wait_for (lock, node.network_params.network.merge_period);
if (stopped)
{
return;
}
lock.unlock ();

node.stats.inc (nano::stat::type::network, nano::stat::detail::loop_reachout);

auto cached_peers = node.peer_cache.cached_peers ();
for (auto const & peer : cached_peers)
{
if (stopped)
{
return;
}

node.stats.inc (nano::stat::type::network, nano::stat::detail::reachout_cached);

merge_peer (peer);

// Throttle reachout attempts
std::this_thread::sleep_for (node.network_params.network.merge_period);
}

lock.lock ();
}
}

void nano::network::send_keepalive (std::shared_ptr<nano::transport::channel> const & channel_a)
{
nano::keepalive message{ node.network_params.network };
Expand Down
2 changes: 2 additions & 0 deletions nano/node/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class network final
void run_cleanup ();
void run_keepalive ();
void run_reachout ();
void run_reachout_cached ();
void process_message (nano::message const &, std::shared_ptr<nano::transport::channel> const &);

private: // Dependencies
Expand Down Expand Up @@ -139,6 +140,7 @@ class network final
std::thread cleanup_thread;
std::thread keepalive_thread;
std::thread reachout_thread;
std::thread reachout_cached_thread;

public:
static unsigned const broadcast_interval_ms = 10;
Expand Down

0 comments on commit 3f9551a

Please sign in to comment.