From 31d21f29cf152d7f42ca5acd23ee340e1dec4aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Sun, 25 Apr 2021 18:25:16 +0100 Subject: [PATCH] chore(backend): use hashicorp/golang-lru for internal cache --- cmd/karma/api_test.go | 6 +++--- cmd/karma/auth_headers_test.go | 4 ++-- cmd/karma/autocomplete.go | 5 ++--- cmd/karma/benchmark_test.go | 4 ++-- cmd/karma/main.go | 7 +++---- cmd/karma/proxy_test.go | 13 ++++++------- cmd/karma/timer.go | 2 +- cmd/karma/views.go | 6 +++--- cmd/karma/views_test.go | 10 +++++----- go.mod | 2 +- go.sum | 4 ++-- internal/filters/matcher.go | 7 +++---- 12 files changed, 33 insertions(+), 37 deletions(-) diff --git a/cmd/karma/api_test.go b/cmd/karma/api_test.go index cf51021f3..b9c7c8cba 100644 --- a/cmd/karma/api_test.go +++ b/cmd/karma/api_test.go @@ -991,7 +991,7 @@ func TestVerifyAllGroups(t *testing.T) { for _, version := range mock.ListAllMocks() { t.Logf("Testing API using mock files from Alertmanager %s", version) mockAlerts(version) - apiCache.Flush() + apiCache.Purge() r := testRouter() setupRouter(r) req := httptest.NewRequest("GET", "/alerts.json", nil) @@ -1236,7 +1236,7 @@ func TestSortOrder(t *testing.T) { setupRouter(r) for _, testCase := range sortTests { - apiCache.Flush() + apiCache.Purge() config.Config.Grid.Sorting.Reverse = testCase.defaultSortReverse uri := fmt.Sprintf( "/alerts.json?sortOrder=%s&sortLabel=%s&sortReverse=%s&%s", @@ -1332,7 +1332,7 @@ func TestStripLabels(t *testing.T) { for _, testCase := range testCases { config.Config.Labels.Keep = testCase.keep config.Config.Labels.Strip = testCase.strip - apiCache.Flush() + apiCache.Purge() req := httptest.NewRequest("GET", "/alerts.json", nil) resp := httptest.NewRecorder() r.ServeHTTP(resp, req) diff --git a/cmd/karma/auth_headers_test.go b/cmd/karma/auth_headers_test.go index bae8a3efb..fa6a86a35 100644 --- a/cmd/karma/auth_headers_test.go +++ b/cmd/karma/auth_headers_test.go @@ -6,8 +6,8 @@ import ( "testing" "time" + lru "github.com/hashicorp/golang-lru" "github.com/jarcoal/httpmock" - cache "github.com/patrickmn/go-cache" "github.com/prymitive/karma/internal/alertmanager" "github.com/prymitive/karma/internal/mock" ) @@ -65,7 +65,7 @@ func TestAuthHeader(t *testing.T) { for _, testCase := range authHeaderTests { testCase := testCase //scopelint pin for _, version := range mock.ListAllMocks() { - apiCache = cache.New(cache.NoExpiration, 10*time.Second) + apiCache, _ = lru.New(100) am, err := alertmanager.NewAlertmanager( "cluster", diff --git a/cmd/karma/autocomplete.go b/cmd/karma/autocomplete.go index 83b3b9069..0038d8d55 100644 --- a/cmd/karma/autocomplete.go +++ b/cmd/karma/autocomplete.go @@ -5,7 +5,6 @@ import ( "net/http" "sort" "strings" - "time" "github.com/prymitive/karma/internal/alertmanager" ) @@ -58,7 +57,7 @@ func knownLabelNames(w http.ResponseWriter, r *http.Request) { data, _ = json.Marshal(acData) - apiCache.Set(cacheKey, data, time.Second*15) + _ = apiCache.Add(cacheKey, data) mimeJSON(w) w.WriteHeader(http.StatusOK) @@ -89,7 +88,7 @@ func knownLabelValues(w http.ResponseWriter, r *http.Request) { data, _ = json.Marshal(values) - apiCache.Set(cacheKey, data, time.Second*15) + _ = apiCache.Add(cacheKey, data) mimeJSON(w) w.WriteHeader(http.StatusOK) diff --git a/cmd/karma/benchmark_test.go b/cmd/karma/benchmark_test.go index cdaf4332b..9465b8d45 100644 --- a/cmd/karma/benchmark_test.go +++ b/cmd/karma/benchmark_test.go @@ -138,7 +138,7 @@ func BenchmarkAlertsAPIMisses(b *testing.B) { b.StopTimer() reportMemoryMetrics(b) - apiCache.Flush() + apiCache.Purge() b.StartTimer() } }) @@ -165,7 +165,7 @@ func BenchmarkAlertsAPIMissesAutoGrid(b *testing.B) { b.StopTimer() reportMemoryMetrics(b) - apiCache.Flush() + apiCache.Purge() b.StartTimer() } }) diff --git a/cmd/karma/main.go b/cmd/karma/main.go index 80f3536da..0ea4c546e 100644 --- a/cmd/karma/main.go +++ b/cmd/karma/main.go @@ -31,12 +31,11 @@ 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" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/spf13/pflag" - - cache "github.com/patrickmn/go-cache" ) var ( @@ -49,7 +48,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 *cache.Cache + apiCache *lru.Cache indexTemplate *template.Template @@ -353,7 +352,7 @@ func mainSetup(errorHandling pflag.ErrorHandling) (*chi.Mux, error) { } transform.SetLinkRules(linkDetectRules) - apiCache = cache.New(cache.NoExpiration, 10*time.Second) + apiCache, _ = lru.New(1024) err = setupUpstreams() if err != nil { diff --git a/cmd/karma/proxy_test.go b/cmd/karma/proxy_test.go index 685d224e3..7fcb878b0 100644 --- a/cmd/karma/proxy_test.go +++ b/cmd/karma/proxy_test.go @@ -12,14 +12,13 @@ import ( "testing" "time" - cache "github.com/patrickmn/go-cache" + lru "github.com/hashicorp/golang-lru" + "github.com/jarcoal/httpmock" + "github.com/pmezard/go-difflib/difflib" "github.com/prymitive/karma/internal/alertmanager" "github.com/prymitive/karma/internal/config" "github.com/prymitive/karma/internal/mock" "github.com/rs/zerolog" - - "github.com/jarcoal/httpmock" - "github.com/pmezard/go-difflib/difflib" ) // httptest.NewRecorder() doesn't implement http.CloseNotifier @@ -526,7 +525,7 @@ func TestProxyUserRewrite(t *testing.T) { setupRouter(r) setupRouterProxyHandlers(r, am) - apiCache = cache.New(cache.NoExpiration, 10*time.Second) + apiCache, _ = lru.New(100) httpmock.Reset() mock.RegisterURL("http://localhost/metrics", version, "metrics") mock.RegisterURL("http://localhost/api/v2/status", version, "api/v2/status") @@ -1184,7 +1183,7 @@ func TestProxySilenceACL(t *testing.T) { } setupRouterProxyHandlers(r, am) - apiCache = cache.New(cache.NoExpiration, 10*time.Second) + apiCache, _ = lru.New(100) httpmock.Reset() mock.RegisterURL("http://localhost/metrics", version, "metrics") mock.RegisterURL("http://localhost/api/v2/status", version, "api/v2/status") @@ -1278,7 +1277,7 @@ func TestProxyRequestToUnsupportedAlertmanager(t *testing.T) { } setupRouterProxyHandlers(r, am) - apiCache = cache.New(cache.NoExpiration, 10*time.Second) + apiCache, _ = lru.New(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/timer.go b/cmd/karma/timer.go index d7d432f15..84b04063b 100644 --- a/cmd/karma/timer.go +++ b/cmd/karma/timer.go @@ -11,7 +11,7 @@ import ( func pullFromAlertmanager() { // always flush cache once we're done - defer apiCache.Flush() + defer apiCache.Purge() log.Info().Msg("Pulling latest alerts and silences from Alertmanager") diff --git a/cmd/karma/views.go b/cmd/karma/views.go index dac3cbbd4..00031fe20 100644 --- a/cmd/karma/views.go +++ b/cmd/karma/views.go @@ -459,7 +459,7 @@ func alerts(w http.ResponseWriter, r *http.Request) { data, _ = json.Marshal(resp) compressedData, _ := compressResponse(data.([]byte), nil) - apiCache.Set(cacheKey, compressedData, -1) + _ = apiCache.Add(cacheKey, compressedData) mimeJSON(w) w.WriteHeader(http.StatusOK) @@ -505,7 +505,7 @@ func autocomplete(w http.ResponseWriter, r *http.Request) { sort.Sort(sort.Reverse(acData)) data, _ = json.Marshal(acData) - apiCache.Set(cacheKey, data, time.Second*15) + _ = apiCache.Add(cacheKey, data) mimeJSON(w) w.WriteHeader(http.StatusOK) @@ -630,7 +630,7 @@ func silences(w http.ResponseWriter, r *http.Request) { data, _ = json.Marshal(dedupedSilences) - apiCache.Set(cacheKey, data, time.Second*15) + _ = apiCache.Add(cacheKey, data) mimeJSON(w) w.WriteHeader(http.StatusOK) diff --git a/cmd/karma/views_test.go b/cmd/karma/views_test.go index ba272cf98..f68ba9b8d 100644 --- a/cmd/karma/views_test.go +++ b/cmd/karma/views_test.go @@ -15,6 +15,7 @@ import ( "testing" "time" + lru "github.com/hashicorp/golang-lru" "github.com/prymitive/karma/internal/alertmanager" "github.com/prymitive/karma/internal/config" "github.com/prymitive/karma/internal/mock" @@ -24,7 +25,6 @@ import ( "github.com/go-chi/chi/v5" "github.com/google/go-cmp/cmp" "github.com/jarcoal/httpmock" - cache "github.com/patrickmn/go-cache" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/spf13/pflag" @@ -130,9 +130,9 @@ func TestIndexPrefix(t *testing.T) { func mockCache() { if apiCache == nil { - apiCache = cache.New(cache.NoExpiration, time.Hour) + apiCache, _ = lru.New(100) } else { - apiCache.Flush() + apiCache.Purge() } } @@ -345,7 +345,7 @@ func TestGrids(t *testing.T) { setupRouter(r) // re-run a few times to test the cache for i := 1; i <= 3; i++ { - apiCache.Flush() + apiCache.Purge() req := httptest.NewRequest("GET", "/alerts.json?gridLabel="+testCase.gridLabel+testCase.requestQuery, nil) resp := httptest.NewRecorder() r.ServeHTTP(resp, req) @@ -2294,7 +2294,7 @@ func TestUpstreamStatus(t *testing.T) { config.Config.Authentication.Header.Name = "" config.Config.Authentication.BasicAuth.Users = []config.AuthenticationUser{} - apiCache = cache.New(cache.NoExpiration, 10*time.Second) + apiCache, _ = lru.New(100) alertmanager.UnregisterAll() upstreamSetup = false config.Config.Alertmanager.Servers = testCase.upstreams diff --git a/go.mod b/go.mod index ae088017a..a7f23579b 100644 --- a/go.mod +++ b/go.mod @@ -19,12 +19,12 @@ require ( github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-cmp v0.5.5 github.com/hansrodtang/randomcolor v0.0.0-20160512071917-d27108b3d7a5 + github.com/hashicorp/golang-lru v0.5.4 github.com/jarcoal/httpmock v1.0.8 github.com/knadh/koanf v0.16.0 github.com/mailru/easyjson v0.7.7 // indirect github.com/mitchellh/copystructure v1.1.2 // indirect github.com/mitchellh/mapstructure v1.4.1 - github.com/patrickmn/go-cache v2.1.1-0.20180815053127-5633e0862627+incompatible github.com/pmezard/go-difflib v1.0.0 github.com/prometheus/client_golang v1.10.0 github.com/prometheus/common v0.21.0 diff --git a/go.sum b/go.sum index cc3067fe2..36b913017 100644 --- a/go.sum +++ b/go.sum @@ -325,6 +325,8 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09 github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= 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 v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= +github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= 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= @@ -485,8 +487,6 @@ github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= -github.com/patrickmn/go-cache v2.1.1-0.20180815053127-5633e0862627+incompatible h1:MUIwjEiAMYk8zkXXUQeb5itrXF+HpS2pfxNsA2a7AiY= -github.com/patrickmn/go-cache v2.1.1-0.20180815053127-5633e0862627+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.4.0/go.mod h1:PN7xzY2wHTK0K9p34ErDQMlFxa51Fk0OUruD3k1mMwo= diff --git a/internal/filters/matcher.go b/internal/filters/matcher.go index 8b0a82a1c..65a289b94 100644 --- a/internal/filters/matcher.go +++ b/internal/filters/matcher.go @@ -5,12 +5,11 @@ import ( "fmt" "regexp" "strconv" - "time" - cache "github.com/patrickmn/go-cache" + lru "github.com/hashicorp/golang-lru" ) -var matchCache = cache.New(5*time.Minute, 1*time.Minute) +var matchCache, _ = lru.New(1000) type matcherT interface { setOperator(operator string) @@ -103,7 +102,7 @@ func (matcher *regexpMatcher) Compare(valA, valB interface{}) bool { if err != nil { return false } - matchCache.Set(valB.(string), r, 1*time.Minute) + matchCache.Add(valB.(string), r) } return r.(*regexp.Regexp).MatchString(valA.(string)) }