Delay subsequent requests to the same host

PR #19801.
Closes #8350.
This commit is contained in:
jNullj
2024-01-19 19:38:16 +02:00
committed by GitHub
parent 8ec3db1807
commit c5d7b62473
13 changed files with 98 additions and 10 deletions

View File

@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Jonathan Ketchker
* Copyright (C) 2015-2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
@@ -49,6 +50,8 @@
#include "downloadhandlerimpl.h"
#include "proxyconfigurationmanager.h"
using namespace std::chrono_literals;
namespace
{
// Disguise as Firefox to avoid web server banning
@@ -188,9 +191,9 @@ Net::DownloadHandler *Net::DownloadManager::download(const DownloadRequest &down
return downloadHandler;
}
void Net::DownloadManager::registerSequentialService(const Net::ServiceID &serviceID)
void Net::DownloadManager::registerSequentialService(const Net::ServiceID &serviceID, const std::chrono::seconds delay)
{
m_sequentialServices.insert(serviceID);
m_sequentialServices.insert(serviceID, delay);
}
QList<QNetworkCookie> Net::DownloadManager::cookiesForUrl(const QUrl &url) const
@@ -309,7 +312,7 @@ void Net::DownloadManager::processRequest(DownloadHandlerImpl *downloadHandler)
QNetworkReply *reply = m_networkManager->get(request);
connect(reply, &QNetworkReply::finished, this, [this, serviceID = ServiceID::fromURL(downloadHandler->url())]
{
QTimer::singleShot(0, this, [this, serviceID] { processWaitingJobs(serviceID); });
QTimer::singleShot(m_sequentialServices.value(serviceID, 0s), this, [this, serviceID] { processWaitingJobs(serviceID); });
});
downloadHandler->assignNetworkReply(reply);
}

View File

@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Jonathan Ketchker
* Copyright (C) 2015-2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
@@ -29,6 +30,8 @@
#pragma once
#include <chrono>
#include <QtTypes>
#include <QHash>
#include <QNetworkProxy>
@@ -137,7 +140,7 @@ namespace Net
template <typename Context, typename Func>
void download(const DownloadRequest &downloadRequest, bool useProxy, Context context, Func &&slot);
void registerSequentialService(const ServiceID &serviceID);
void registerSequentialService(const ServiceID &serviceID, std::chrono::seconds delay = std::chrono::seconds(0));
QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const;
bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url);
@@ -161,7 +164,8 @@ namespace Net
QNetworkAccessManager *m_networkManager = nullptr;
QNetworkProxy m_proxy;
QSet<ServiceID> m_sequentialServices;
// m_sequentialServices value is delay for same host requests
QHash<ServiceID, std::chrono::seconds> m_sequentialServices;
QSet<ServiceID> m_busyServices;
QHash<ServiceID, QQueue<DownloadHandlerImpl *>> m_waitingJobs;
};

View File

@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Jonathan Ketchker
* Copyright (C) 2015-2022 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
* Copyright (C) 2010 Arnaud Demaiziere <arnaud@qbittorrent.org>
@@ -98,7 +99,7 @@ Feed::Feed(const QUuid &uid, const QString &url, const QString &path, Session *s
else
connect(m_session, &Session::processingStateChanged, this, &Feed::handleSessionProcessingEnabledChanged);
Net::DownloadManager::instance()->registerSequentialService(Net::ServiceID::fromURL(m_url));
Net::DownloadManager::instance()->registerSequentialService(Net::ServiceID::fromURL(m_url), m_session->fetchDelay());
load();
}
@@ -159,6 +160,12 @@ void Feed::refresh()
emit stateChanged(this);
}
void Feed::updateFetchDelay()
{
// Update delay values for registered sequential services
Net::DownloadManager::instance()->registerSequentialService(Net::ServiceID::fromURL(m_url), m_session->fetchDelay());
}
QUuid Feed::uid() const
{
return m_uid;

View File

@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Jonathan Ketchker
* Copyright (C) 2015-2022 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
* Copyright (C) 2010 Arnaud Demaiziere <arnaud@qbittorrent.org>
@@ -75,6 +76,7 @@ namespace RSS
int unreadCount() const override;
void markAsRead() override;
void refresh() override;
void updateFetchDelay() override;
QUuid uid() const;
QString url() const;

View File

@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Jonathan Ketchker
* Copyright (C) 2017 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
* Copyright (C) 2010 Arnaud Demaiziere <arnaud@qbittorrent.org>
@@ -91,6 +92,12 @@ void Folder::refresh()
item->refresh();
}
void Folder::updateFetchDelay()
{
for (Item *item : asConst(items()))
item->updateFetchDelay();
}
QList<Item *> Folder::items() const
{
return m_items;

View File

@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Jonathan Ketchker
* Copyright (C) 2017 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
* Copyright (C) 2010 Arnaud Demaiziere <arnaud@qbittorrent.org>
@@ -54,6 +55,7 @@ namespace RSS
int unreadCount() const override;
void markAsRead() override;
void refresh() override;
void updateFetchDelay() override;
QList<Item *> items() const;

View File

@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Jonathan Ketchker
* Copyright (C) 2017 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
* Copyright (C) 2010 Arnaud Demaiziere <arnaud@qbittorrent.org>
@@ -52,6 +53,7 @@ namespace RSS
virtual int unreadCount() const = 0;
virtual void markAsRead() = 0;
virtual void refresh() = 0;
virtual void updateFetchDelay() = 0;
QString path() const;
QString name() const;

View File

@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Jonathan Ketchker
* Copyright (C) 2017 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
* Copyright (C) 2010 Arnaud Demaiziere <arnaud@qbittorrent.org>
@@ -62,6 +63,7 @@ QPointer<Session> Session::m_instance = nullptr;
Session::Session()
: m_storeProcessingEnabled(u"RSS/Session/EnableProcessing"_s)
, m_storeRefreshInterval(u"RSS/Session/RefreshInterval"_s, 30)
, m_storeFetchDelay(u"RSS/Session/FetchDelay"_s, 2)
, m_storeMaxArticlesPerFeed(u"RSS/Session/MaxArticlesPerFeed"_s, 50)
, m_workingThread(new QThread)
{
@@ -525,6 +527,19 @@ void Session::setRefreshInterval(const int refreshInterval)
}
}
std::chrono::seconds Session::fetchDelay() const
{
return std::chrono::seconds(m_storeFetchDelay);
}
void Session::setFetchDelay(const std::chrono::seconds delay)
{
if (delay == fetchDelay())
return;
m_storeFetchDelay = static_cast<qint64>(delay.count());
rootFolder()->updateFetchDelay();
}
QThread *Session::workingThread() const
{
return m_workingThread.get();

View File

@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Jonathan Ketchker
* Copyright (C) 2017 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
* Copyright (C) 2010 Arnaud Demaiziere <arnaud@qbittorrent.org>
@@ -68,6 +69,8 @@
* 3. Feed is JSON object (keys are property names, values are property values; 'uid' and 'url' are required)
*/
#include <chrono>
#include <QHash>
#include <QObject>
#include <QPointer>
@@ -114,6 +117,9 @@ namespace RSS
int refreshInterval() const;
void setRefreshInterval(int refreshInterval);
std::chrono::seconds fetchDelay() const;
void setFetchDelay(std::chrono::seconds delay);
nonstd::expected<void, QString> addFolder(const QString &path);
nonstd::expected<void, QString> addFeed(const QString &url, const QString &path);
nonstd::expected<void, QString> setFeedURL(const QString &path, const QString &url);
@@ -161,6 +167,7 @@ namespace RSS
CachedSettingValue<bool> m_storeProcessingEnabled;
CachedSettingValue<int> m_storeRefreshInterval;
CachedSettingValue<qint64> m_storeFetchDelay;
CachedSettingValue<int> m_storeMaxArticlesPerFeed;
Utils::Thread::UniquePtr m_workingThread;
AsyncFileStorage *m_confFileStorage = nullptr;