Skip to content

Commit

Permalink
Configurable socket queue size
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Dec 16, 2024
1 parent 38d52c7 commit c2352e8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
8 changes: 4 additions & 4 deletions nano/core_test/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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<nano::transport::tcp_socket> (*node, nano::transport::socket_endpoint::client); // socket with a max queue size much larger than OS buffers
auto socket = std::make_shared<nano::transport::tcp_socket> (*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);
Expand Down Expand Up @@ -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<nano::transport::tcp_socket> (*node, nano::transport::socket_endpoint::client); // socket with a max queue size much larger than OS buffers
auto socket = std::make_shared<nano::transport::tcp_socket> (*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);

Expand Down
7 changes: 4 additions & 3 deletions nano/node/transport/tcp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 () },
Expand Down
8 changes: 5 additions & 3 deletions nano/node/transport/tcp_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,19 @@ class tcp_socket final : public std::enable_shared_from_this<tcp_socket>
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 (
nano::node &,
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 ();

Expand Down Expand Up @@ -141,6 +142,7 @@ class tcp_socket final : public std::enable_shared_from_this<tcp_socket>
}

private:
size_t const queue_size;
socket_queue send_queue;

protected:
Expand Down

0 comments on commit c2352e8

Please sign in to comment.