From afd3dcfe9876ac016aa6e1994c16820c0705e2dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Tue, 26 Mar 2024 11:13:39 +0100 Subject: [PATCH] Use async accept --- nano/node/transport/tcp_listener.cpp | 24 +++++++++++++++++++++--- nano/node/transport/tcp_listener.hpp | 2 ++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/nano/node/transport/tcp_listener.cpp b/nano/node/transport/tcp_listener.cpp index 0c3d773b24..a4358ac51a 100644 --- a/nano/node/transport/tcp_listener.cpp +++ b/nano/node/transport/tcp_listener.cpp @@ -5,6 +5,8 @@ #include #include +#include + #include using namespace std::chrono_literals; @@ -86,11 +88,16 @@ void nano::transport::tcp_listener::stop () { nano::lock_guard lock{ mutex }; stopped = true; + + boost::system::error_code ec; + acceptor.close (ec); // Best effort to close the acceptor, ignore errors + if (ec) + { + logger.debug (nano::log::type::tcp_listener, "Error while closing acceptor: {}", ec.message ()); + } } condition.notify_all (); - acceptor.close (); - if (thread.joinable ()) { thread.join (); @@ -193,9 +200,20 @@ void nano::transport::tcp_listener::run () } } +boost::asio::ip::tcp::socket nano::transport::tcp_listener::accept_socket () +{ + std::future future; + { + nano::unique_lock lock{ mutex }; + future = acceptor.async_accept (boost::asio::use_future); + } + future.wait (); + return future.get (); +} + auto nano::transport::tcp_listener::accept_one () -> accept_result { - auto raw_socket = acceptor.accept (); + auto raw_socket = accept_socket (); auto const remote_endpoint = raw_socket.remote_endpoint (); auto const local_endpoint = raw_socket.local_endpoint (); diff --git a/nano/node/transport/tcp_listener.hpp b/nano/node/transport/tcp_listener.hpp index 33652b4b15..4111992842 100644 --- a/nano/node/transport/tcp_listener.hpp +++ b/nano/node/transport/tcp_listener.hpp @@ -69,6 +69,8 @@ class tcp_listener final accept_result accept_one (); accept_result check_limits (boost::asio::ip::address const & ip); + boost::asio::ip::tcp::socket accept_socket (); + size_t count_per_ip (boost::asio::ip::address const & ip) const; size_t count_per_subnetwork (boost::asio::ip::address const & ip) const;