From c59f8a40f02f34a38c4e3fa97cc2bb2a4ecdb833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Sat, 25 Jan 2025 21:50:40 +0100 Subject: [PATCH] Generify rate limit. --- .../picture_loader/picture_loader_worker.cpp | 39 +++++++------------ .../ui/picture_loader/picture_loader_worker.h | 2 +- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/cockatrice/src/client/ui/picture_loader/picture_loader_worker.cpp b/cockatrice/src/client/ui/picture_loader/picture_loader_worker.cpp index edd831854..57bd9a73b 100644 --- a/cockatrice/src/client/ui/picture_loader/picture_loader_worker.cpp +++ b/cockatrice/src/client/ui/picture_loader/picture_loader_worker.cpp @@ -116,7 +116,7 @@ QNetworkReply *PictureLoaderWorker::makeRequest(const QUrl &url, PictureLoaderWo if (reply->error() == QNetworkReply::NoError) { worker->picDownloadFinished(reply); } else if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 429) { - handleRateLimit(reply, url, worker); + handleRateLimit(url, worker); } else { worker->picDownloadFinished(reply); } @@ -126,32 +126,23 @@ QNetworkReply *PictureLoaderWorker::makeRequest(const QUrl &url, PictureLoaderWo return reply; } -void PictureLoaderWorker::handleRateLimit(QNetworkReply *reply, const QUrl &url, PictureLoaderWorkerWork *worker) +void PictureLoaderWorker::handleRateLimit(const QUrl &url, PictureLoaderWorkerWork *worker) { - QByteArray responseData = reply->readAll(); - QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData); + // Prevent multiple rate-limit handling + if (!rateLimited) { + int retryAfter = 70; + rateLimited = true; + qWarning() << "Rate limit exceeded! Queuing requests for" << retryAfter << "seconds."; - if (jsonDoc.isObject()) { - QJsonObject jsonObj = jsonDoc.object(); - if (jsonObj.value("object").toString() == "error" && jsonObj.value("code").toString() == "rate_limited") { - int retryAfter = 70; // Default retry delay - - // Prevent multiple rate-limit handling - if (!rateLimited) { - rateLimited = true; - qWarning() << "Scryfall rate limit hit! Queuing requests for" << retryAfter << "seconds."; - - // Start a timer to reset the rate-limited state - rateLimitTimer.singleShot(retryAfter * 1000, this, [this]() { - qWarning() << "Rate limit expired. Resuming queued requests."; - processQueuedRequests(); - }); - } - - // Always queue the request even if already rate-limited - requestQueue.append(qMakePair(url, worker)); - } + // Start a timer to reset the rate-limited state + rateLimitTimer.singleShot(retryAfter * 1000, this, [this]() { + qWarning() << "Rate limit expired. Resuming queued requests."; + processQueuedRequests(); + }); } + + // Always queue the request even if already rate-limited + requestQueue.append(qMakePair(url, worker)); } void PictureLoaderWorker::processQueuedRequests() diff --git a/cockatrice/src/client/ui/picture_loader/picture_loader_worker.h b/cockatrice/src/client/ui/picture_loader/picture_loader_worker.h index dbe23a14c..46f710cb0 100644 --- a/cockatrice/src/client/ui/picture_loader/picture_loader_worker.h +++ b/cockatrice/src/client/ui/picture_loader/picture_loader_worker.h @@ -32,7 +32,7 @@ public: public slots: QNetworkReply *makeRequest(const QUrl &url, PictureLoaderWorkerWork *workThread); - void handleRateLimit(QNetworkReply *reply, const QUrl &url, PictureLoaderWorkerWork *worker); + void handleRateLimit(const QUrl &url, PictureLoaderWorkerWork *worker); void processQueuedRequests(); void imageLoadedSuccessfully(CardInfoPtr card, const QImage &image);