mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-10 07:29:06 +02:00
[management] Reuse a single cache store across all management server consumers (#5889)
* Add support for legacy IDP cache environment variable * Centralize cache store creation to reuse a single Redis connection pool Each cache consumer (IDP cache, token store, PKCE store, secrets manager, EDR validator) was independently calling NewStore, creating separate Redis clients with their own connection pools — up to 1400 potential connections from a single management server process. Introduce a shared CacheStore() singleton on BaseServer that creates one store at boot and injects it into all consumers. Consumer constructors now receive a store.StoreInterface instead of creating their own. For Redis mode, all consumers share one connection pool (1000 max conns). For in-memory mode, all consumers share one GoCache instance. * Update management-integrations module to latest version * sync go.sum * Export `GetAddrFromEnv` to allow reuse across packages * Update management-integrations module version in go.mod and go.sum * Update management-integrations module version in go.mod and go.sum
This commit is contained in:
Vendored
+23
-2
@@ -17,12 +17,24 @@ import (
|
||||
|
||||
// RedisStoreEnvVar is the environment variable that determines if a redis store should be used.
|
||||
// The value should follow redis URL format. https://github.com/redis/redis-specifications/blob/master/uri/redis.txt
|
||||
const RedisStoreEnvVar = "NB_IDP_CACHE_REDIS_ADDRESS"
|
||||
const RedisStoreEnvVar = "NB_CACHE_REDIS_ADDRESS"
|
||||
|
||||
// legacyIdPCacheRedisEnvVar is the previous environment variable used for IDP cache.
|
||||
const legacyIdPCacheRedisEnvVar = "NB_IDP_CACHE_REDIS_ADDRESS"
|
||||
|
||||
const (
|
||||
// DefaultStoreMaxTimeout is the default max timeout for the shared cache store.
|
||||
DefaultStoreMaxTimeout = 7 * 24 * time.Hour
|
||||
// DefaultStoreCleanupInterval is the default cleanup interval for the shared cache store.
|
||||
DefaultStoreCleanupInterval = 30 * time.Minute
|
||||
// DefaultStoreMaxConn is the default max connections for the shared cache store.
|
||||
DefaultStoreMaxConn = 1000
|
||||
)
|
||||
|
||||
// NewStore creates a new cache store with the given max timeout and cleanup interval. It checks for the environment Variable RedisStoreEnvVar
|
||||
// to determine if a redis store should be used. If the environment variable is set, it will attempt to connect to the redis store.
|
||||
func NewStore(ctx context.Context, maxTimeout, cleanupInterval time.Duration, maxConn int) (store.StoreInterface, error) {
|
||||
redisAddr := os.Getenv(RedisStoreEnvVar)
|
||||
redisAddr := GetAddrFromEnv()
|
||||
if redisAddr != "" {
|
||||
return getRedisStore(ctx, redisAddr, maxConn)
|
||||
}
|
||||
@@ -30,6 +42,15 @@ func NewStore(ctx context.Context, maxTimeout, cleanupInterval time.Duration, ma
|
||||
return gocache_store.NewGoCache(goc), nil
|
||||
}
|
||||
|
||||
// GetAddrFromEnv returns the redis address from the environment variable RedisStoreEnvVar or its legacy counterpart.
|
||||
func GetAddrFromEnv() string {
|
||||
addr := os.Getenv(RedisStoreEnvVar)
|
||||
if addr == "" {
|
||||
addr = os.Getenv(legacyIdPCacheRedisEnvVar)
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
func getRedisStore(ctx context.Context, redisEnvAddr string, maxConn int) (store.StoreInterface, error) {
|
||||
options, err := redis.ParseURL(redisEnvAddr)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user