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. 36
      src/webui/api/synccontroller.cpp
  3. 7
      src/webui/api/synccontroller.h

4
src/webui/api/freediskspacechecker.h

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

36
src/webui/api/synccontroller.cpp

@ -32,7 +32,7 @@
#include <QJsonObject> #include <QJsonObject>
#include <QMetaObject> #include <QMetaObject>
#include <QThread> #include <QThreadPool>
#include "base/bittorrent/cachestatus.h" #include "base/bittorrent/cachestatus.h"
#include "base/bittorrent/infohash.h" #include "base/bittorrent/infohash.h"
@ -49,7 +49,6 @@
#include "base/utils/string.h" #include "base/utils/string.h"
#include "apierror.h" #include "apierror.h"
#include "freediskspacechecker.h" #include "freediskspacechecker.h"
#include "isessionmanager.h"
#include "serialize/serialize_torrent.h" #include "serialize/serialize_torrent.h"
namespace namespace
@ -372,15 +371,7 @@ namespace
SyncController::SyncController(IApplication *app, QObject *parent) SyncController::SyncController(IApplication *app, QObject *parent)
: APIController(app, 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(); invokeChecker();
m_freeDiskSpaceElapsedTimer.start(); m_freeDiskSpaceElapsedTimer.start();
} }
@ -595,20 +586,27 @@ void SyncController::torrentPeersAction()
qint64 SyncController::getFreeDiskSpace() qint64 SyncController::getFreeDiskSpace()
{ {
if (m_freeDiskSpaceElapsedTimer.hasExpired(FREEDISKSPACE_CHECK_TIMEOUT)) if (m_freeDiskSpaceElapsedTimer.hasExpired(FREEDISKSPACE_CHECK_TIMEOUT))
{
invokeChecker(); invokeChecker();
m_freeDiskSpaceElapsedTimer.restart();
}
return m_freeDiskSpace; return m_freeDiskSpace;
} }
void SyncController::freeDiskSpaceSizeUpdated(qint64 freeSpaceSize) void SyncController::invokeChecker()
{ {
m_freeDiskSpace = freeSpaceSize; if (m_isFreeDiskSpaceCheckerRunning)
} return;
void SyncController::invokeChecker() const auto *freeDiskSpaceChecker = new FreeDiskSpaceChecker;
{ connect(freeDiskSpaceChecker, &FreeDiskSpaceChecker::checked, this, [this](const qint64 freeSpaceSize)
QMetaObject::invokeMethod(m_freeDiskSpaceChecker, &FreeDiskSpaceChecker::check, Qt::QueuedConnection); {
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 @@
#include <QElapsedTimer> #include <QElapsedTimer>
#include <QVariantMap> #include <QVariantMap>
#include "base/utils/thread.h"
#include "apicontroller.h" #include "apicontroller.h"
class QThread; class QThread;
@ -51,16 +50,14 @@ public:
private slots: private slots:
void maindataAction(); void maindataAction();
void torrentPeersAction(); void torrentPeersAction();
void freeDiskSpaceSizeUpdated(qint64 freeSpaceSize);
private: private:
qint64 getFreeDiskSpace(); qint64 getFreeDiskSpace();
void invokeChecker() const; void invokeChecker();
qint64 m_freeDiskSpace = 0; qint64 m_freeDiskSpace = 0;
FreeDiskSpaceChecker *m_freeDiskSpaceChecker = nullptr;
Utils::Thread::UniquePtr m_freeDiskSpaceThread;
QElapsedTimer m_freeDiskSpaceElapsedTimer; QElapsedTimer m_freeDiskSpaceElapsedTimer;
bool m_isFreeDiskSpaceCheckerRunning = false;
QVariantMap m_lastMaindataResponse; QVariantMap m_lastMaindataResponse;
QVariantMap m_lastAcceptedMaindataResponse; QVariantMap m_lastAcceptedMaindataResponse;

Loading…
Cancel
Save