From 8bdbeeb5a8e8772a73813f42dfd230e6b2940297 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 3 Jan 2026 21:27:23 +0000 Subject: [PATCH] Refactor retry logic to reduce code duplication - Extract retry condition check into ShouldRetryRequest() helper - Extract backoff delay calculation into CalculateBackoffDelay() helper - Use helper methods in both GetRecentTracksRequestFinished and GetTopTracksRequestFinished - Improves code maintainability and consistency Co-authored-by: jonaski <10343810+jonaski@users.noreply.github.com> --- src/scrobbler/lastfmimport.cpp | 45 +++++++++++++++++++--------------- src/scrobbler/lastfmimport.h | 3 +++ 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/scrobbler/lastfmimport.cpp b/src/scrobbler/lastfmimport.cpp index 137c9735b..02e305b01 100644 --- a/src/scrobbler/lastfmimport.cpp +++ b/src/scrobbler/lastfmimport.cpp @@ -49,6 +49,7 @@ #include "lastfmscrobbler.h" using namespace Qt::Literals::StringLiterals; +using namespace ScrobblerSettings; namespace { constexpr int kRequestsDelay = 2000; @@ -253,16 +254,14 @@ void LastFMImport::GetRecentTracksRequestFinished(QNetworkReply *reply, GetRecen const JsonObjectResult json_object_result = ParseJsonObject(reply); if (!json_object_result.success()) { - if (json_object_result.http_status_code == 500 || json_object_result.http_status_code == 503 || json_object_result.network_error == QNetworkReply::TemporaryNetworkFailureError) { - if (request.retry_count < kMaxRetries) { - const int delay_ms = kInitialBackoffMs * (1 << request.retry_count); - qLog(Warning) << "Last.fm request failed with status" << json_object_result.http_status_code << ", retrying in" << delay_ms << "ms (attempt" << (request.retry_count + 1) << "of" << kMaxRetries << ")"; - QTimer::singleShot(delay_ms, this, [this, request]() { - GetRecentTracksRequest retry_request(request.page, request.retry_count + 1); - SendGetRecentTracksRequest(retry_request); - }); - return; - } + if (ShouldRetryRequest(json_object_result) && request.retry_count < kMaxRetries) { + const int delay_ms = CalculateBackoffDelay(request.retry_count); + qLog(Warning) << "Last.fm request failed with status" << json_object_result.http_status_code << ", retrying in" << delay_ms << "ms (attempt" << (request.retry_count + 1) << "of" << kMaxRetries << ")"; + QTimer::singleShot(delay_ms, this, [this, request]() { + GetRecentTracksRequest retry_request(request.page, request.retry_count + 1); + SendGetRecentTracksRequest(retry_request); + }); + return; } Error(json_object_result.error_message); return; @@ -422,16 +421,14 @@ void LastFMImport::GetTopTracksRequestFinished(QNetworkReply *reply, GetTopTrack const JsonObjectResult json_object_result = ParseJsonObject(reply); if (!json_object_result.success()) { - if (json_object_result.http_status_code == 500 || json_object_result.http_status_code == 503 || json_object_result.network_error == QNetworkReply::TemporaryNetworkFailureError) { - if (request.retry_count < kMaxRetries) { - const int delay_ms = kInitialBackoffMs * (1 << request.retry_count); - qLog(Warning) << "Last.fm request failed with status" << json_object_result.http_status_code << ", retrying in" << delay_ms << "ms (attempt" << (request.retry_count + 1) << "of" << kMaxRetries << ")"; - QTimer::singleShot(delay_ms, this, [this, request]() { - GetTopTracksRequest retry_request(request.page, request.retry_count + 1); - SendGetTopTracksRequest(retry_request); - }); - return; - } + if (ShouldRetryRequest(json_object_result) && request.retry_count < kMaxRetries) { + const int delay_ms = CalculateBackoffDelay(request.retry_count); + qLog(Warning) << "Last.fm request failed with status" << json_object_result.http_status_code << ", retrying in" << delay_ms << "ms (attempt" << (request.retry_count + 1) << "of" << kMaxRetries << ")"; + QTimer::singleShot(delay_ms, this, [this, request]() { + GetTopTracksRequest retry_request(request.page, request.retry_count + 1); + SendGetTopTracksRequest(retry_request); + }); + return; } Error(json_object_result.error_message); return; @@ -548,6 +545,14 @@ void LastFMImport::GetTopTracksRequestFinished(QNetworkReply *reply, GetTopTrack } +bool LastFMImport::ShouldRetryRequest(const JsonObjectResult &result) const { + return result.http_status_code == 500 || result.http_status_code == 503 || result.network_error == QNetworkReply::TemporaryNetworkFailureError; +} + +int LastFMImport::CalculateBackoffDelay(const int retry_count) const { + return kInitialBackoffMs * (1 << retry_count); +} + void LastFMImport::UpdateTotalCheck() { Q_EMIT UpdateTotal(lastplayed_total_, playcount_total_); diff --git a/src/scrobbler/lastfmimport.h b/src/scrobbler/lastfmimport.h index ff05e3e1c..bf3c5c074 100644 --- a/src/scrobbler/lastfmimport.h +++ b/src/scrobbler/lastfmimport.h @@ -80,6 +80,9 @@ class LastFMImport : public JsonBaseRequest { void SendGetRecentTracksRequest(GetRecentTracksRequest request); void SendGetTopTracksRequest(GetTopTracksRequest request); + bool ShouldRetryRequest(const JsonObjectResult &result) const; + int CalculateBackoffDelay(const int retry_count) const; + void Error(const QString &error, const QVariant &debug = QVariant()) override; void UpdateTotalCheck();