quintodrome/utils/cache/cached_http_client.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

108 lines
2.4 KiB
Go
Raw Normal View History

2024-05-11 15:36:33 -08:00
package cache
2021-02-07 19:26:05 -09:00
import (
"bufio"
"bytes"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"strings"
"time"
"github.com/jellydator/ttlcache/v2"
2021-02-07 19:26:05 -09:00
"github.com/navidrome/navidrome/log"
)
2021-02-08 12:33:09 -09:00
const cacheSizeLimit = 100
2021-02-07 19:26:05 -09:00
2024-05-11 15:36:33 -08:00
type HTTPClient struct {
2021-02-07 19:26:05 -09:00
cache *ttlcache.Cache
hc httpDoer
}
type httpDoer interface {
Do(req *http.Request) (*http.Response, error)
}
type requestData struct {
Method string
Header http.Header
URL string
Body *string
}
2024-05-11 15:36:33 -08:00
func NewHTTPClient(wrapped httpDoer, ttl time.Duration) *HTTPClient {
c := &HTTPClient{hc: wrapped}
2021-02-07 19:26:05 -09:00
c.cache = ttlcache.NewCache()
c.cache.SetCacheSizeLimit(cacheSizeLimit)
c.cache.SkipTTLExtensionOnHit(true)
c.cache.SetLoaderFunction(func(key string) (interface{}, time.Duration, error) {
req, err := c.deserializeReq(key)
if err != nil {
return nil, 0, err
}
resp, err := c.hc.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
2021-02-07 19:26:05 -09:00
return c.serializeResponse(resp), ttl, nil
})
c.cache.SetNewItemCallback(func(key string, value interface{}) {
log.Trace("New request cached", "req", key, "resp", value)
})
return c
}
2024-05-11 15:36:33 -08:00
func (c *HTTPClient) Do(req *http.Request) (*http.Response, error) {
2021-02-07 19:26:05 -09:00
key := c.serializeReq(req)
respStr, err := c.cache.Get(key)
if err != nil {
return nil, err
}
return c.deserializeResponse(req, respStr.(string))
}
2024-05-11 15:36:33 -08:00
func (c *HTTPClient) serializeReq(req *http.Request) string {
2021-02-07 19:26:05 -09:00
data := requestData{
Method: req.Method,
Header: req.Header,
URL: req.URL.String(),
}
if req.Body != nil {
bodyData, _ := io.ReadAll(req.Body)
2021-02-07 19:26:05 -09:00
bodyStr := base64.StdEncoding.EncodeToString(bodyData)
data.Body = &bodyStr
}
j, _ := json.Marshal(&data)
return string(j)
}
2024-05-11 15:36:33 -08:00
func (c *HTTPClient) deserializeReq(reqStr string) (*http.Request, error) {
2021-02-07 19:26:05 -09:00
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))
}
2021-02-08 06:14:29 -09:00
req, err := http.NewRequest(data.Method, data.URL, body)
if err != nil {
return nil, err
}
req.Header = data.Header
return req, nil
2021-02-07 19:26:05 -09:00
}
2024-05-11 15:36:33 -08:00
func (c *HTTPClient) serializeResponse(resp *http.Response) string {
2021-02-07 19:26:05 -09:00
var b = &bytes.Buffer{}
_ = resp.Write(b)
return b.String()
}
2024-05-11 15:36:33 -08:00
func (c *HTTPClient) deserializeResponse(req *http.Request, respStr string) (*http.Response, error) {
2021-02-07 19:26:05 -09:00
r := bufio.NewReader(strings.NewReader(respStr))
return http.ReadResponse(r, req)
}