Skip to content

Fix Last.fm import failures for large libraries with custom API keys and retry logic#1943

Closed
Copilot wants to merge 5 commits intomasterfrom
copilot/fix-lastfm-import-large-libraries
Closed

Fix Last.fm import failures for large libraries with custom API keys and retry logic#1943
Copilot wants to merge 5 commits intomasterfrom
copilot/fix-lastfm-import-large-libraries

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 3, 2026

Last.fm import fails with HTTP 500 errors when users have large libraries (100k+ songs) due to rate limiting on the shared application API key.

Changes

Custom API Key Support

  • Added optional API key field in Last.fm settings UI
  • LastFMImport uses custom key if provided, falls back to default
  • Key stored in settings under LastFM/api_key
// In CreateRequest()
const QString api_key = !api_key_.isEmpty() ? api_key_ : QLatin1String(LastFMScrobbler::kApiKey);
ParamList params = ParamList() << Param(u"api_key"_s, api_key) << ...;

Exponential Backoff Retry

  • Retry HTTP 500/503 errors up to 5 times with exponential backoff (5s, 10s, 20s, 40s, 80s)
  • Request structures track retry_count
  • Helper methods: ShouldRetryRequest(), CalculateBackoffDelay(), LogRetryAttempt()
  • Integer overflow protection in bit shift calculations
if (ShouldRetryRequest(result) && request.retry_count < kMaxRetries) {
  const int delay_ms = CalculateBackoffDelay(request.retry_count);
  QTimer::singleShot(delay_ms, this, [this, request]() {
    SendGetRecentTracksRequest(GetRecentTracksRequest(request.page, request.retry_count + 1));
  });
}

UI Changes

New field in Last.fm settings section with help text linking to API key creation page.

Original prompt

This section details on the original issue you should resolve

<issue_title>Last.fm Import does not work for large libraries</issue_title>
<issue_description>Image

https://github.com/strawberrymusicplayer/strawberry/blob/afc3effc9d6bcfec83f60e9b90bd55dbac14c672/src/scrobbler/lastfmscrobbler.cpp#L69C13-L69C28

A possible solution would be to allow the user to specify a last.fm api key, which they own, either manually (via an environment variable, config file or passed in argument) or via the ui.

Additionally, some more resilience (backoff logic) would allow large libraries to eventually successfully be fetched even with the shared application api key, even if it would be slower.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI self-assigned this Jan 3, 2026
Copilot AI and others added 4 commits January 3, 2026 21:24
- Add API key field to Last.fm settings UI with helpful info text
- Store and load custom API key from settings
- Use custom API key in lastfmimport if provided, fall back to default
- Implement exponential backoff retry logic (up to 5 retries)
- Retry on HTTP 500/503 errors with increasing delays (5s, 10s, 20s, 40s, 80s)
- Add retry count tracking to request structures

Co-authored-by: jonaski <10343810+jonaski@users.noreply.github.com>
- 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>
- Add named constants for retry-eligible HTTP status codes (500, 503)
- Add bounds checking in backoff calculation to prevent integer overflow
- Use kMaxBackoffShift constant to limit bit shift operations
- Improves code safety and readability

Co-authored-by: jonaski <10343810+jonaski@users.noreply.github.com>
- Add LogRetryAttempt() helper method for consistent logging
- Fix formatting in ShouldRetryRequest() for better readability
- Use helper method in both GetRecentTracksRequestFinished and GetTopTracksRequestFinished
- Eliminates duplicate logging code

Co-authored-by: jonaski <10343810+jonaski@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix Last.fm import issue for large libraries Fix Last.fm import failures for large libraries with custom API keys and retry logic Jan 3, 2026
Copilot AI requested a review from jonaski January 3, 2026 21:32
@jonaski jonaski closed this May 3, 2026
@jonaski jonaski deleted the copilot/fix-lastfm-import-large-libraries branch May 3, 2026 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Last.fm Import does not work for large libraries

2 participants