From a35b6cc8dd4d7a8d86380e49f8412f519ac31aa4 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 15 Apr 2019 19:52:49 +0800 Subject: [PATCH] Remove closed connections immediately Previously it relied on a timer to drop dead connections but that proved to be too slow when there is an incoming burst of connections. Fixes #10487. --- src/base/http/server.cpp | 13 ++++++++++--- src/base/http/server.h | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/base/http/server.cpp b/src/base/http/server.cpp index 04d7aff62..8dcf3174c 100644 --- a/src/base/http/server.cpp +++ b/src/base/http/server.cpp @@ -108,15 +108,22 @@ void Server::incomingConnection(const qintptr socketDescriptor) auto *c = new Connection(serverSocket, m_requestHandler, this); m_connections.append(c); + connect(serverSocket, &QAbstractSocket::disconnected, this, [c, this]() { removeConnection(c); }); +} + +void Server::removeConnection(Connection *connection) +{ + m_connections.removeOne(connection); + connection->deleteLater(); } void Server::dropTimedOutConnection() { QMutableListIterator i(m_connections); while (i.hasNext()) { - const auto *connection = i.next(); - if (connection->isClosed() || connection->hasExpired(KEEP_ALIVE_DURATION)) { - delete connection; + Connection *connection = i.next(); + if (connection->hasExpired(KEEP_ALIVE_DURATION)) { + connection->deleteLater(); i.remove(); } } diff --git a/src/base/http/server.h b/src/base/http/server.h index 2f13509fe..415e905ac 100644 --- a/src/base/http/server.h +++ b/src/base/http/server.h @@ -56,6 +56,7 @@ namespace Http private: void incomingConnection(qintptr socketDescriptor); + void removeConnection(Connection *connection); IRequestHandler *m_requestHandler; QList m_connections; // for tracking persistent connections