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
+40 -26
View File
@@ -59,10 +59,8 @@ type HTTPOptions struct {
// upstream. Each provider owns one, configured with its own pacing and
// auth semantics.
type HTTPClient struct {
opts HTTPOptions
mu sync.Mutex
lastCall time.Time
opts HTTPOptions
interval *Interval
}
// NewHTTPClient fills opts' defaults and returns a client.
@@ -82,7 +80,42 @@ func NewHTTPClient(opts HTTPOptions) *HTTPClient {
if opts.ImageBodyLimit == 0 {
opts.ImageBodyLimit = 5 * 1024 * 1024
}
return &HTTPClient{opts: opts}
return &HTTPClient{opts: opts, interval: NewInterval(opts.MinInterval)}
}
// Interval spaces calls at least every apart. Share one between clients
// that spend the same upstream budget. A zero interval never waits.
type Interval struct {
every time.Duration
mu sync.Mutex
lastCall time.Time
}
// NewInterval returns an Interval spacing calls every apart.
func NewInterval(every time.Duration) *Interval { return &Interval{every: every} }
// Wait blocks until the interval has passed since the last call, or ctx
// ends, then claims the slot.
func (iv *Interval) Wait(ctx context.Context) error {
if iv.every <= 0 {
return nil
}
iv.mu.Lock()
wait := time.Until(iv.lastCall.Add(iv.every))
if wait > 0 {
timer := time.NewTimer(wait)
iv.mu.Unlock()
select {
case <-ctx.Done():
timer.Stop()
return ctx.Err()
case <-timer.C:
}
iv.mu.Lock()
}
iv.lastCall = time.Now()
iv.mu.Unlock()
return nil
}
// GetJSON sends GET fullURL and decodes the 200 body into out. It retries
@@ -194,31 +227,12 @@ func (c *HTTPClient) do(ctx context.Context, fullURL, accept string) (*http.Resp
return resp, nil
}
// pace waits on the Pacer, or until MinInterval has passed since the last
// request, then claims the slot.
// pace waits on the Pacer, or on the client's own interval.
func (c *HTTPClient) pace(ctx context.Context) error {
if c.opts.Pacer != nil {
return c.opts.Pacer.Wait(ctx)
}
if c.opts.MinInterval <= 0 {
return nil
}
c.mu.Lock()
wait := time.Until(c.lastCall.Add(c.opts.MinInterval))
if wait > 0 {
timer := time.NewTimer(wait)
c.mu.Unlock()
select {
case <-ctx.Done():
timer.Stop()
return ctx.Err()
case <-timer.C:
}
c.mu.Lock()
}
c.lastCall = time.Now()
c.mu.Unlock()
return nil
return c.interval.Wait(ctx)
}
// backoff sleeps 2^attempt × BaseBackoff ± 20%, or until ctx is done.