Refactor cache.HTTPClient
This commit is contained in:
@@ -14,7 +14,7 @@ import (
|
|||||||
"github.com/navidrome/navidrome/core/scrobbler"
|
"github.com/navidrome/navidrome/core/scrobbler"
|
||||||
"github.com/navidrome/navidrome/log"
|
"github.com/navidrome/navidrome/log"
|
||||||
"github.com/navidrome/navidrome/model"
|
"github.com/navidrome/navidrome/model"
|
||||||
"github.com/navidrome/navidrome/utils"
|
"github.com/navidrome/navidrome/utils/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -47,7 +47,7 @@ func lastFMConstructor(ds model.DataStore) *lastfmAgent {
|
|||||||
hc := &http.Client{
|
hc := &http.Client{
|
||||||
Timeout: consts.DefaultHttpClientTimeOut,
|
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)
|
l.client = newClient(l.apiKey, l.secret, l.lang, chc)
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/navidrome/navidrome/core/scrobbler"
|
"github.com/navidrome/navidrome/core/scrobbler"
|
||||||
"github.com/navidrome/navidrome/log"
|
"github.com/navidrome/navidrome/log"
|
||||||
"github.com/navidrome/navidrome/model"
|
"github.com/navidrome/navidrome/model"
|
||||||
"github.com/navidrome/navidrome/utils"
|
"github.com/navidrome/navidrome/utils/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -35,7 +35,7 @@ func listenBrainzConstructor(ds model.DataStore) *listenBrainzAgent {
|
|||||||
hc := &http.Client{
|
hc := &http.Client{
|
||||||
Timeout: consts.DefaultHttpClientTimeOut,
|
Timeout: consts.DefaultHttpClientTimeOut,
|
||||||
}
|
}
|
||||||
chc := utils.NewCachedHTTPClient(hc, consts.DefaultHttpClientTimeOut)
|
chc := cache.NewHTTPClient(hc, consts.DefaultHttpClientTimeOut)
|
||||||
l.client = newClient(l.baseURL, chc)
|
l.client = newClient(l.baseURL, chc)
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
"github.com/navidrome/navidrome/core/agents"
|
"github.com/navidrome/navidrome/core/agents"
|
||||||
"github.com/navidrome/navidrome/log"
|
"github.com/navidrome/navidrome/log"
|
||||||
"github.com/navidrome/navidrome/model"
|
"github.com/navidrome/navidrome/model"
|
||||||
"github.com/navidrome/navidrome/utils"
|
"github.com/navidrome/navidrome/utils/cache"
|
||||||
"github.com/xrash/smetrics"
|
"github.com/xrash/smetrics"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ func spotifyConstructor(ds model.DataStore) agents.Interface {
|
|||||||
hc := &http.Client{
|
hc := &http.Client{
|
||||||
Timeout: consts.DefaultHttpClientTimeOut,
|
Timeout: consts.DefaultHttpClientTimeOut,
|
||||||
}
|
}
|
||||||
chc := utils.NewCachedHTTPClient(hc, consts.DefaultHttpClientTimeOut)
|
chc := cache.NewHTTPClient(hc, consts.DefaultHttpClientTimeOut)
|
||||||
l.client = newClient(l.id, l.secret, chc)
|
l.client = newClient(l.id, l.secret, chc)
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package utils
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
@@ -16,7 +16,7 @@ import (
|
|||||||
|
|
||||||
const cacheSizeLimit = 100
|
const cacheSizeLimit = 100
|
||||||
|
|
||||||
type CachedHTTPClient struct {
|
type HTTPClient struct {
|
||||||
cache *ttlcache.Cache
|
cache *ttlcache.Cache
|
||||||
hc httpDoer
|
hc httpDoer
|
||||||
}
|
}
|
||||||
@@ -32,8 +32,8 @@ type requestData struct {
|
|||||||
Body *string
|
Body *string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCachedHTTPClient(wrapped httpDoer, ttl time.Duration) *CachedHTTPClient {
|
func NewHTTPClient(wrapped httpDoer, ttl time.Duration) *HTTPClient {
|
||||||
c := &CachedHTTPClient{hc: wrapped}
|
c := &HTTPClient{hc: wrapped}
|
||||||
c.cache = ttlcache.NewCache()
|
c.cache = ttlcache.NewCache()
|
||||||
c.cache.SetCacheSizeLimit(cacheSizeLimit)
|
c.cache.SetCacheSizeLimit(cacheSizeLimit)
|
||||||
c.cache.SkipTTLExtensionOnHit(true)
|
c.cache.SkipTTLExtensionOnHit(true)
|
||||||
@@ -55,7 +55,7 @@ func NewCachedHTTPClient(wrapped httpDoer, ttl time.Duration) *CachedHTTPClient
|
|||||||
return c
|
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)
|
key := c.serializeReq(req)
|
||||||
respStr, err := c.cache.Get(key)
|
respStr, err := c.cache.Get(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -64,7 +64,7 @@ func (c *CachedHTTPClient) Do(req *http.Request) (*http.Response, error) {
|
|||||||
return c.deserializeResponse(req, respStr.(string))
|
return c.deserializeResponse(req, respStr.(string))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CachedHTTPClient) serializeReq(req *http.Request) string {
|
func (c *HTTPClient) serializeReq(req *http.Request) string {
|
||||||
data := requestData{
|
data := requestData{
|
||||||
Method: req.Method,
|
Method: req.Method,
|
||||||
Header: req.Header,
|
Header: req.Header,
|
||||||
@@ -79,7 +79,7 @@ func (c *CachedHTTPClient) serializeReq(req *http.Request) string {
|
|||||||
return string(j)
|
return string(j)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CachedHTTPClient) deserializeReq(reqStr string) (*http.Request, error) {
|
func (c *HTTPClient) deserializeReq(reqStr string) (*http.Request, error) {
|
||||||
var data requestData
|
var data requestData
|
||||||
_ = json.Unmarshal([]byte(reqStr), &data)
|
_ = json.Unmarshal([]byte(reqStr), &data)
|
||||||
var body io.Reader
|
var body io.Reader
|
||||||
@@ -95,13 +95,13 @@ func (c *CachedHTTPClient) deserializeReq(reqStr string) (*http.Request, error)
|
|||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CachedHTTPClient) serializeResponse(resp *http.Response) string {
|
func (c *HTTPClient) serializeResponse(resp *http.Response) string {
|
||||||
var b = &bytes.Buffer{}
|
var b = &bytes.Buffer{}
|
||||||
_ = resp.Write(b)
|
_ = resp.Write(b)
|
||||||
return b.String()
|
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))
|
r := bufio.NewReader(strings.NewReader(respStr))
|
||||||
return http.ReadResponse(r, req)
|
return http.ReadResponse(r, req)
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package utils
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -12,9 +12,9 @@ import (
|
|||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("CachedHttpClient", func() {
|
var _ = Describe("HTTPClient", func() {
|
||||||
Context("GET", func() {
|
Context("GET", func() {
|
||||||
var chc *CachedHTTPClient
|
var chc *HTTPClient
|
||||||
var ts *httptest.Server
|
var ts *httptest.Server
|
||||||
var requestsReceived int
|
var requestsReceived int
|
||||||
var header string
|
var header string
|
||||||
@@ -25,7 +25,7 @@ var _ = Describe("CachedHttpClient", func() {
|
|||||||
header = r.Header.Get("head")
|
header = r.Header.Get("head")
|
||||||
_, _ = fmt.Fprintf(w, "Hello, %s", r.URL.Query()["name"])
|
_, _ = fmt.Fprintf(w, "Hello, %s", r.URL.Query()["name"])
|
||||||
}))
|
}))
|
||||||
chc = NewCachedHTTPClient(http.DefaultClient, consts.DefaultHttpClientTimeOut)
|
chc = NewHTTPClient(http.DefaultClient, consts.DefaultHttpClientTimeOut)
|
||||||
})
|
})
|
||||||
|
|
||||||
AfterEach(func() {
|
AfterEach(func() {
|
||||||
@@ -73,7 +73,7 @@ var _ = Describe("CachedHttpClient", func() {
|
|||||||
|
|
||||||
It("expires responses after TTL", func() {
|
It("expires responses after TTL", func() {
|
||||||
requestsReceived = 0
|
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)
|
r, _ := http.NewRequest("GET", ts.URL+"?name=doe", nil)
|
||||||
_, err := chc.Do(r)
|
_, err := chc.Do(r)
|
||||||
Reference in New Issue
Block a user