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:
@@ -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.
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package providers
|
||||
|
||||
import "time"
|
||||
|
||||
// LastfmBaseURL is Last.fm's API root, used by both the cover-art and the
|
||||
// tag Last.fm providers.
|
||||
const LastfmBaseURL = "https://ws.audioscrobbler.com/2.0/"
|
||||
|
||||
// LastfmPacer spaces every Last.fm request this server makes, whichever
|
||||
// family asks: 4 a second, under Last.fm's 5 a second per key. Each
|
||||
// family pacing on its own could reach 8 a second when an admin uses one
|
||||
// key for both.
|
||||
var LastfmPacer = NewInterval(250 * time.Millisecond)
|
||||
|
||||
// lastfmTransientCodes are the JSON error codes that mean "try again"
|
||||
// rather than "no data": 8 operation failed, 11 and 16 service temporarily
|
||||
// unavailable, 29 rate limit exceeded.
|
||||
var lastfmTransientCodes = map[int]bool{8: true, 11: true, 16: true, 29: true}
|
||||
|
||||
// LastfmError maps the error field Last.fm returns in a 200 body. 0 is no
|
||||
// error. A transient code is ErrTransient, so the row is retried on a later
|
||||
// pass instead of settling as having nothing from Last.fm; anything else (6
|
||||
// not found, 10 invalid key, …) is ErrNotFound, and the chain moves on.
|
||||
func LastfmError(code int) error {
|
||||
switch {
|
||||
case code == 0:
|
||||
return nil
|
||||
case lastfmTransientCodes[code]:
|
||||
return ErrTransient
|
||||
default:
|
||||
return ErrNotFound
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package providers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLastfmError(t *testing.T) {
|
||||
for code, want := range map[int]error{0: nil, 6: ErrNotFound, 10: ErrNotFound, 8: ErrTransient, 11: ErrTransient, 16: ErrTransient, 29: ErrTransient} {
|
||||
if got := LastfmError(code); !errors.Is(got, want) || (want == nil && got != nil) {
|
||||
t.Errorf("LastfmError(%d) = %v, want %v", code, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user