🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Refactor cache.HTTPClient
Deluanec68d69d566b
diff --git a/core/agents/lastfm/agent.go b/core/agents/lastfm/agent.goindex ca3045c48..55deab32b 100644--- a/core/agents/lastfm/agent.go+++ b/core/agents/lastfm/agent.go@@ -14,7 +14,7 @@ import ("github.com/navidrome/navidrome/core/scrobbler""github.com/navidrome/navidrome/log""github.com/navidrome/navidrome/model"- "github.com/navidrome/navidrome/utils"+ "github.com/navidrome/navidrome/utils/cache")const (@@ -47,7 +47,7 @@ func lastFMConstructor(ds model.DataStore) *lastfmAgent {hc := &http.Client{Timeout: consts.DefaultHttpClientTimeOut,}- chc := utils.NewCachedHTTPClient(hc, consts.DefaultHttpClientTimeOut)+ chc := cache.NewHTTPClient(hc, consts.DefaultHttpClientTimeOut)l.client = newClient(l.apiKey, l.secret, l.lang, chc)return l}diff --git a/core/agents/listenbrainz/agent.go b/core/agents/listenbrainz/agent.goindex 3e1446a25..f5d39925a 100644--- a/core/agents/listenbrainz/agent.go+++ b/core/agents/listenbrainz/agent.go@@ -11,7 +11,7 @@ import ("github.com/navidrome/navidrome/core/scrobbler""github.com/navidrome/navidrome/log""github.com/navidrome/navidrome/model"- "github.com/navidrome/navidrome/utils"+ "github.com/navidrome/navidrome/utils/cache")const (@@ -35,7 +35,7 @@ func listenBrainzConstructor(ds model.DataStore) *listenBrainzAgent {hc := &http.Client{Timeout: consts.DefaultHttpClientTimeOut,}- chc := utils.NewCachedHTTPClient(hc, consts.DefaultHttpClientTimeOut)+ chc := cache.NewHTTPClient(hc, consts.DefaultHttpClientTimeOut)l.client = newClient(l.baseURL, chc)return l}diff --git a/core/agents/spotify/spotify.go b/core/agents/spotify/spotify.goindex b0b3f39be..869c0ecc8 100644--- a/core/agents/spotify/spotify.go+++ b/core/agents/spotify/spotify.go@@ -13,7 +13,7 @@ import ("github.com/navidrome/navidrome/core/agents""github.com/navidrome/navidrome/log""github.com/navidrome/navidrome/model"- "github.com/navidrome/navidrome/utils"+ "github.com/navidrome/navidrome/utils/cache""github.com/xrash/smetrics")@@ -35,7 +35,7 @@ func spotifyConstructor(ds model.DataStore) agents.Interface {hc := &http.Client{Timeout: consts.DefaultHttpClientTimeOut,}- chc := utils.NewCachedHTTPClient(hc, consts.DefaultHttpClientTimeOut)+ chc := cache.NewHTTPClient(hc, consts.DefaultHttpClientTimeOut)l.client = newClient(l.id, l.secret, chc)return l}diff --git a/utils/cached_http_client.go b/utils/cache/cached_http_client.gosimilarity index 77%rename from utils/cached_http_client.gorename to utils/cache/cached_http_client.goindex 9ba5ae2f6..e10118065 100644--- a/utils/cached_http_client.go+++ b/utils/cache/cached_http_client.go@@ -1,4 +1,4 @@-package utils+package cacheimport ("bufio"@@ -16,7 +16,7 @@ import (const cacheSizeLimit = 100-type CachedHTTPClient struct {+type HTTPClient struct {cache *ttlcache.Cachehc httpDoer}@@ -32,8 +32,8 @@ type requestData struct {Body *string}-func NewCachedHTTPClient(wrapped httpDoer, ttl time.Duration) *CachedHTTPClient {- c := &CachedHTTPClient{hc: wrapped}+func NewHTTPClient(wrapped httpDoer, ttl time.Duration) *HTTPClient {+ c := &HTTPClient{hc: wrapped}c.cache = ttlcache.NewCache()c.cache.SetCacheSizeLimit(cacheSizeLimit)c.cache.SkipTTLExtensionOnHit(true)@@ -55,7 +55,7 @@ func NewCachedHTTPClient(wrapped httpDoer, ttl time.Duration) *CachedHTTPClientreturn c}-func (c *CachedHTTPClient) Do(req *http.Request) (*http.Response, error) {+func (c *HTTPClient) Do(req *http.Request) (*http.Response, error) {key := c.serializeReq(req)respStr, err := c.cache.Get(key)if err != nil {@@ -64,7 +64,7 @@ func (c *CachedHTTPClient) Do(req *http.Request) (*http.Response, error) {return c.deserializeResponse(req, respStr.(string))}-func (c *CachedHTTPClient) serializeReq(req *http.Request) string {+func (c *HTTPClient) serializeReq(req *http.Request) string {data := requestData{Method: req.Method,Header: req.Header,@@ -79,7 +79,7 @@ func (c *CachedHTTPClient) serializeReq(req *http.Request) string {return string(j)}-func (c *CachedHTTPClient) deserializeReq(reqStr string) (*http.Request, error) {+func (c *HTTPClient) deserializeReq(reqStr string) (*http.Request, error) {var data requestData_ = json.Unmarshal([]byte(reqStr), &data)var body io.Reader@@ -95,13 +95,13 @@ func (c *CachedHTTPClient) deserializeReq(reqStr string) (*http.Request, error)return req, nil}-func (c *CachedHTTPClient) serializeResponse(resp *http.Response) string {+func (c *HTTPClient) serializeResponse(resp *http.Response) string {var b = &bytes.Buffer{}_ = resp.Write(b)return b.String()}-func (c *CachedHTTPClient) deserializeResponse(req *http.Request, respStr string) (*http.Response, error) {+func (c *HTTPClient) deserializeResponse(req *http.Request, respStr string) (*http.Response, error) {r := bufio.NewReader(strings.NewReader(respStr))return http.ReadResponse(r, req)}diff --git a/utils/cached_http_client_test.go b/utils/cache/cached_http_client_test.gosimilarity index 90%rename from utils/cached_http_client_test.gorename to utils/cache/cached_http_client_test.goindex 0d00b120a..1ec1a3a27 100644--- a/utils/cached_http_client_test.go+++ b/utils/cache/cached_http_client_test.go@@ -1,4 +1,4 @@-package utils+package cacheimport ("fmt"@@ -12,9 +12,9 @@ import (. "github.com/onsi/gomega")-var _ = Describe("CachedHttpClient", func() {+var _ = Describe("HTTPClient", func() {Context("GET", func() {- var chc *CachedHTTPClient+ var chc *HTTPClientvar ts *httptest.Servervar requestsReceived intvar header string@@ -25,7 +25,7 @@ var _ = Describe("CachedHttpClient", func() {header = r.Header.Get("head")_, _ = fmt.Fprintf(w, "Hello, %s", r.URL.Query()["name"])}))- chc = NewCachedHTTPClient(http.DefaultClient, consts.DefaultHttpClientTimeOut)+ chc = NewHTTPClient(http.DefaultClient, consts.DefaultHttpClientTimeOut)})AfterEach(func() {@@ -73,7 +73,7 @@ var _ = Describe("CachedHttpClient", func() {It("expires responses after TTL", func() {requestsReceived = 0- chc = NewCachedHTTPClient(http.DefaultClient, 10*time.Millisecond)+ chc = NewHTTPClient(http.DefaultClient, 10*time.Millisecond)r, _ := http.NewRequest("GET", ts.URL+"?name=doe", nil)_, err := chc.Do(r)