Skip to content

Commit

Permalink
PUBLIC ATOMICS
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 22, 2024
1 parent adfa0f7 commit e7e1ff4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2827,7 +2827,7 @@ TEST (node, peers)
node2->start ();
ASSERT_TIMELY (10s, !node2->network.empty () && !node1->network.empty ())
// Wait to finish TCP node ID handshakes
ASSERT_TIMELY (10s, node1->tcp_listener->realtime_count != 0 && node2->tcp_listener->realtime_count != 0);
ASSERT_TIMELY (10s, node1->tcp_listener->realtime_count () != 0 && node2->tcp_listener->realtime_count () != 0);
// Confirm that the peers match with the endpoints we are expecting
ASSERT_EQ (1, node1->network.size ());
auto list1 (node1->network.list (2));
Expand Down
28 changes: 27 additions & 1 deletion nano/node/transport/tcp_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,38 @@ auto nano::transport::tcp_listener::check_limits (boost::asio::ip::address const
return check_result::accepted;
}

std::size_t nano::transport::tcp_listener::connection_count () const
size_t nano::transport::tcp_listener::connection_count () const
{
nano::lock_guard<nano::mutex> lock{ mutex };
return connections.size ();
}

size_t nano::transport::tcp_listener::realtime_count () const
{
nano::lock_guard<nano::mutex> lock{ mutex };

return std::count_if (connections.begin (), connections.end (), [] (auto const & connection) {
if (auto socket = connection.socket.lock ())
{
return socket->is_realtime_connection ();
}
return false;
});
}

size_t nano::transport::tcp_listener::bootstrap_count () const
{
nano::lock_guard<nano::mutex> lock{ mutex };

return std::count_if (connections.begin (), connections.end (), [] (auto const & connection) {
if (auto socket = connection.socket.lock ())
{
return socket->is_bootstrap_connection ();
}
return false;
});
}

size_t nano::transport::tcp_listener::count_per_ip (boost::asio::ip::address const & ip) const
{
debug_assert (!mutex.try_lock ());
Expand Down
8 changes: 3 additions & 5 deletions nano/node/transport/tcp_listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ class tcp_listener final
void start (std::function<bool (std::shared_ptr<nano::transport::socket> const &, boost::system::error_code const &)> callback = {});
void stop ();

std::size_t connection_count () const;
nano::tcp_endpoint endpoint () const;
size_t connection_count () const;
size_t realtime_count () const;
size_t bootstrap_count () const;

std::unique_ptr<nano::container_info_component> collect_container_info (std::string const & name);

Expand Down Expand Up @@ -70,10 +72,6 @@ class tcp_listener final
size_t count_per_ip (boost::asio::ip::address const & ip) const;
size_t count_per_subnetwork (boost::asio::ip::address const & ip) const;

public:
std::atomic<std::size_t> bootstrap_count{ 0 };
std::atomic<std::size_t> realtime_count{ 0 };

private:
struct entry
{
Expand Down
12 changes: 2 additions & 10 deletions nano/node/transport/tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,8 @@ nano::transport::tcp_server::~tcp_server ()

node->logger.debug (nano::log::type::tcp_server, "Exiting TCP server ({})", fmt::streamed (remote_endpoint));

if (socket->type () == nano::transport::socket_type::bootstrap)
if (socket->type () == nano::transport::socket_type::realtime)
{
--node->tcp_listener->bootstrap_count;
}
else if (socket->type () == nano::transport::socket_type::realtime)
{
--node->tcp_listener->realtime_count;

// Clear temporary channel
auto exisiting_response_channel (node->network.tcp_channels.find_channel (remote_endpoint));
if (exisiting_response_channel != nullptr)
Expand Down Expand Up @@ -608,7 +602,7 @@ bool nano::transport::tcp_server::to_bootstrap_connection ()
{
return false;
}
if (node->tcp_listener->bootstrap_count >= node->config.bootstrap_connections_max)
if (node->tcp_listener->bootstrap_count () >= node->config.bootstrap_connections_max)
{
return false;
}
Expand All @@ -617,7 +611,6 @@ bool nano::transport::tcp_server::to_bootstrap_connection ()
return false;
}

++node->tcp_listener->bootstrap_count;
socket->type_set (nano::transport::socket_type::bootstrap);

node->logger.debug (nano::log::type::tcp_server, "Switched to bootstrap mode ({})", fmt::streamed (remote_endpoint));
Expand All @@ -642,7 +635,6 @@ bool nano::transport::tcp_server::to_realtime_connection (nano::account const &
}

remote_node_id = node_id;
++node->tcp_listener->realtime_count;
socket->type_set (nano::transport::socket_type::realtime);

node->logger.debug (nano::log::type::tcp_server, "Switched to realtime mode ({})", fmt::streamed (remote_endpoint));
Expand Down
8 changes: 4 additions & 4 deletions nano/test_common/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ std::shared_ptr<nano::node> nano::test::system::add_node (nano::node_config cons
auto starting_size_1 = node1->network.size ();
auto starting_size_2 = node2->network.size ();

auto starting_realtime_1 = node1->tcp_listener->realtime_count.load ();
auto starting_realtime_2 = node2->tcp_listener->realtime_count.load ();
auto starting_realtime_1 = node1->tcp_listener->realtime_count ();
auto starting_realtime_2 = node2->tcp_listener->realtime_count ();

auto starting_keepalives_1 = node1->stats.count (stat::type::message, stat::detail::keepalive, stat::dir::in);
auto starting_keepalives_2 = node2->stats.count (stat::type::message, stat::detail::keepalive, stat::dir::in);
Expand All @@ -146,8 +146,8 @@ std::shared_ptr<nano::node> nano::test::system::add_node (nano::node_config cons
{
// Wait for initial connection finish
auto ec = poll_until_true (5s, [&node1, &node2, starting_realtime_1, starting_realtime_2] () {
auto realtime_1 = node1->tcp_listener->realtime_count.load ();
auto realtime_2 = node2->tcp_listener->realtime_count.load ();
auto realtime_1 = node1->tcp_listener->realtime_count ();
auto realtime_2 = node2->tcp_listener->realtime_count ();
return realtime_1 > starting_realtime_1 && realtime_2 > starting_realtime_2;
});
debug_assert (!ec);
Expand Down

0 comments on commit e7e1ff4

Please sign in to comment.