Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 30, 2024
1 parent 9d2a192 commit e299984
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
1 change: 1 addition & 0 deletions nano/lib/stats_enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ enum class detail : uint8_t
max_per_ip,
max_per_subnetwork,
excluded,
erase_dead,

// tcp_server
handshake,
Expand Down
38 changes: 18 additions & 20 deletions nano/node/transport/tcp_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ void nano::transport::tcp_listener::cleanup ()
erase_if (connections, [this] (auto const & connection) {
if (connection.socket.expired () && connection.server.expired ())
{
stats.inc (nano::stat::type::tcp_listener, nano::stat::detail::erase_dead);
logger.debug (nano::log::type::tcp_listener, "Evicting dead connection: {}", fmt::streamed (connection.endpoint));
return true;
}
Expand Down Expand Up @@ -187,8 +188,8 @@ void nano::transport::tcp_listener::run ()
}
if (!stopped)
{
debug_assert (false, "acceptor stopped unexpectedly");
logger.error (nano::log::type::tcp_listener, "Acceptor stopped unexpectedly");
debug_assert (false, "acceptor stopped unexpectedly");
}
}

Expand All @@ -211,8 +212,8 @@ auto nano::transport::tcp_listener::accept_one () -> accept_result
}
catch (boost::system::system_error const & ex)
{
logger.debug (nano::log::type::tcp_listener, "Error while closing socket after refusing connection: {}", ex.what ());
stats.inc (nano::stat::type::tcp_listener, nano::stat::detail::close_error, nano::stat::dir::in);
logger.debug (nano::log::type::tcp_listener, "Error while closing socket after refusing connection: {}", ex.what ());
}

return result;
Expand Down Expand Up @@ -240,13 +241,8 @@ auto nano::transport::tcp_listener::accept_one () -> accept_result

void nano::transport::tcp_listener::wait_available_slots ()
{
auto should_wait = [this] {
nano::lock_guard<nano::mutex> lock{ mutex };
return connections.size () >= max_inbound_connections;
};

nano::interval log_interval;
while (!stopped && should_wait ())
while (connection_count () >= max_inbound_connections && !stopped)
{
if (log_interval.elapsed (node.network_params.network.is_dev_network () ? 1s : 15s))
{
Expand All @@ -266,12 +262,21 @@ auto nano::transport::tcp_listener::check_limits (boost::asio::ip::address const

debug_assert (connections.size () <= max_inbound_connections); // Should be checked earlier (wait_available_slots)

if (node.network.excluded_peers.check (ip)) // true => error
{
stats.inc (nano::stat::type::tcp_listener, nano::stat::detail::excluded, nano::stat::dir::in);
logger.debug (nano::log::type::tcp_listener, "Rejected connection from excluded peer: {}", ip.to_string ());

return accept_result::excluded;
}

if (!node.flags.disable_max_peers_per_ip)
{
if (count_per_ip (ip) >= node.network_params.network.max_peers_per_ip)
if (auto count = count_per_ip (ip); count >= node.network_params.network.max_peers_per_ip)
{
stats.inc (nano::stat::type::tcp_listener, nano::stat::detail::max_per_ip, nano::stat::dir::in);
logger.debug (nano::log::type::tcp_listener, "Max connections per IP reached ({}), unable to open new connection", ip.to_string ());
logger.debug (nano::log::type::tcp_listener, "Max connections per IP reached (ip: {}, count: {}), unable to open new connection",
ip.to_string (), count);

return accept_result::too_many_per_ip;
}
Expand All @@ -280,23 +285,16 @@ auto nano::transport::tcp_listener::check_limits (boost::asio::ip::address const
// If the address is IPv4 we don't check for a network limit, since its address space isn't big as IPv6/64.
if (!node.flags.disable_max_peers_per_subnetwork && !nano::transport::is_ipv4_or_v4_mapped_address (ip))
{
if (count_per_subnetwork (ip) >= node.network_params.network.max_peers_per_subnetwork)
if (auto count = count_per_subnetwork (ip); count >= node.network_params.network.max_peers_per_subnetwork)
{
stats.inc (nano::stat::type::tcp_listener, nano::stat::detail::max_per_subnetwork, nano::stat::dir::in);
logger.debug (nano::log::type::tcp_listener, "Max connections per subnetwork reached ({}), unable to open new connection", ip.to_string ());
logger.debug (nano::log::type::tcp_listener, "Max connections per subnetwork reached (ip: {}, count: {}), unable to open new connection",
ip.to_string (), count);

return accept_result::too_many_per_subnetwork;
}
}

if (node.network.excluded_peers.check (ip)) // true => error
{
stats.inc (nano::stat::type::tcp_listener, nano::stat::detail::excluded, nano::stat::dir::in);
logger.debug (nano::log::type::tcp_listener, "Rejected connection from excluded peer: {}", ip.to_string ());

return accept_result::excluded;
}

return accept_result::accepted;
}

Expand Down

0 comments on commit e299984

Please sign in to comment.