Generify rate limit.

This commit is contained in:
Lukas Brübach 2025-01-25 21:50:40 +01:00
parent 1d4bdc202d
commit c59f8a40f0
2 changed files with 16 additions and 25 deletions

View file

@ -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,20 +126,13 @@ 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);
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) {
int retryAfter = 70;
rateLimited = true;
qWarning() << "Scryfall rate limit hit! Queuing requests for" << retryAfter << "seconds.";
qWarning() << "Rate limit exceeded! Queuing requests for" << retryAfter << "seconds.";
// Start a timer to reset the rate-limited state
rateLimitTimer.singleShot(retryAfter * 1000, this, [this]() {
@ -150,8 +143,6 @@ void PictureLoaderWorker::handleRateLimit(QNetworkReply *reply, const QUrl &url,
// Always queue the request even if already rate-limited
requestQueue.append(qMakePair(url, worker));
}
}
}
void PictureLoaderWorker::processQueuedRequests()

View file

@ -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);