From f29194b53484011e609cbf3ef4ce96d67ce18578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Tue, 15 Nov 2022 12:15:43 +0000 Subject: [PATCH] fix(backend): update lru to v2 --- cmd/karma/alert_history.go | 14 +++++++------- cmd/karma/auth_headers_test.go | 4 ++-- cmd/karma/autocomplete.go | 8 ++++---- cmd/karma/main.go | 6 +++--- cmd/karma/proxy_test.go | 8 ++++---- cmd/karma/views.go | 18 +++++++++--------- cmd/karma/views_test.go | 6 +++--- go.mod | 1 + go.sum | 2 ++ internal/filters/matcher.go | 6 +++--- 10 files changed, 38 insertions(+), 35 deletions(-) diff --git a/cmd/karma/alert_history.go b/cmd/karma/alert_history.go index 5ad3caa59..f699a0308 100644 --- a/cmd/karma/alert_history.go +++ b/cmd/karma/alert_history.go @@ -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 } diff --git a/cmd/karma/auth_headers_test.go b/cmd/karma/auth_headers_test.go index abcd0d080..cc3c97747 100644 --- a/cmd/karma/auth_headers_test.go +++ b/cmd/karma/auth_headers_test.go @@ -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", diff --git a/cmd/karma/autocomplete.go b/cmd/karma/autocomplete.go index 0038d8d55..042dae12e 100644 --- a/cmd/karma/autocomplete.go +++ b/cmd/karma/autocomplete.go @@ -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) } diff --git a/cmd/karma/main.go b/cmd/karma/main.go index 3cb206954..18f3f8f57 100644 --- a/cmd/karma/main.go +++ b/cmd/karma/main.go @@ -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 { diff --git a/cmd/karma/proxy_test.go b/cmd/karma/proxy_test.go index a7de62f04..0272151bd 100644 --- a/cmd/karma/proxy_test.go +++ b/cmd/karma/proxy_test.go @@ -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 `)) diff --git a/cmd/karma/views.go b/cmd/karma/views.go index 660382397..ff73e5689 100644 --- a/cmd/karma/views.go +++ b/cmd/karma/views.go @@ -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) diff --git a/cmd/karma/views_test.go b/cmd/karma/views_test.go index cb1ea8604..74e8dbf51 100644 --- a/cmd/karma/views_test.go +++ b/cmd/karma/views_test.go @@ -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 diff --git a/go.mod b/go.mod index df140cda5..a87d28101 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 1549fa91a..9d404b4f3 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/filters/matcher.go b/internal/filters/matcher.go index 8f9add950..381f60c89 100644 --- a/internal/filters/matcher.go +++ b/internal/filters/matcher.go @@ -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 {