fix(backend): update lru to v2

This commit is contained in:
Łukasz Mierzwa
2022-11-15 12:37:15 +00:00
committed by Łukasz Mierzwa
parent e6d68b847e
commit f29194b534
10 changed files with 38 additions and 35 deletions
+7 -7
View File
@@ -13,7 +13,7 @@ import (
"sync"
"time"
lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
@@ -124,14 +124,14 @@ type knownBadUpstream struct {
type historyPoller struct {
queue chan historyJob
queryTimeout time.Duration
knownBad *lru.Cache
cache *lru.Cache
knownBad *lru.Cache[string, *knownBadUpstream]
cache *lru.Cache[string, *cachedOffsets]
}
func newHistoryPoller(queueSize int, queryTimeout time.Duration) *historyPoller {
log.Debug().Int("queue", queueSize).Dur("timeout", queryTimeout).Msg("Starting history poller")
cache, _ := lru.New(1000)
knownBad, _ := lru.New(100)
cache, _ := lru.New[string, *cachedOffsets](1000)
knownBad, _ := lru.New[string, *knownBadUpstream](100)
return &historyPoller{
queue: make(chan historyJob, queueSize),
queryTimeout: queryTimeout,
@@ -168,7 +168,7 @@ func (hp *historyPoller) cacheSave(key string, values []OffsetSample) {
func (hp *historyPoller) cacheLookup(key string) *cachedOffsets {
if val, found := hp.cache.Get(key); found {
return val.(*cachedOffsets)
return val
}
return nil
}
@@ -179,7 +179,7 @@ func (hp *historyPoller) knownBadSave(key string, kb knownBadUpstream) {
func (hp *historyPoller) knownBadLookup(key string) (*knownBadUpstream, bool) {
if val, found := hp.knownBad.Get(key); found {
return val.(*knownBadUpstream), true
return val, true
}
return nil, false
}
+2 -2
View File
@@ -6,7 +6,7 @@ import (
"testing"
"time"
lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/jarcoal/httpmock"
"github.com/prymitive/karma/internal/alertmanager"
@@ -67,7 +67,7 @@ func TestAuthHeader(t *testing.T) {
for _, testCase := range authHeaderTests {
testCase := testCase // scopelint pin
for _, version := range mock.ListAllMocks() {
apiCache, _ = lru.New(100)
apiCache, _ = lru.New[string, []byte](100)
am, err := alertmanager.NewAlertmanager(
"cluster",
+4 -4
View File
@@ -33,7 +33,7 @@ func knownLabelNames(w http.ResponseWriter, r *http.Request) {
if found {
mimeJSON(w)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data.([]byte))
_, _ = w.Write(data)
return
}
@@ -61,7 +61,7 @@ func knownLabelNames(w http.ResponseWriter, r *http.Request) {
mimeJSON(w)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data.([]byte))
_, _ = w.Write(data)
}
func knownLabelValues(w http.ResponseWriter, r *http.Request) {
@@ -73,7 +73,7 @@ func knownLabelValues(w http.ResponseWriter, r *http.Request) {
if found {
mimeJSON(w)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data.([]byte))
_, _ = w.Write(data)
return
}
@@ -92,5 +92,5 @@ func knownLabelValues(w http.ResponseWriter, r *http.Request) {
mimeJSON(w)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data.([]byte))
_, _ = w.Write(data)
}
+3 -3
View File
@@ -28,7 +28,7 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/klauspost/compress/flate"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
@@ -47,7 +47,7 @@ var (
// apiCache will be used to keep short lived copy of JSON reponses generated for the UI
// If there are requests with the same filter we should respond from cache
// rather than do all the filtering every time
apiCache *lru.Cache
apiCache *lru.Cache[string, []byte]
indexTemplate *template.Template
@@ -386,7 +386,7 @@ func mainSetup(errorHandling pflag.ErrorHandling) (*chi.Mux, *historyPoller, err
}
transform.SetLinkRules(linkDetectRules)
apiCache, _ = lru.New(1024)
apiCache, _ = lru.New[string, []byte](1024)
err = setupUpstreams()
if err != nil {
+4 -4
View File
@@ -12,7 +12,7 @@ import (
"testing"
"time"
lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/jarcoal/httpmock"
"github.com/pmezard/go-difflib/difflib"
"github.com/rs/zerolog"
@@ -560,7 +560,7 @@ func TestProxyUserRewrite(t *testing.T) {
setupRouter(r, nil)
setupRouterProxyHandlers(r, am)
apiCache, _ = lru.New(100)
apiCache, _ = lru.New[string, []byte](100)
httpmock.Reset()
mock.RegisterURL("http://localhost/metrics", version, "metrics")
mock.RegisterURL("http://localhost/api/v2/status", version, "api/v2/status")
@@ -1334,7 +1334,7 @@ func TestProxySilenceACL(t *testing.T) {
}
setupRouterProxyHandlers(r, am)
apiCache, _ = lru.New(100)
apiCache, _ = lru.New[string, []byte](100)
httpmock.Reset()
mock.RegisterURL("http://localhost/metrics", version, "metrics")
mock.RegisterURL("http://localhost/api/v2/status", version, "api/v2/status")
@@ -1428,7 +1428,7 @@ func TestProxyRequestToUnsupportedAlertmanager(t *testing.T) {
}
setupRouterProxyHandlers(r, am)
apiCache, _ = lru.New(100)
apiCache, _ = lru.New[string, []byte](100)
httpmock.Reset()
httpmock.RegisterResponder("GET", "http://localhost/metrics", httpmock.NewStringResponder(200, `alertmanager_build_info{version="0.1.0"} 1
`))
+9 -9
View File
@@ -225,7 +225,7 @@ func alerts(w http.ResponseWriter, r *http.Request) {
data, found := apiCache.Get(cacheKey)
if found {
r := bytes.NewReader(data.([]byte))
r := bytes.NewReader(data)
rawData, _ := decompressCachedResponse(r)
// need to overwrite settings as they can have user specific data
newResp := models.AlertsResponse{}
@@ -505,12 +505,12 @@ func alerts(w http.ResponseWriter, r *http.Request) {
resp.Receivers = receivers
data, _ = json.Marshal(resp)
compressedData, _ := compressResponse(data.([]byte), nil)
compressedData, _ := compressResponse(data, nil)
_ = apiCache.Add(cacheKey, compressedData)
mimeJSON(w)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data.([]byte))
_, _ = w.Write(data)
}
func labelsSettings(grids []models.APIGrid, store models.LabelsSettings) {
@@ -563,7 +563,7 @@ func autocomplete(w http.ResponseWriter, r *http.Request) {
if found {
mimeJSON(w)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data.([]byte))
_, _ = w.Write(data)
return
}
@@ -596,7 +596,7 @@ func autocomplete(w http.ResponseWriter, r *http.Request) {
mimeJSON(w)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data.([]byte))
_, _ = w.Write(data)
}
func silences(w http.ResponseWriter, r *http.Request) {
@@ -608,7 +608,7 @@ func silences(w http.ResponseWriter, r *http.Request) {
if found {
mimeJSON(w)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data.([]byte))
_, _ = w.Write(data)
return
}
@@ -721,7 +721,7 @@ func silences(w http.ResponseWriter, r *http.Request) {
mimeJSON(w)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(data.([]byte))
_, _ = w.Write(data)
}
type AlertList struct {
@@ -736,7 +736,7 @@ func alertList(w http.ResponseWriter, r *http.Request) {
d, found := apiCache.Get(cacheKey)
if found {
r := bytes.NewReader(d.([]byte))
r := bytes.NewReader(d)
rawData, _ := decompressCachedResponse(r)
mimeJSON(w)
w.WriteHeader(http.StatusOK)
@@ -821,7 +821,7 @@ func counters(w http.ResponseWriter, r *http.Request) {
d, found := apiCache.Get(cacheKey)
if found {
r := bytes.NewReader(d.([]byte))
r := bytes.NewReader(d)
rawData, _ := decompressCachedResponse(r)
mimeJSON(w)
w.WriteHeader(http.StatusOK)
+3 -3
View File
@@ -17,7 +17,7 @@ import (
"time"
"github.com/beme/abide"
lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/prymitive/karma/internal/alertmanager"
"github.com/prymitive/karma/internal/config"
@@ -224,7 +224,7 @@ func TestIndex(t *testing.T) {
func mockCache() {
if apiCache == nil {
apiCache, _ = lru.New(100)
apiCache, _ = lru.New[string, []byte](100)
} else {
apiCache.Purge()
}
@@ -2718,7 +2718,7 @@ func TestUpstreamStatus(t *testing.T) {
config.Config.Authentication.Header.Name = ""
config.Config.Authentication.BasicAuth.Users = []config.AuthenticationUser{}
apiCache, _ = lru.New(100)
apiCache, _ = lru.New[string, []byte](100)
alertmanager.UnregisterAll()
upstreamSetup = false
config.Config.Alertmanager.Servers = testCase.upstreams
+1
View File
@@ -15,6 +15,7 @@ require (
github.com/go-openapi/swag v0.22.3
github.com/go-openapi/validate v0.22.0
github.com/google/go-cmp v0.5.9
github.com/hashicorp/golang-lru/v2 v2.0.1
github.com/jarcoal/httpmock v1.2.0
github.com/klauspost/compress v1.15.12
github.com/knadh/koanf v1.4.4
+2
View File
@@ -271,6 +271,8 @@ github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b
github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru/v2 v2.0.1 h1:5pv5N1lT1fjLg2VQ5KWc7kmucp2x/kvFOnxuVTqZ6x4=
github.com/hashicorp/golang-lru/v2 v2.0.1/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
+3 -3
View File
@@ -6,10 +6,10 @@ import (
"regexp"
"strconv"
lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
)
var matchCache, _ = lru.New(1000)
var matchCache, _ = lru.New[string, *regexp.Regexp](1000)
type matcherT interface {
setOperator(operator string)
@@ -104,7 +104,7 @@ func (matcher *regexpMatcher) Compare(valA, valB any) bool {
}
matchCache.Add(valB.(string), r)
}
return r.(*regexp.Regexp).MatchString(valA.(string))
return r.MatchString(valA.(string))
}
type negativeRegexMatcher struct {