Restore BitTorrent session asynchronously

Reduce the total startup time of the application and maintain sufficient responsiveness of the UI during startup due to the following:
1. Load resume data from disk asynchronously in separate thread;
2. Split handling of loaded resume data in chunks;
3. Reduce the number of emitting signals.

PR #16840.
This commit is contained in:
Vladimir Golovnev
2022-07-04 12:48:21 +03:00
committed by GitHub
parent ec1d2cba40
commit be7cfb78de
25 changed files with 927 additions and 553 deletions

View File

@@ -255,13 +255,12 @@ TorrentFilesWatcher *TorrentFilesWatcher::instance()
TorrentFilesWatcher::TorrentFilesWatcher(QObject *parent)
: QObject {parent}
, m_ioThread {new QThread(this)}
, m_asyncWorker {new TorrentFilesWatcher::Worker}
{
connect(m_asyncWorker, &TorrentFilesWatcher::Worker::magnetFound, this, &TorrentFilesWatcher::onMagnetFound);
connect(m_asyncWorker, &TorrentFilesWatcher::Worker::torrentFound, this, &TorrentFilesWatcher::onTorrentFound);
m_asyncWorker->moveToThread(m_ioThread);
m_ioThread->start();
const auto *btSession = BitTorrent::Session::instance();
if (btSession->isRestored())
initWorker();
else
connect(btSession, &BitTorrent::Session::restored, this, &TorrentFilesWatcher::initWorker);
load();
}
@@ -273,6 +272,27 @@ TorrentFilesWatcher::~TorrentFilesWatcher()
delete m_asyncWorker;
}
void TorrentFilesWatcher::initWorker()
{
Q_ASSERT(!m_asyncWorker);
m_asyncWorker = new TorrentFilesWatcher::Worker;
connect(m_asyncWorker, &TorrentFilesWatcher::Worker::magnetFound, this, &TorrentFilesWatcher::onMagnetFound);
connect(m_asyncWorker, &TorrentFilesWatcher::Worker::torrentFound, this, &TorrentFilesWatcher::onTorrentFound);
m_asyncWorker->moveToThread(m_ioThread);
m_ioThread->start();
for (auto it = m_watchedFolders.cbegin(); it != m_watchedFolders.cend(); ++it)
{
QMetaObject::invokeMethod(m_asyncWorker, [this, path = it.key(), options = it.value()]()
{
m_asyncWorker->setWatchedFolder(path, options);
});
}
}
void TorrentFilesWatcher::load()
{
QFile confFile {(specialFolderLocation(SpecialFolder::Config) / Path(CONF_FILE_NAME)).data()};
@@ -399,10 +419,13 @@ void TorrentFilesWatcher::doSetWatchedFolder(const Path &path, const WatchedFold
m_watchedFolders[path] = options;
QMetaObject::invokeMethod(m_asyncWorker, [this, path, options]()
if (m_asyncWorker)
{
m_asyncWorker->setWatchedFolder(path, options);
});
QMetaObject::invokeMethod(m_asyncWorker, [this, path, options]()
{
m_asyncWorker->setWatchedFolder(path, options);
});
}
emit watchedFolderSet(path, options);
}
@@ -411,10 +434,13 @@ void TorrentFilesWatcher::removeWatchedFolder(const Path &path)
{
if (m_watchedFolders.remove(path))
{
QMetaObject::invokeMethod(m_asyncWorker, [this, path]()
if (m_asyncWorker)
{
m_asyncWorker->removeWatchedFolder(path);
});
QMetaObject::invokeMethod(m_asyncWorker, [this, path]()
{
m_asyncWorker->removeWatchedFolder(path);
});
}
emit watchedFolderRemoved(path);