refactor(providers): one paced, retrying HTTP client for cover art and tags (#8002)

coverart/httpclient.go and tags/httpclient.go were the same client:
pacing, exponential backoff with jitter, 401/403-as-transient, body
caps. Tags had added a User-Agent and a shared Pacer, cover art an image
GET. providers.HTTPClient now has all of it, with the tests that were in
coverart. ErrTransient is shared too, so both families' errors.Is still
match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 12:58:55 -05:00
co-authored by Claude Opus 5.5
parent 487e11f59f
commit b901560edf
16 changed files with 333 additions and 471 deletions
+10 -8
View File
@@ -8,6 +8,8 @@ import (
"sync/atomic"
"testing"
"time"
"git.fabledsword.com/bvandeusen/minstrel/internal/providers"
)
// newLastfmTestProvider returns a fresh enabled provider configured
@@ -19,7 +21,7 @@ func newLastfmTestProvider(t *testing.T, baseURL, key string) *lastfmProvider {
lastfmBaseURL = baseURL
t.Cleanup(func() { lastfmBaseURL = prevBase })
p := &lastfmProvider{client: newHTTPClient(httpClientOptions{Name: "lastfm", HTTPClient: &http.Client{Timeout: 5 * time.Second}, TreatAuthAsTransient: true, BaseBackoff: time.Millisecond})}
p := &lastfmProvider{client: providers.NewHTTPClient(providers.HTTPOptions{Name: "lastfm", HTTPClient: &http.Client{Timeout: 5 * time.Second}, TreatAuthAsTransient: true, BaseBackoff: time.Millisecond})}
p.enabled.Store(true)
p.apiKey.Store(&key)
return p
@@ -177,7 +179,7 @@ func TestLastfm_FetchArtistArt_ErrorResponse(t *testing.T) {
func TestLastfm_FetchArtistArt_EmptyAPIKey(t *testing.T) {
// Enabled but no key: every call must return ErrNotFound, not a network error.
p := &lastfmProvider{client: newHTTPClient(httpClientOptions{Name: "lastfm", HTTPClient: &http.Client{Timeout: 5 * time.Second}, TreatAuthAsTransient: true, BaseBackoff: time.Millisecond})}
p := &lastfmProvider{client: providers.NewHTTPClient(providers.HTTPOptions{Name: "lastfm", HTTPClient: &http.Client{Timeout: 5 * time.Second}, TreatAuthAsTransient: true, BaseBackoff: time.Millisecond})}
p.enabled.Store(true)
emptyKey := ""
p.apiKey.Store(&emptyKey)
@@ -190,7 +192,7 @@ func TestLastfm_FetchArtistArt_EmptyAPIKey(t *testing.T) {
func TestLastfm_FetchArtistArt_Disabled(t *testing.T) {
// Key set but disabled.
p := &lastfmProvider{client: newHTTPClient(httpClientOptions{Name: "lastfm", HTTPClient: &http.Client{Timeout: 5 * time.Second}, TreatAuthAsTransient: true, BaseBackoff: time.Millisecond})}
p := &lastfmProvider{client: providers.NewHTTPClient(providers.HTTPOptions{Name: "lastfm", HTTPClient: &http.Client{Timeout: 5 * time.Second}, TreatAuthAsTransient: true, BaseBackoff: time.Millisecond})}
// enabled defaults to false
key := "some-key"
p.apiKey.Store(&key)
@@ -203,7 +205,7 @@ func TestLastfm_FetchArtistArt_Disabled(t *testing.T) {
func TestLastfm_FetchArtistArt_EmptyRefs(t *testing.T) {
// Neither MBID nor Name: ErrNotFound without making any HTTP call.
p := &lastfmProvider{client: newHTTPClient(httpClientOptions{Name: "lastfm", HTTPClient: &http.Client{Timeout: 5 * time.Second}, TreatAuthAsTransient: true, BaseBackoff: time.Millisecond})}
p := &lastfmProvider{client: providers.NewHTTPClient(providers.HTTPOptions{Name: "lastfm", HTTPClient: &http.Client{Timeout: 5 * time.Second}, TreatAuthAsTransient: true, BaseBackoff: time.Millisecond})}
p.enabled.Store(true)
key := "test-key"
p.apiKey.Store(&key)
@@ -301,7 +303,7 @@ func TestLastfm_FetchAlbumCover_WithMBIDHint(t *testing.T) {
// ── FetchAlbumCover — missing fields ─────────────────────────────────────────
func TestLastfm_FetchAlbumCover_MissingFields(t *testing.T) {
p := &lastfmProvider{client: newHTTPClient(httpClientOptions{Name: "lastfm", HTTPClient: &http.Client{Timeout: 5 * time.Second}, TreatAuthAsTransient: true, BaseBackoff: time.Millisecond})}
p := &lastfmProvider{client: providers.NewHTTPClient(providers.HTTPOptions{Name: "lastfm", HTTPClient: &http.Client{Timeout: 5 * time.Second}, TreatAuthAsTransient: true, BaseBackoff: time.Millisecond})}
p.enabled.Store(true)
key := "test-key"
p.apiKey.Store(&key)
@@ -320,7 +322,7 @@ func TestLastfm_FetchAlbumCover_MissingFields(t *testing.T) {
}
func TestLastfm_FetchAlbumCover_EmptyAPIKey(t *testing.T) {
p := &lastfmProvider{client: newHTTPClient(httpClientOptions{Name: "lastfm", HTTPClient: &http.Client{Timeout: 5 * time.Second}, TreatAuthAsTransient: true, BaseBackoff: time.Millisecond})}
p := &lastfmProvider{client: providers.NewHTTPClient(providers.HTTPOptions{Name: "lastfm", HTTPClient: &http.Client{Timeout: 5 * time.Second}, TreatAuthAsTransient: true, BaseBackoff: time.Millisecond})}
p.enabled.Store(true)
emptyKey := ""
p.apiKey.Store(&emptyKey)
@@ -332,7 +334,7 @@ func TestLastfm_FetchAlbumCover_EmptyAPIKey(t *testing.T) {
}
func TestLastfm_FetchAlbumCover_Disabled(t *testing.T) {
p := &lastfmProvider{client: newHTTPClient(httpClientOptions{Name: "lastfm", HTTPClient: &http.Client{Timeout: 5 * time.Second}, TreatAuthAsTransient: true, BaseBackoff: time.Millisecond})}
p := &lastfmProvider{client: providers.NewHTTPClient(providers.HTTPOptions{Name: "lastfm", HTTPClient: &http.Client{Timeout: 5 * time.Second}, TreatAuthAsTransient: true, BaseBackoff: time.Millisecond})}
// enabled defaults to false
key := "some-key"
p.apiKey.Store(&key)
@@ -360,7 +362,7 @@ func TestLastfm_RateLimit_Serializes(t *testing.T) {
// 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.
p := &lastfmProvider{client: newHTTPClient(httpClientOptions{
p := &lastfmProvider{client: providers.NewHTTPClient(providers.HTTPOptions{
Name: "lastfm",
HTTPClient: &http.Client{Timeout: 5 * time.Second},
MinInterval: lastfmMinPeriod,