Skip to content

Commit

Permalink
Refactor cache.HTTPClient
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed May 11, 2024
1 parent 955a9b4 commit ec68d69
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions core/agents/lastfm/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions core/agents/listenbrainz/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions core/agents/spotify/spotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package cache

import (
"bufio"
Expand All @@ -16,7 +16,7 @@ import (

const cacheSizeLimit = 100

type CachedHTTPClient struct {
type HTTPClient struct {
cache *ttlcache.Cache
hc httpDoer
}
Expand All @@ -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)
Expand All @@ -55,7 +55,7 @@ func NewCachedHTTPClient(wrapped httpDoer, ttl time.Duration) *CachedHTTPClient
return 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 {
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package cache

import (
"fmt"
Expand All @@ -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 *HTTPClient
var ts *httptest.Server
var requestsReceived int
var header string
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ec68d69

Please sign in to comment.