Browse Source

Use QThreadPool to invoke free disk space checking jobs

Prevent the creation of an excessive number of threads.
PR #18347.
Closes #18202.
adaptive-webui-19844
Vladimir Golovnev 2 years ago committed by GitHub
parent
commit
9cdf660ddb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      src/webui/api/freediskspacechecker.h
  2. 42
      src/webui/api/synccontroller.cpp
  3. 7
      src/webui/api/synccontroller.h

4
src/webui/api/freediskspacechecker.h

@ -30,13 +30,13 @@ @@ -30,13 +30,13 @@
#include <QObject>
class FreeDiskSpaceChecker : public QObject
class FreeDiskSpaceChecker final : public QObject
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(FreeDiskSpaceChecker)
public:
FreeDiskSpaceChecker() = default;
using QObject::QObject;
public slots:
void check();

42
src/webui/api/synccontroller.cpp

@ -32,7 +32,7 @@ @@ -32,7 +32,7 @@
#include <QJsonObject>
#include <QMetaObject>
#include <QThread>
#include <QThreadPool>
#include "base/bittorrent/cachestatus.h"
#include "base/bittorrent/infohash.h"
@ -49,7 +49,6 @@ @@ -49,7 +49,6 @@
#include "base/utils/string.h"
#include "apierror.h"
#include "freediskspacechecker.h"
#include "isessionmanager.h"
#include "serialize/serialize_torrent.h"
namespace
@ -177,7 +176,7 @@ namespace @@ -177,7 +176,7 @@ namespace
switch (static_cast<QMetaType::Type>(value.type()))
{
case QMetaType::QVariantMap:
{
{
QVariantMap map;
processMap(prevData[key].toMap(), value.toMap(), map);
if (!map.isEmpty())
@ -185,7 +184,7 @@ namespace @@ -185,7 +184,7 @@ namespace
}
break;
case QMetaType::QVariantHash:
{
{
QVariantMap map;
processHash(prevData[key].toHash(), value.toHash(), map, removedItems);
if (!map.isEmpty())
@ -195,7 +194,7 @@ namespace @@ -195,7 +194,7 @@ namespace
}
break;
case QMetaType::QVariantList:
{
{
QVariantList list;
processList(prevData[key].toList(), value.toList(), list, removedItems);
if (!list.isEmpty())
@ -372,15 +371,7 @@ namespace @@ -372,15 +371,7 @@ namespace
SyncController::SyncController(IApplication *app, QObject *parent)
: APIController(app, parent)
, m_freeDiskSpaceChecker {new FreeDiskSpaceChecker}
, m_freeDiskSpaceThread {new QThread}
{
m_freeDiskSpaceChecker->moveToThread(m_freeDiskSpaceThread.get());
connect(m_freeDiskSpaceThread.get(), &QThread::finished, m_freeDiskSpaceChecker, &QObject::deleteLater);
connect(m_freeDiskSpaceChecker, &FreeDiskSpaceChecker::checked, this, &SyncController::freeDiskSpaceSizeUpdated);
m_freeDiskSpaceThread->start();
invokeChecker();
m_freeDiskSpaceElapsedTimer.start();
}
@ -595,20 +586,27 @@ void SyncController::torrentPeersAction() @@ -595,20 +586,27 @@ void SyncController::torrentPeersAction()
qint64 SyncController::getFreeDiskSpace()
{
if (m_freeDiskSpaceElapsedTimer.hasExpired(FREEDISKSPACE_CHECK_TIMEOUT))
{
invokeChecker();
m_freeDiskSpaceElapsedTimer.restart();
}
return m_freeDiskSpace;
}
void SyncController::freeDiskSpaceSizeUpdated(qint64 freeSpaceSize)
void SyncController::invokeChecker()
{
m_freeDiskSpace = freeSpaceSize;
}
if (m_isFreeDiskSpaceCheckerRunning)
return;
void SyncController::invokeChecker() const
{
QMetaObject::invokeMethod(m_freeDiskSpaceChecker, &FreeDiskSpaceChecker::check, Qt::QueuedConnection);
auto *freeDiskSpaceChecker = new FreeDiskSpaceChecker;
connect(freeDiskSpaceChecker, &FreeDiskSpaceChecker::checked, this, [this](const qint64 freeSpaceSize)
{
m_freeDiskSpace = freeSpaceSize;
m_isFreeDiskSpaceCheckerRunning = false;
m_freeDiskSpaceElapsedTimer.restart();
});
connect(freeDiskSpaceChecker, &FreeDiskSpaceChecker::checked, freeDiskSpaceChecker, &QObject::deleteLater);
m_isFreeDiskSpaceCheckerRunning = true;
QThreadPool::globalInstance()->start([freeDiskSpaceChecker]
{
freeDiskSpaceChecker->check();
});
}

7
src/webui/api/synccontroller.h

@ -31,7 +31,6 @@ @@ -31,7 +31,6 @@
#include <QElapsedTimer>
#include <QVariantMap>
#include "base/utils/thread.h"
#include "apicontroller.h"
class QThread;
@ -51,16 +50,14 @@ public: @@ -51,16 +50,14 @@ public:
private slots:
void maindataAction();
void torrentPeersAction();
void freeDiskSpaceSizeUpdated(qint64 freeSpaceSize);
private:
qint64 getFreeDiskSpace();
void invokeChecker() const;
void invokeChecker();
qint64 m_freeDiskSpace = 0;
FreeDiskSpaceChecker *m_freeDiskSpaceChecker = nullptr;
Utils::Thread::UniquePtr m_freeDiskSpaceThread;
QElapsedTimer m_freeDiskSpaceElapsedTimer;
bool m_isFreeDiskSpaceCheckerRunning = false;
QVariantMap m_lastMaindataResponse;
QVariantMap m_lastAcceptedMaindataResponse;

Loading…
Cancel
Save