From c2352e834e7953d273c247f7527dfbaf9834e8aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Mon, 16 Dec 2024 22:33:47 +0100 Subject: [PATCH] Configurable socket queue size --- nano/core_test/socket.cpp | 8 ++++---- nano/node/transport/tcp_socket.cpp | 7 ++++--- nano/node/transport/tcp_socket.hpp | 8 +++++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/nano/core_test/socket.cpp b/nano/core_test/socket.cpp index f0e4911470..d0c213a066 100644 --- a/nano/core_test/socket.cpp +++ b/nano/core_test/socket.cpp @@ -171,10 +171,10 @@ TEST (socket, drop_policy) // We're going to write twice the queue size + 1, and the server isn't reading // The total number of drops should thus be 1 (the socket allows doubling the queue size for no_socket_drop) - func (nano::transport::tcp_socket::queue_size * 2 + 1); + func (nano::transport::tcp_socket::default_queue_size * 2 + 1); ASSERT_EQ (1, failed_writes); - func (nano::transport::tcp_socket::queue_size + 1); + func (nano::transport::tcp_socket::default_queue_size + 1); ASSERT_EQ (0, failed_writes); } @@ -398,7 +398,7 @@ TEST (socket_timeout, write) // create a client socket and send lots of data to fill the socket queue on the local and remote side // eventually, the all tcp queues should fill up and async_write will not be able to progress // and the timeout should kick in and close the socket, which will cause the async_write to return an error - auto socket = std::make_shared (*node, nano::transport::socket_endpoint::client); // socket with a max queue size much larger than OS buffers + auto socket = std::make_shared (*node, nano::transport::socket_endpoint::client, 1024 * 1024); // socket with a max queue size much larger than OS buffers socket->async_connect (acceptor.local_endpoint (), [&socket, &done] (boost::system::error_code const & ec_a) { EXPECT_FALSE (ec_a); @@ -513,7 +513,7 @@ TEST (socket_timeout, write_overlapped) // create a client socket and send lots of data to fill the socket queue on the local and remote side // eventually, the all tcp queues should fill up and async_write will not be able to progress // and the timeout should kick in and close the socket, which will cause the async_write to return an error - auto socket = std::make_shared (*node, nano::transport::socket_endpoint::client); // socket with a max queue size much larger than OS buffers + auto socket = std::make_shared (*node, nano::transport::socket_endpoint::client, 1024 * 1024); // socket with a max queue size much larger than OS buffers socket->async_connect (acceptor.local_endpoint (), [&socket, &done] (boost::system::error_code const & ec_a) { EXPECT_FALSE (ec_a); diff --git a/nano/node/transport/tcp_socket.cpp b/nano/node/transport/tcp_socket.cpp index 1e96e4e306..07bc9a4ec8 100644 --- a/nano/node/transport/tcp_socket.cpp +++ b/nano/node/transport/tcp_socket.cpp @@ -16,12 +16,13 @@ * socket */ -nano::transport::tcp_socket::tcp_socket (nano::node & node_a, nano::transport::socket_endpoint endpoint_type_a) : - tcp_socket{ node_a, boost::asio::ip::tcp::socket{ node_a.io_ctx }, {}, {}, endpoint_type_a } +nano::transport::tcp_socket::tcp_socket (nano::node & node_a, nano::transport::socket_endpoint endpoint_type_a, size_t queue_size_a) : + tcp_socket{ node_a, boost::asio::ip::tcp::socket{ node_a.io_ctx }, {}, {}, endpoint_type_a, queue_size_a } { } -nano::transport::tcp_socket::tcp_socket (nano::node & node_a, boost::asio::ip::tcp::socket raw_socket_a, boost::asio::ip::tcp::endpoint remote_endpoint_a, boost::asio::ip::tcp::endpoint local_endpoint_a, nano::transport::socket_endpoint endpoint_type_a) : +nano::transport::tcp_socket::tcp_socket (nano::node & node_a, boost::asio::ip::tcp::socket raw_socket_a, boost::asio::ip::tcp::endpoint remote_endpoint_a, boost::asio::ip::tcp::endpoint local_endpoint_a, nano::transport::socket_endpoint endpoint_type_a, size_t queue_size_a) : + queue_size{ queue_size_a }, send_queue{ queue_size }, node_w{ node_a.shared () }, strand{ node_a.io_ctx.get_executor () }, diff --git a/nano/node/transport/tcp_socket.hpp b/nano/node/transport/tcp_socket.hpp index db573921e0..6e1ba40da4 100644 --- a/nano/node/transport/tcp_socket.hpp +++ b/nano/node/transport/tcp_socket.hpp @@ -67,10 +67,10 @@ class tcp_socket final : public std::enable_shared_from_this friend class tcp_listener; public: - static size_t constexpr queue_size = 16; + static size_t constexpr default_queue_size = 16; public: - explicit tcp_socket (nano::node &, nano::transport::socket_endpoint = socket_endpoint::client); + explicit tcp_socket (nano::node &, nano::transport::socket_endpoint = socket_endpoint::client, size_t queue_size = default_queue_size); // TODO: Accepting remote/local endpoints as a parameter is unnecessary, but is needed for now to keep compatibility with the legacy code tcp_socket ( @@ -78,7 +78,8 @@ class tcp_socket final : public std::enable_shared_from_this boost::asio::ip::tcp::socket, boost::asio::ip::tcp::endpoint remote_endpoint, boost::asio::ip::tcp::endpoint local_endpoint, - nano::transport::socket_endpoint = socket_endpoint::server); + nano::transport::socket_endpoint = socket_endpoint::server, + size_t queue_size = default_queue_size); ~tcp_socket (); @@ -141,6 +142,7 @@ class tcp_socket final : public std::enable_shared_from_this } private: + size_t const queue_size; socket_queue send_queue; protected: