forked from qt-creator/qt-creator
SSH: Close unused cached connections after a period of inactivity.
We keep connections open to avoid re-doing the authentication procedure when the user runs or deploys a remote app repeatedly. However, the connections should not be around indefinitely; we therefore now close them after about five minutes of inactivity. Change-Id: Ie677eb133b618d34c08528da2f2bc221472316b3 Reviewed-by: hjk <hjk121@nokiamail.com> Reviewed-by: Christian Kandeler <christian.kandeler@theqtcompany.com>
This commit is contained in:
@@ -38,9 +38,23 @@
|
||||
#include <QMutexLocker>
|
||||
#include <QObject>
|
||||
#include <QThread>
|
||||
#include <QTimer>
|
||||
|
||||
namespace QSsh {
|
||||
namespace Internal {
|
||||
class UnaquiredConnection {
|
||||
public:
|
||||
UnaquiredConnection(SshConnection *conn) : connection(conn), scheduledForRemoval(false) {}
|
||||
|
||||
SshConnection *connection;
|
||||
bool scheduledForRemoval;
|
||||
};
|
||||
bool operator==(const UnaquiredConnection &c1, const UnaquiredConnection &c2) {
|
||||
return c1.connection == c2.connection;
|
||||
}
|
||||
bool operator!=(const UnaquiredConnection &c1, const UnaquiredConnection &c2) {
|
||||
return !(c1 == c2);
|
||||
}
|
||||
|
||||
class SshConnectionManager : public QObject
|
||||
{
|
||||
@@ -50,13 +64,16 @@ public:
|
||||
SshConnectionManager()
|
||||
{
|
||||
moveToThread(QCoreApplication::instance()->thread());
|
||||
connect(&m_removalTimer, &QTimer::timeout,
|
||||
this, &SshConnectionManager::removeInactiveConnections);
|
||||
m_removalTimer.start(150000); // For a total timeout of five minutes.
|
||||
}
|
||||
|
||||
~SshConnectionManager()
|
||||
{
|
||||
foreach (SshConnection * const connection, m_unacquiredConnections) {
|
||||
disconnect(connection, 0, this, 0);
|
||||
delete connection;
|
||||
foreach (const UnaquiredConnection &connection, m_unacquiredConnections) {
|
||||
disconnect(connection.connection, 0, this, 0);
|
||||
delete connection.connection;
|
||||
}
|
||||
|
||||
QSSH_ASSERT(m_acquiredConnections.isEmpty());
|
||||
@@ -82,8 +99,9 @@ public:
|
||||
return connection;
|
||||
}
|
||||
|
||||
// Checked cached open connections:
|
||||
foreach (SshConnection * const connection, m_unacquiredConnections) {
|
||||
// Check cached open connections:
|
||||
foreach (const UnaquiredConnection &c, m_unacquiredConnections) {
|
||||
SshConnection * const connection = c.connection;
|
||||
if (connection->state() != SshConnection::Connected
|
||||
|| connection->connectionParameters() != sshParams)
|
||||
continue;
|
||||
@@ -97,7 +115,7 @@ public:
|
||||
Q_ARG(QObject *, QThread::currentThread()));
|
||||
}
|
||||
|
||||
m_unacquiredConnections.removeOne(connection);
|
||||
m_unacquiredConnections.removeOne(c);
|
||||
m_acquiredConnections.append(connection);
|
||||
return connection;
|
||||
}
|
||||
@@ -125,21 +143,21 @@ public:
|
||||
|| connection->state() != SshConnection::Connected) {
|
||||
doDelete = true;
|
||||
} else {
|
||||
QSSH_ASSERT_AND_RETURN(!m_unacquiredConnections.contains(connection));
|
||||
QSSH_ASSERT_AND_RETURN(!m_unacquiredConnections.contains(UnaquiredConnection(connection)));
|
||||
|
||||
// It can happen that two or more connections with the same parameters were acquired
|
||||
// if the clients were running in different threads. Only keep one of them in
|
||||
// such a case.
|
||||
bool haveConnection = false;
|
||||
foreach (SshConnection * const conn, m_unacquiredConnections) {
|
||||
if (conn->connectionParameters() == connection->connectionParameters()) {
|
||||
foreach (const UnaquiredConnection &c, m_unacquiredConnections) {
|
||||
if (c.connection->connectionParameters() == connection->connectionParameters()) {
|
||||
haveConnection = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!haveConnection) {
|
||||
connection->closeAllChannels(); // Clean up after neglectful clients.
|
||||
m_unacquiredConnections.append(connection);
|
||||
m_unacquiredConnections.append(UnaquiredConnection(connection));
|
||||
} else {
|
||||
doDelete = true;
|
||||
}
|
||||
@@ -157,7 +175,7 @@ public:
|
||||
QMutexLocker locker(&m_listMutex);
|
||||
|
||||
for (int i = 0; i < m_unacquiredConnections.count(); ++i) {
|
||||
SshConnection * const connection = m_unacquiredConnections.at(i);
|
||||
SshConnection * const connection = m_unacquiredConnections.at(i).connection;
|
||||
if (connection->connectionParameters() == sshParams) {
|
||||
disconnect(connection, 0, this, 0);
|
||||
delete connection;
|
||||
@@ -189,23 +207,39 @@ private slots:
|
||||
if (!currentConnection)
|
||||
return;
|
||||
|
||||
if (m_unacquiredConnections.removeOne(currentConnection)) {
|
||||
if (m_unacquiredConnections.removeOne(UnaquiredConnection(currentConnection))) {
|
||||
disconnect(currentConnection, 0, this, 0);
|
||||
currentConnection->deleteLater();
|
||||
}
|
||||
}
|
||||
|
||||
void removeInactiveConnections()
|
||||
{
|
||||
QMutexLocker locker(&m_listMutex);
|
||||
for (int i = m_unacquiredConnections.count() - 1; i >= 0; --i) {
|
||||
UnaquiredConnection &c = m_unacquiredConnections[i];
|
||||
if (c.scheduledForRemoval) {
|
||||
disconnect(c.connection, 0, this, 0);
|
||||
c.connection->deleteLater();
|
||||
m_unacquiredConnections.removeAt(i);
|
||||
} else {
|
||||
c.scheduledForRemoval = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
// We expect the number of concurrently open connections to be small.
|
||||
// If that turns out to not be the case, we can still use a data
|
||||
// structure with faster access.
|
||||
QList<SshConnection *> m_unacquiredConnections;
|
||||
QList<UnaquiredConnection> m_unacquiredConnections;
|
||||
|
||||
// Can contain the same connection more than once; this acts as a reference count.
|
||||
QList<SshConnection *> m_acquiredConnections;
|
||||
|
||||
QList<SshConnection *> m_deprecatedConnections;
|
||||
QMutex m_listMutex;
|
||||
QTimer m_removalTimer;
|
||||
};
|
||||
|
||||
} // namespace Internal
|
||||
|
||||
Reference in New Issue
Block a user