Skip to content

Commit

Permalink
improve: using parallel_flat_hash on connections (#1695)
Browse files Browse the repository at this point in the history
The library itself already has a container that supports parallelism, so
we'll let it manage these locks.
  • Loading branch information
mehah authored Oct 16, 2023
1 parent 5c4b79d commit 7ce2982
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
13 changes: 4 additions & 9 deletions src/server/network/connection/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,25 @@
#include "server/server.hpp"

Connection_ptr ConnectionManager::createConnection(asio::io_service &io_service, ConstServicePort_ptr servicePort) {
std::lock_guard<std::mutex> lockClass(connectionManagerLock);

auto connection = std::make_shared<Connection>(io_service, servicePort);
connections.insert(connection);
connections.emplace(connection);
return connection;
}

void ConnectionManager::releaseConnection(const Connection_ptr &connection) {
std::lock_guard<std::mutex> lockClass(connectionManagerLock);

connections.erase(connection);
}

void ConnectionManager::closeAll() {
std::lock_guard<std::mutex> 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();
}

Expand Down
3 changes: 1 addition & 2 deletions src/server/network/connection/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class ConnectionManager {
void closeAll();

private:
phmap::flat_hash_set<Connection_ptr> connections;
std::mutex connectionManagerLock;
phmap::parallel_flat_hash_set_m<Connection_ptr> connections;
};

class Connection : public std::enable_shared_from_this<Connection> {
Expand Down

0 comments on commit 7ce2982

Please sign in to comment.