optimizing performance issues under large-scale connection
diff --git a/lib/cpp/src/thrift/server/TNonblockingServer.cpp b/lib/cpp/src/thrift/server/TNonblockingServer.cpp
index ae92da3..d53535b 100644
--- a/lib/cpp/src/thrift/server/TNonblockingServer.cpp
+++ b/lib/cpp/src/thrift/server/TNonblockingServer.cpp
@@ -886,8 +886,8 @@
TNonblockingServer::~TNonblockingServer() {
// Close any active connections (moves them to the idle connection stack)
- while (activeConnections_.size()) {
- activeConnections_.front()->close();
+ while (!activeConnections_.empty()) {
+ (*activeConnections_.begin())->close();
}
// Clean up unused TConnection objects in connectionStack_
while (!connectionStack_.empty()) {
@@ -931,7 +931,8 @@
result->setSocket(socket);
result->init(ioThread);
}
- activeConnections_.push_back(result);
+
+ activeConnections_.insert(result);
return result;
}
@@ -941,11 +942,7 @@
void TNonblockingServer::returnConnection(TConnection* connection) {
Guard g(connMutex_);
- activeConnections_.erase(std::remove(activeConnections_.begin(),
- activeConnections_.end(),
- connection),
- activeConnections_.end());
-
+ activeConnections_.erase(connection);
if (connectionStackLimit_ && (connectionStack_.size() >= connectionStackLimit_)) {
delete connection;
--numTConnections_;
diff --git a/lib/cpp/src/thrift/server/TNonblockingServer.h b/lib/cpp/src/thrift/server/TNonblockingServer.h
index 65e569d..9f813ed 100644
--- a/lib/cpp/src/thrift/server/TNonblockingServer.h
+++ b/lib/cpp/src/thrift/server/TNonblockingServer.h
@@ -36,6 +36,7 @@
#include <vector>
#include <string>
#include <cstdlib>
+#include <unordered_set>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
@@ -259,7 +260,7 @@
* which in turn allows their transports, protocols, processors and handlers
* to deallocate and clean up correctly.
*/
- std::vector<TConnection*> activeConnections_;
+ std::unordered_set<TConnection*> activeConnections_;
/*
*/