Skip to content

Commit e15f983

Browse files
aalexandruaalexand
and
aalexand
authored
feat: cluster registry api cache (#202)
* Add redis cache for apiserver list clusters handler * Add missing license header * Add caching tests for both v1 and v2 handlers * fix linting * fix linting * fix go-sec * Fix indent * fix config test * fix config test * fix config test * Add missing env value * Add missing env value * Fix local setup * Updated cached response to include headers * Add missing error test * Do not error out on cache miss * Also don't error out on cache set error --------- Co-authored-by: aalexand <[email protected]>
1 parent f37bf6a commit e15f983

File tree

14 files changed

+671
-133
lines changed

14 files changed

+671
-133
lines changed

cmd/apiserver/apiserver.go

+27-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ governing permissions and limitations under the License.
1313
package main
1414

1515
import (
16-
// docs "github.com/adobe/cluster-registry/pkg/apiserver/docs"
16+
"context"
1717
"github.com/adobe/cluster-registry/pkg/apiserver/docs"
1818
"github.com/adobe/cluster-registry/pkg/apiserver/event"
1919
"github.com/adobe/cluster-registry/pkg/apiserver/web"
@@ -26,7 +26,11 @@ import (
2626
monitoring "github.com/adobe/cluster-registry/pkg/monitoring/apiserver"
2727
"github.com/adobe/cluster-registry/pkg/sqs"
2828
awssqs "github.com/aws/aws-sdk-go/service/sqs"
29+
"github.com/eko/gocache/lib/v4/cache"
30+
"github.com/eko/gocache/lib/v4/store"
31+
redisstore "github.com/eko/gocache/store/redis/v4"
2932
"github.com/labstack/gommon/log"
33+
"github.com/redis/go-redis/v9"
3034
echoSwagger "github.com/swaggo/echo-swagger"
3135
)
3236

@@ -64,10 +68,10 @@ func main() {
6468
AWSRegion: appConfig.SqsAwsRegion,
6569
Endpoint: appConfig.SqsEndpoint,
6670
QueueName: appConfig.SqsQueueName,
67-
BatchSize: 10,
71+
BatchSize: appConfig.SqsBatchSize,
6872
VisibilityTimeout: 120,
69-
WaitSeconds: 5,
70-
RunInterval: 20,
73+
WaitSeconds: appConfig.SqsWaitSeconds,
74+
RunInterval: appConfig.SqsRunInterval,
7175
RunOnce: false,
7276
MaxHandlers: 10,
7377
BusyTimeout: 30,
@@ -78,6 +82,17 @@ func main() {
7882
return
7983
}
8084

85+
redisClient := redis.NewClient(&redis.Options{
86+
Addr: appConfig.ApiCacheRedisHost,
87+
})
88+
cmd := redisClient.Info(context.Background())
89+
if cmd.Err() != nil {
90+
log.Fatalf("Cannot connect to redis: %s", cmd.Err().Error())
91+
return
92+
}
93+
redisStore := redisstore.NewRedis(redisClient)
94+
cacheManager := cache.New[string](redisStore)
95+
8196
handler := event.NewClusterUpdateHandler(db)
8297
q.RegisterHandler(func(msg *awssqs.Message) {
8398
log.Debugf("Received message: %s", *msg.MessageId)
@@ -99,10 +114,15 @@ func main() {
99114
log.Errorf("Failed to delete message: %s", err.Error())
100115
return
101116
}
117+
log.Debugf("Invalidating clusters cache")
118+
err = cacheManager.Invalidate(context.Background(), store.WithInvalidateTags([]string{"clusters"}))
119+
if err != nil {
120+
log.Errorf("Failed to invalidate clusters cache: %s", err.Error())
121+
return
122+
}
102123
})
103124

104125
a := api.NewRouter()
105-
106126
status := api.StatusSessions{
107127
Db: db,
108128
SQS: q,
@@ -118,11 +138,11 @@ func main() {
118138
a.GET("/metrics", web.Metrics())
119139

120140
v1 := a.Group("/api/v1")
121-
hv1 := apiv1.NewHandler(appConfig, db, m)
141+
hv1 := apiv1.NewHandler(appConfig, db, m, cacheManager)
122142
hv1.Register(v1)
123143

124144
v2 := a.Group("/api/v2")
125-
hv2 := apiv2.NewHandler(appConfig, db, m, &k8s.ClientProvider{})
145+
hv2 := apiv2.NewHandler(appConfig, db, m, &k8s.ClientProvider{}, cacheManager)
126146
hv2.Register(v2)
127147

128148
go q.Poll()

go.mod

+9-5
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@ require (
77
github.com/Azure/go-autorest/autorest/azure/auth v0.5.13
88
github.com/aws/aws-sdk-go v1.55.4
99
github.com/coreos/go-oidc/v3 v3.11.0
10+
github.com/eko/gocache/lib/v4 v4.1.6
11+
github.com/eko/gocache/store/redis/v4 v4.2.2
1012
github.com/evanphx/json-patch/v5 v5.9.0
1113
github.com/go-jose/go-jose/v3 v3.0.3
12-
github.com/go-jose/go-jose/v4 v4.0.4
1314
github.com/go-logr/logr v1.4.2
15+
github.com/go-redis/redismock/v9 v9.2.0
1416
github.com/google/uuid v1.6.0
1517
github.com/gusaul/go-dynamock v0.0.0-20210107061312-3e989056e1e6
1618
github.com/jinzhu/gorm v1.9.16
1719
github.com/json-iterator/go v1.1.12
1820
github.com/labstack/echo/v4 v4.12.0
1921
github.com/labstack/gommon v0.4.2
2022
github.com/onsi/ginkgo v1.16.5
21-
github.com/onsi/ginkgo/v2 v2.19.1
2223
github.com/onsi/gomega v1.34.1
2324
github.com/prometheus/client_golang v1.19.1
25+
github.com/redis/go-redis/v9 v9.2.0
2426
github.com/stretchr/testify v1.9.0
2527
github.com/swaggo/echo-swagger v1.4.1
2628
github.com/swaggo/swag v1.16.3
@@ -53,12 +55,13 @@ require (
5355
github.com/Microsoft/hcsshim v0.11.5 // indirect
5456
github.com/beorn7/perks v1.0.1 // indirect
5557
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
56-
github.com/cespare/xxhash/v2 v2.2.0 // indirect
58+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
5759
github.com/containerd/containerd v1.7.18 // indirect
5860
github.com/containerd/errdefs v0.1.0 // indirect
5961
github.com/containerd/log v0.1.0 // indirect
6062
github.com/cpuguy83/dockercfg v0.3.1 // indirect
6163
github.com/davecgh/go-spew v1.1.1 // indirect
64+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
6265
github.com/dimchansky/utfbom v1.1.1 // indirect
6366
github.com/distribution/reference v0.6.0 // indirect
6467
github.com/docker/docker v27.0.3+incompatible // indirect
@@ -69,6 +72,7 @@ require (
6972
github.com/felixge/httpsnoop v1.0.4 // indirect
7073
github.com/fsnotify/fsnotify v1.7.0 // indirect
7174
github.com/ghodss/yaml v1.0.0 // indirect
75+
github.com/go-jose/go-jose/v4 v4.0.4 // indirect
7276
github.com/go-logr/stdr v1.2.2 // indirect
7377
github.com/go-logr/zapr v1.3.0 // indirect
7478
github.com/go-ole/go-ole v1.2.6 // indirect
@@ -82,6 +86,7 @@ require (
8286
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
8387
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
8488
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
89+
github.com/golang/mock v1.6.0 // indirect
8590
github.com/golang/protobuf v1.5.4 // indirect
8691
github.com/google/gnostic-models v0.6.8 // indirect
8792
github.com/google/go-cmp v0.6.0 // indirect
@@ -108,6 +113,7 @@ require (
108113
github.com/morikuni/aec v1.0.0 // indirect
109114
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
110115
github.com/nxadm/tail v1.4.8 // indirect
116+
github.com/onsi/ginkgo/v2 v2.19.1 // indirect
111117
github.com/opencontainers/go-digest v1.0.0 // indirect
112118
github.com/opencontainers/image-spec v1.1.0 // indirect
113119
github.com/pkg/errors v0.9.1 // indirect
@@ -133,7 +139,6 @@ require (
133139
go.uber.org/multierr v1.11.0 // indirect
134140
go.uber.org/zap v1.26.0 // indirect
135141
golang.org/x/crypto v0.25.0 // indirect
136-
golang.org/x/mod v0.19.0 // indirect
137142
golang.org/x/oauth2 v0.21.0 // indirect
138143
golang.org/x/sync v0.7.0 // indirect
139144
golang.org/x/sys v0.22.0 // indirect
@@ -142,7 +147,6 @@ require (
142147
golang.org/x/time v0.5.0 // indirect
143148
golang.org/x/tools v0.23.0 // indirect
144149
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
145-
google.golang.org/appengine v1.6.8 // indirect
146150
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect
147151
google.golang.org/grpc v1.59.0 // indirect
148152
google.golang.org/protobuf v1.34.1 // indirect

0 commit comments

Comments
 (0)