mirror of
https://github.com/prymitive/karma
synced 2026-08-19 11:36:24 +00:00
chore(backend): use hashicorp/golang-lru for internal cache
This commit is contained in:
committed by
Łukasz Mierzwa
parent
0fd7b8ee70
commit
31d21f29cf
@@ -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)
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
})
|
||||
|
||||
+3
-4
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
`))
|
||||
|
||||
+1
-1
@@ -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")
|
||||
|
||||
|
||||
+3
-3
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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=
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user