update
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
This commit is contained in:
110
utils/cache/cached_http_client.go
vendored
Normal file
110
utils/cache/cached_http_client.go
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/navidrome/navidrome/log"
|
||||
)
|
||||
|
||||
const cacheSizeLimit = 100
|
||||
|
||||
type HTTPClient struct {
|
||||
cache SimpleCache[string, string]
|
||||
hc httpDoer
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
type httpDoer interface {
|
||||
Do(req *http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
type requestData struct {
|
||||
Method string
|
||||
Header http.Header
|
||||
URL string
|
||||
Body *string
|
||||
}
|
||||
|
||||
func NewHTTPClient(wrapped httpDoer, ttl time.Duration) *HTTPClient {
|
||||
c := &HTTPClient{hc: wrapped, ttl: ttl}
|
||||
c.cache = NewSimpleCache[string, string](Options{
|
||||
SizeLimit: cacheSizeLimit,
|
||||
DefaultTTL: ttl,
|
||||
})
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *HTTPClient) Do(req *http.Request) (*http.Response, error) {
|
||||
key := c.serializeReq(req)
|
||||
cached := true
|
||||
start := time.Now()
|
||||
respStr, err := c.cache.GetWithLoader(key, func(key string) (string, time.Duration, error) {
|
||||
cached = false
|
||||
req, err := c.deserializeReq(key)
|
||||
if err != nil {
|
||||
log.Trace(req.Context(), "CachedHTTPClient.Do", "key", key, err)
|
||||
return "", 0, err
|
||||
}
|
||||
resp, err := c.hc.Do(req)
|
||||
if err != nil {
|
||||
log.Trace(req.Context(), "CachedHTTPClient.Do", "req", req, err)
|
||||
return "", 0, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return c.serializeResponse(resp), c.ttl, nil
|
||||
})
|
||||
log.Trace(req.Context(), "CachedHTTPClient.Do", "key", key, "cached", cached, "elapsed", time.Since(start), err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.deserializeResponse(req, respStr)
|
||||
}
|
||||
|
||||
func (c *HTTPClient) serializeReq(req *http.Request) string {
|
||||
data := requestData{
|
||||
Method: req.Method,
|
||||
Header: req.Header,
|
||||
URL: req.URL.String(),
|
||||
}
|
||||
if req.Body != nil {
|
||||
bodyData, _ := io.ReadAll(req.Body)
|
||||
bodyStr := base64.StdEncoding.EncodeToString(bodyData)
|
||||
data.Body = &bodyStr
|
||||
}
|
||||
j, _ := json.Marshal(&data)
|
||||
return string(j)
|
||||
}
|
||||
|
||||
func (c *HTTPClient) deserializeReq(reqStr string) (*http.Request, error) {
|
||||
var data requestData
|
||||
_ = json.Unmarshal([]byte(reqStr), &data)
|
||||
var body io.Reader
|
||||
if data.Body != nil {
|
||||
bodyStr, _ := base64.StdEncoding.DecodeString(*data.Body)
|
||||
body = strings.NewReader(string(bodyStr))
|
||||
}
|
||||
req, err := http.NewRequest(data.Method, data.URL, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header = data.Header
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (c *HTTPClient) serializeResponse(resp *http.Response) string {
|
||||
var b = &bytes.Buffer{}
|
||||
_ = resp.Write(b)
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (c *HTTPClient) deserializeResponse(req *http.Request, respStr string) (*http.Response, error) {
|
||||
r := bufio.NewReader(strings.NewReader(respStr))
|
||||
return http.ReadResponse(r, req)
|
||||
}
|
||||
Reference in New Issue
Block a user