From e7e1ff46384ae256c50f7efef27f598ec62b6fc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Fri, 22 Mar 2024 23:40:49 +0100 Subject: [PATCH] PUBLIC ATOMICS --- nano/core_test/node.cpp | 2 +- nano/node/transport/tcp_listener.cpp | 28 +++++++++++++++++++++++++++- nano/node/transport/tcp_listener.hpp | 8 +++----- nano/node/transport/tcp_server.cpp | 12 ++---------- nano/test_common/system.cpp | 8 ++++---- 5 files changed, 37 insertions(+), 21 deletions(-) diff --git a/nano/core_test/node.cpp b/nano/core_test/node.cpp index 177c6b4acf..7361b8b0a7 100644 --- a/nano/core_test/node.cpp +++ b/nano/core_test/node.cpp @@ -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)); diff --git a/nano/node/transport/tcp_listener.cpp b/nano/node/transport/tcp_listener.cpp index 3c213b9934..6d8c64d3f2 100644 --- a/nano/node/transport/tcp_listener.cpp +++ b/nano/node/transport/tcp_listener.cpp @@ -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 lock{ mutex }; return connections.size (); } +size_t nano::transport::tcp_listener::realtime_count () const +{ + nano::lock_guard 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 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 ()); diff --git a/nano/node/transport/tcp_listener.hpp b/nano/node/transport/tcp_listener.hpp index 26fe5b3655..85e1de9b27 100644 --- a/nano/node/transport/tcp_listener.hpp +++ b/nano/node/transport/tcp_listener.hpp @@ -36,8 +36,10 @@ class tcp_listener final void start (std::function 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 collect_container_info (std::string const & name); @@ -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 bootstrap_count{ 0 }; - std::atomic realtime_count{ 0 }; - private: struct entry { diff --git a/nano/node/transport/tcp_server.cpp b/nano/node/transport/tcp_server.cpp index 070a2882fb..3920d568d0 100644 --- a/nano/node/transport/tcp_server.cpp +++ b/nano/node/transport/tcp_server.cpp @@ -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) @@ -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; } @@ -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)); @@ -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)); diff --git a/nano/test_common/system.cpp b/nano/test_common/system.cpp index c9d1920946..92bac20118 100644 --- a/nano/test_common/system.cpp +++ b/nano/test_common/system.cpp @@ -122,8 +122,8 @@ std::shared_ptr 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); @@ -146,8 +146,8 @@ std::shared_ptr 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);