fix(providers): Last.fm's rate limit retries art later, and both Last.fm sources share one pace (#8002)

The cover-art Last.fm provider read every JSON error code as "no art",
so a burst of rate-limit (29) or service-unavailable (8, 11, 16)
answers marked albums and artists as having nothing on Last.fm. The tag
provider already retried those. providers.LastfmError is now the one
mapping, used by both.

Each family also paced Last.fm on its own at 4 requests a second, so
together they could reach 8 a second against the 5-per-key limit when
an admin uses one key for both. Both now wait on providers.LastfmPacer.
The client's own spacing is a providers.Interval, so a pace can be
shared.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 13:00:51 -05:00
co-authored by Claude Opus 5.5
parent b901560edf
commit d8abc457e7
6 changed files with 125 additions and 57 deletions
+11 -8
View File
@@ -12,13 +12,10 @@ import (
"git.fabledsword.com/bvandeusen/minstrel/internal/providers"
)
// lastfmBaseURL is a var (not const) so tests can override it.
var lastfmBaseURL = "https://ws.audioscrobbler.com/2.0/"
// lastfmBaseURL is a var (not const) so tests can point it at a test server.
var lastfmBaseURL = providers.LastfmBaseURL
const (
// lastfmMinPeriod throttles to 4 req/s under their 5/s per-key ceiling.
lastfmMinPeriod = 250 * time.Millisecond
lastfmMaxRetries = 3
)
@@ -51,7 +48,7 @@ func init() {
client: providers.NewHTTPClient(providers.HTTPOptions{
Name: "lastfm",
HTTPClient: &http.Client{Timeout: 30 * time.Second},
MinInterval: lastfmMinPeriod,
Pacer: providers.LastfmPacer,
MaxRetries: lastfmMaxRetries,
TreatAuthAsTransient: true,
}),
@@ -116,7 +113,10 @@ func (p *lastfmProvider) FetchArtistArt(ctx context.Context, ref ArtistRef) (thu
if err := p.client.GetJSON(ctx, lastfmBaseURL+"?"+q.Encode(), &resp); err != nil {
return nil, nil, err
}
if resp.Error != 0 || resp.Artist.Name == "" {
if err := providers.LastfmError(resp.Error); err != nil {
return nil, nil, err
}
if resp.Artist.Name == "" {
return nil, nil, ErrNotFound
}
imgURL := pickLargestImage(resp.Artist.Image)
@@ -162,7 +162,10 @@ func (p *lastfmProvider) FetchAlbumCover(ctx context.Context, ref AlbumRef) ([]b
if err := p.client.GetJSON(ctx, lastfmBaseURL+"?"+q.Encode(), &resp); err != nil {
return nil, err
}
if resp.Error != 0 || resp.Album.Name == "" {
if err := providers.LastfmError(resp.Error); err != nil {
return nil, err
}
if resp.Album.Name == "" {
return nil, ErrNotFound
}
imgURL := pickLargestImage(resp.Album.Image)
+22 -6
View File
@@ -347,6 +347,9 @@ func TestLastfm_FetchAlbumCover_Disabled(t *testing.T) {
// ── Rate-limit serialization ─────────────────────────────────────────────────
// lastfmTestPeriod is providers.LastfmPacer's spacing.
const lastfmTestPeriod = 250 * time.Millisecond
func TestLastfm_RateLimit_Serializes(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
@@ -359,13 +362,13 @@ func TestLastfm_RateLimit_Serializes(t *testing.T) {
lastfmBaseURL = srv.URL + "/"
t.Cleanup(func() { lastfmBaseURL = prevBase })
// Build a provider WITH the production MinInterval so this test can
// verify the rate-limit actually serializes. Other tests use
// newLastfmTestProvider which leaves MinInterval=0 for speed.
// Build a provider WITH a fresh production-rate pacer so this test can
// verify the pacing actually serializes. Other tests use
// newLastfmTestProvider, which doesn't pace, for speed.
p := &lastfmProvider{client: providers.NewHTTPClient(providers.HTTPOptions{
Name: "lastfm",
HTTPClient: &http.Client{Timeout: 5 * time.Second},
MinInterval: lastfmMinPeriod,
Pacer: providers.NewInterval(lastfmTestPeriod),
TreatAuthAsTransient: true,
BaseBackoff: time.Millisecond,
})}
@@ -378,9 +381,9 @@ func TestLastfm_RateLimit_Serializes(t *testing.T) {
_, _, _ = p.FetchArtistArt(context.Background(), ArtistRef{Name: "B"})
elapsed := time.Since(start)
if elapsed < lastfmMinPeriod {
if elapsed < lastfmTestPeriod {
t.Errorf("two back-to-back calls completed in %v, want at least %v (rate-limit didn't serialize)",
elapsed, lastfmMinPeriod)
elapsed, lastfmTestPeriod)
}
}
@@ -516,3 +519,16 @@ func TestPickLargestImage(t *testing.T) {
})
}
}
// A rate-limit answer in a 200 body is retried later, not read as "no art".
func TestLastfm_RateLimitCodeIsTransient(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"error":29,"message":"Rate limit exceeded"}`))
}))
defer srv.Close()
p := newLastfmTestProvider(t, srv.URL+"/", "test-key")
if _, err := p.FetchAlbumCover(context.Background(), AlbumRef{ArtistName: "A", AlbumTitle: "B"}); !errors.Is(err, ErrTransient) {
t.Fatalf("err = %v, want ErrTransient", err)
}
}