Skip to content

Commit f081435

Browse files
[management] add log when using redis cache (#3562)
1 parent b62a1b5 commit f081435

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

management/server/account.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func BuildManager(
198198
log.WithContext(ctx).Infof("single account mode disabled, accounts number %d", accountsCounter)
199199
}
200200

201-
cacheStore, err := nbcache.NewStore(nbcache.DefaultIDPCacheExpirationMax, nbcache.DefaultIDPCacheCleanupInterval)
201+
cacheStore, err := nbcache.NewStore(ctx, nbcache.DefaultIDPCacheExpirationMax, nbcache.DefaultIDPCacheCleanupInterval)
202202
if err != nil {
203203
return nil, fmt.Errorf("getting cache store: %s", err)
204204
}

management/server/cache/idp_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestNewIDPCacheManagers(t *testing.T) {
4444

4545
t.Setenv(cache.RedisStoreEnvVar, redisURL)
4646
}
47-
cacheStore, err := cache.NewStore(cache.DefaultIDPCacheExpirationMax, cache.DefaultIDPCacheCleanupInterval)
47+
cacheStore, err := cache.NewStore(context.Background(), cache.DefaultIDPCacheExpirationMax, cache.DefaultIDPCacheCleanupInterval)
4848
if err != nil {
4949
t.Fatalf("couldn't create cache store: %s", err)
5050
}

management/server/cache/store.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
redis_store "github.com/eko/gocache/store/redis/v4"
1212
gocache "github.com/patrickmn/go-cache"
1313
"github.com/redis/go-redis/v9"
14+
log "github.com/sirupsen/logrus"
1415
)
1516

1617
// RedisStoreEnvVar is the environment variable that determines if a redis store should be used.
@@ -19,16 +20,16 @@ const RedisStoreEnvVar = "NB_IDP_CACHE_REDIS_ADDRESS"
1920

2021
// NewStore creates a new cache store with the given max timeout and cleanup interval. It checks for the environment Variable RedisStoreEnvVar
2122
// to determine if a redis store should be used. If the environment variable is set, it will attempt to connect to the redis store.
22-
func NewStore(maxTimeout, cleanupInterval time.Duration) (store.StoreInterface, error) {
23+
func NewStore(ctx context.Context, maxTimeout, cleanupInterval time.Duration) (store.StoreInterface, error) {
2324
redisAddr := os.Getenv(RedisStoreEnvVar)
2425
if redisAddr != "" {
25-
return getRedisStore(redisAddr)
26+
return getRedisStore(ctx, redisAddr)
2627
}
2728
goc := gocache.New(maxTimeout, cleanupInterval)
2829
return gocache_store.NewGoCache(goc), nil
2930
}
3031

31-
func getRedisStore(redisEnvAddr string) (store.StoreInterface, error) {
32+
func getRedisStore(ctx context.Context, redisEnvAddr string) (store.StoreInterface, error) {
3233
options, err := redis.ParseURL(redisEnvAddr)
3334
if err != nil {
3435
return nil, fmt.Errorf("parsing redis cache url: %s", err)
@@ -38,13 +39,15 @@ func getRedisStore(redisEnvAddr string) (store.StoreInterface, error) {
3839
options.MinIdleConns = 3
3940
options.MaxActiveConns = 100
4041
redisClient := redis.NewClient(options)
41-
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
42+
subCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
4243
defer cancel()
4344

44-
_, err = redisClient.Ping(ctx).Result()
45+
_, err = redisClient.Ping(subCtx).Result()
4546
if err != nil {
4647
return nil, err
4748
}
4849

50+
log.WithContext(subCtx).Infof("using redis cache at %s", redisEnvAddr)
51+
4952
return redis_store.NewRedis(redisClient), nil
5053
}

management/server/cache/store_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
func TestMemoryStore(t *testing.T) {
18-
memStore, err := cache.NewStore(100*time.Millisecond, 300*time.Millisecond)
18+
memStore, err := cache.NewStore(context.Background(), 100*time.Millisecond, 300*time.Millisecond)
1919
if err != nil {
2020
t.Fatalf("couldn't create memory store: %s", err)
2121
}
@@ -42,7 +42,7 @@ func TestMemoryStore(t *testing.T) {
4242

4343
func TestRedisStoreConnectionFailure(t *testing.T) {
4444
t.Setenv(cache.RedisStoreEnvVar, "redis://127.0.0.1:6379")
45-
_, err := cache.NewStore(10*time.Millisecond, 30*time.Millisecond)
45+
_, err := cache.NewStore(context.Background(), 10*time.Millisecond, 30*time.Millisecond)
4646
if err == nil {
4747
t.Fatal("getting redis cache store should return error")
4848
}
@@ -65,7 +65,7 @@ func TestRedisStoreConnectionSuccess(t *testing.T) {
6565
}
6666

6767
t.Setenv(cache.RedisStoreEnvVar, redisURL)
68-
redisStore, err := cache.NewStore(100*time.Millisecond, 300*time.Millisecond)
68+
redisStore, err := cache.NewStore(context.Background(), 100*time.Millisecond, 300*time.Millisecond)
6969
if err != nil {
7070
t.Fatalf("couldn't create redis store: %s", err)
7171
}

management/server/user_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ func TestUser_InviteNewUser(t *testing.T) {
516516
cacheLoading: map[string]chan struct{}{},
517517
}
518518

519-
cs, err := nbcache.NewStore(nbcache.DefaultIDPCacheExpirationMax, nbcache.DefaultIDPCacheCleanupInterval)
519+
cs, err := nbcache.NewStore(context.Background(), nbcache.DefaultIDPCacheExpirationMax, nbcache.DefaultIDPCacheCleanupInterval)
520520
require.NoError(t, err)
521521

522522
am.cacheManager = nbcache.NewAccountUserDataCache(am.loadAccount, cs)
@@ -1094,7 +1094,7 @@ func TestDefaultAccountManager_ExternalCache(t *testing.T) {
10941094
cacheLoading: map[string]chan struct{}{},
10951095
}
10961096

1097-
cacheStore, err := nbcache.NewStore(nbcache.DefaultIDPCacheExpirationMax, nbcache.DefaultIDPCacheCleanupInterval)
1097+
cacheStore, err := nbcache.NewStore(context.Background(), nbcache.DefaultIDPCacheExpirationMax, nbcache.DefaultIDPCacheCleanupInterval)
10981098
assert.NoError(t, err)
10991099
am.externalCacheManager = nbcache.NewUserDataCache(cacheStore)
11001100
am.cacheManager = nbcache.NewAccountUserDataCache(am.loadAccount, cacheStore)

0 commit comments

Comments
 (0)