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:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user