From 7ce2982522bca3c9b81c9bd8c55124b8287d16e9 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Mon, 16 Oct 2023 18:24:45 -0300 Subject: [PATCH] improve: using parallel_flat_hash on connections (#1695) The library itself already has a container that supports parallelism, so we'll let it manage these locks. --- src/server/network/connection/connection.cpp | 13 ++++--------- src/server/network/connection/connection.hpp | 3 +-- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/server/network/connection/connection.cpp b/src/server/network/connection/connection.cpp index c2df08b2824..b7764f4a2d1 100644 --- a/src/server/network/connection/connection.cpp +++ b/src/server/network/connection/connection.cpp @@ -17,30 +17,25 @@ #include "server/server.hpp" Connection_ptr ConnectionManager::createConnection(asio::io_service &io_service, ConstServicePort_ptr servicePort) { - std::lock_guard lockClass(connectionManagerLock); - auto connection = std::make_shared(io_service, servicePort); - connections.insert(connection); + connections.emplace(connection); return connection; } void ConnectionManager::releaseConnection(const Connection_ptr &connection) { - std::lock_guard lockClass(connectionManagerLock); - connections.erase(connection); } void ConnectionManager::closeAll() { - std::lock_guard lockClass(connectionManagerLock); - - for (const auto &connection : connections) { + connections.for_each([](const Connection_ptr &connection) { try { std::error_code error; connection->socket.shutdown(asio::ip::tcp::socket::shutdown_both, error); } catch (const std::system_error &systemError) { g_logger().error("[ConnectionManager::closeAll] - Failed to close connection, system error code {}", systemError.what()); } - } + }); + connections.clear(); } diff --git a/src/server/network/connection/connection.hpp b/src/server/network/connection/connection.hpp index 903d607632c..c240046a01c 100644 --- a/src/server/network/connection/connection.hpp +++ b/src/server/network/connection/connection.hpp @@ -42,8 +42,7 @@ class ConnectionManager { void closeAll(); private: - phmap::flat_hash_set connections; - std::mutex connectionManagerLock; + phmap::parallel_flat_hash_set_m connections; }; class Connection : public std::enable_shared_from_this {