Refactor SshConnectionManager internals

It makes it easier to manage just one hash of connections
instead of 3 lists.

Change-Id: Id6a3661d8dfdb0bd269e35ece36410bd2477e5c6
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Jarek Kobus
2021-12-15 21:25:24 +01:00
parent 0326bbdc81
commit 37718e3874

View File

@@ -31,7 +31,7 @@
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <QCoreApplication> #include <QCoreApplication>
#include <QList> #include <QHash>
#include <QObject> #include <QObject>
#include <QThread> #include <QThread>
#include <QTimer> #include <QTimer>
@@ -39,20 +39,6 @@
namespace QSsh { namespace QSsh {
namespace Internal { namespace Internal {
class UnacquiredConnection {
public:
UnacquiredConnection(SshConnection *conn) : connection(conn), scheduledForRemoval(false) {}
SshConnection *connection;
bool scheduledForRemoval;
};
bool operator==(const UnacquiredConnection &c1, const UnacquiredConnection &c2) {
return c1.connection == c2.connection;
}
bool operator!=(const UnacquiredConnection &c1, const UnacquiredConnection &c2) {
return !(c1 == c2);
}
class SshConnectionManagerPrivate : public QObject class SshConnectionManagerPrivate : public QObject
{ {
public: public:
@@ -65,137 +51,146 @@ public:
~SshConnectionManagerPrivate() override ~SshConnectionManagerPrivate() override
{ {
for (const UnacquiredConnection &connection : qAsConst(m_unacquiredConnections)) { for (auto it = m_connections.cbegin(); it != m_connections.cend(); ++it) {
disconnect(connection.connection, nullptr, this, nullptr); SshConnection * const connection = it.key();
delete connection.connection; const SshConnectionState &state = it.value();
QTC_CHECK(state.refCount() == 0);
QTC_CHECK(!state.isStale());
disconnect(connection, nullptr, this, nullptr);
delete connection;
} }
QTC_CHECK(m_acquiredConnections.isEmpty());
QTC_CHECK(m_deprecatedConnections.isEmpty());
} }
SshConnection *acquireConnection(const SshConnectionParameters &sshParams) SshConnection *acquireConnection(const SshConnectionParameters &sshParams)
{ {
// Check in-use connections: if (SshSettings::connectionSharingEnabled()) {
for (SshConnection * const connection : qAsConst(m_acquiredConnections)) { for (auto it = m_connections.begin(); it != m_connections.end(); ++it) {
SshConnection * const connection = it.key();
if (connection->connectionParameters() != sshParams) if (connection->connectionParameters() != sshParams)
continue; continue;
if (connection->sharingEnabled() != SshSettings::connectionSharingEnabled()) SshConnectionState &state = it.value();
if (state.isStale())
continue; continue;
if (m_deprecatedConnections.contains(connection)) // we were asked to no longer use this one... if (state.refCount() == 0 && connection->state() != SshConnection::Connected)
continue; continue;
m_acquiredConnections.append(connection); state.ref();
return connection; return connection;
} }
// Check cached open connections:
for (int i = 0; i < m_unacquiredConnections.count(); ++i) {
SshConnection * const connection = m_unacquiredConnections.at(i).connection;
if (connection->state() != SshConnection::Connected
|| connection->connectionParameters() != sshParams)
continue;
m_unacquiredConnections.removeAt(i);
m_acquiredConnections.append(connection);
return connection;
} }
// create a new connection:
SshConnection * const connection = new SshConnection(sshParams); SshConnection * const connection = new SshConnection(sshParams);
if (SshSettings::connectionSharingEnabled()) {
connect(connection, &SshConnection::disconnected, connect(connection, &SshConnection::disconnected,
this, &SshConnectionManagerPrivate::cleanup); this, [this, connection] { cleanup(connection); });
if (SshSettings::connectionSharingEnabled()) m_connections.insert(connection, {});
m_acquiredConnections.append(connection); }
return connection; return connection;
} }
void releaseConnection(SshConnection *connection) void releaseConnection(SshConnection *connection)
{ {
const bool wasAcquired = m_acquiredConnections.removeOne(connection); auto it = m_connections.find(connection);
QTC_ASSERT(wasAcquired == connection->sharingEnabled(), return);
if (m_acquiredConnections.contains(connection))
return;
bool doDelete = false; bool doDelete = false;
if (!connection->sharingEnabled()) { if (it == m_connections.end()) {
doDelete = true; QTC_ASSERT(!connection->sharingEnabled(), return);
} else if (m_deprecatedConnections.removeOne(connection)
|| connection->state() != SshConnection::Connected) {
doDelete = true; doDelete = true;
} else { } else {
UnacquiredConnection uc(connection); SshConnectionState &state = it.value();
QTC_ASSERT(!m_unacquiredConnections.contains(uc), return); if (state.deref())
m_unacquiredConnections.append(uc); return;
if (state.isStale() || connection->state() != SshConnection::Connected) {
doDelete = true;
m_connections.erase(it);
}
} }
if (doDelete) { if (doDelete) {
disconnect(connection, nullptr, this, nullptr); disconnect(connection, nullptr, this, nullptr);
m_deprecatedConnections.removeAll(connection);
connection->deleteLater(); connection->deleteLater();
} }
} }
void forceNewConnection(const SshConnectionParameters &sshParams) void forceNewConnection(const SshConnectionParameters &sshParams)
{ {
for (int i = 0; i < m_unacquiredConnections.count(); ++i) { auto it = m_connections.begin();
SshConnection * const connection = m_unacquiredConnections.at(i).connection; while (it != m_connections.end()) {
if (connection->connectionParameters() == sshParams) { SshConnection * const connection = it.key();
disconnect(connection, nullptr, this, nullptr); if (connection->connectionParameters() != sshParams) {
delete connection; ++it;
m_unacquiredConnections.removeAt(i); continue;
break;
}
} }
for (SshConnection * const connection : qAsConst(m_acquiredConnections)) { SshConnectionState &state = it.value();
if (connection->connectionParameters() == sshParams) { if (state.refCount()) {
if (!m_deprecatedConnections.contains(connection)) state.makeStale();
m_deprecatedConnections.append(connection); ++it;
continue;
} }
disconnect(connection, nullptr, this, nullptr);
delete connection;
it = m_connections.erase(it);
} }
} }
private: private:
void cleanup() void cleanup(SshConnection *connection)
{ {
SshConnection *currentConnection = qobject_cast<SshConnection *>(sender()); auto it = m_connections.find(connection);
if (!currentConnection) if (it == m_connections.end())
return; return;
if (m_unacquiredConnections.removeOne(UnacquiredConnection(currentConnection))) { SshConnectionState &state = it.value();
disconnect(currentConnection, nullptr, this, nullptr); if (state.refCount())
currentConnection->deleteLater(); return;
}
disconnect(connection, nullptr, this, nullptr);
connection->deleteLater();
} }
void removeInactiveConnections() void removeInactiveConnections()
{ {
for (int i = m_unacquiredConnections.count() - 1; i >= 0; --i) { auto it = m_connections.begin();
UnacquiredConnection &c = m_unacquiredConnections[i]; while (it != m_connections.end()) {
if (c.scheduledForRemoval) { SshConnection * const connection = it.key();
disconnect(c.connection, nullptr, this, nullptr); SshConnectionState &state = it.value();
c.connection->deleteLater(); if (state.refCount() == 0 && state.scheduleForRemoval()) {
m_unacquiredConnections.removeAt(i); disconnect(connection, nullptr, this, nullptr);
connection->deleteLater();
it = m_connections.erase(it);
} else { } else {
c.scheduledForRemoval = true; ++it;
} }
} }
} }
private: private:
// We expect the number of concurrently open connections to be small. struct SshConnectionState {
// If that turns out to not be the case, we can still use a data void ref() { ++m_ref; m_scheduledForRemoval = false; }
// structure with faster access. bool deref() { QTC_ASSERT(m_ref, return false); return --m_ref; }
QList<UnacquiredConnection> m_unacquiredConnections; int refCount() const { return m_ref; }
// Can contain the same connection more than once; this acts as a reference count. void makeStale() { m_isStale = true; }
QList<SshConnection *> m_acquiredConnections; bool isStale() const { return m_isStale; }
QList<SshConnection *> m_deprecatedConnections; bool scheduleForRemoval()
{
const bool ret = m_scheduledForRemoval;
m_scheduledForRemoval = true;
return ret;
}
private:
int m_ref = 1; // 0 means unacquired connection
bool m_isStale = false;
bool m_scheduledForRemoval = false;
};
QHash<SshConnection *, SshConnectionState> m_connections;
QTimer m_removalTimer; QTimer m_removalTimer;
}; };