From f44f147313450056ad9a247b04e512814139b314 Mon Sep 17 00:00:00 2001 From: felipe <130432649+felipe-op@users.noreply.github.com> Date: Fri, 23 Feb 2024 13:58:00 -0800 Subject: [PATCH] feat(proxyd): change default cache ttl to 2 hours and make it configurable (#9645) --- proxyd/proxyd/cache.go | 9 ++++----- proxyd/proxyd/config.go | 3 ++- proxyd/proxyd/proxyd.go | 6 +++++- proxyd/proxyd/server.go | 1 + 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/proxyd/proxyd/cache.go b/proxyd/proxyd/cache.go index a93a393..5add4f2 100644 --- a/proxyd/proxyd/cache.go +++ b/proxyd/proxyd/cache.go @@ -21,8 +21,6 @@ type Cache interface { const ( // assuming an average RPCRes size of 3 KB memoryCacheLimit = 4096 - // Set a large ttl to avoid expirations. However, a ttl must be set for volatile-lru to take effect. - redisTTL = 30 * 7 * 24 * time.Hour ) type cache struct { @@ -49,10 +47,11 @@ func (c *cache) Put(ctx context.Context, key string, value string) error { type redisCache struct { rdb *redis.Client prefix string + ttl time.Duration } -func newRedisCache(rdb *redis.Client, prefix string) *redisCache { - return &redisCache{rdb, prefix} +func newRedisCache(rdb *redis.Client, prefix string, ttl time.Duration) *redisCache { + return &redisCache{rdb, prefix, ttl} } func (c *redisCache) namespaced(key string) string { @@ -78,7 +77,7 @@ func (c *redisCache) Get(ctx context.Context, key string) (string, error) { func (c *redisCache) Put(ctx context.Context, key string, value string) error { start := time.Now() - err := c.rdb.SetEx(ctx, c.namespaced(key), value, redisTTL).Err() + err := c.rdb.SetEx(ctx, c.namespaced(key), value, c.ttl).Err() redisCacheDurationSumm.WithLabelValues("SETEX").Observe(float64(time.Since(start).Milliseconds())) if err != nil { diff --git a/proxyd/proxyd/config.go b/proxyd/proxyd/config.go index bb7fe22..cd0cca1 100644 --- a/proxyd/proxyd/config.go +++ b/proxyd/proxyd/config.go @@ -29,7 +29,8 @@ type ServerConfig struct { } type CacheConfig struct { - Enabled bool `toml:"enabled"` + Enabled bool `toml:"enabled"` + TTL TOMLDuration `toml:"ttl"` } type RedisConfig struct { diff --git a/proxyd/proxyd/proxyd.go b/proxyd/proxyd/proxyd.go index 87d9055..7af47b1 100644 --- a/proxyd/proxyd/proxyd.go +++ b/proxyd/proxyd/proxyd.go @@ -235,7 +235,11 @@ func Start(config *Config) (*Server, func(), error) { log.Warn("redis is not configured, using in-memory cache") cache = newMemoryCache() } else { - cache = newRedisCache(redisClient, config.Redis.Namespace) + ttl := defaultCacheTtl + if config.Cache.TTL != 0 { + ttl = time.Duration(config.Cache.TTL) + } + cache = newRedisCache(redisClient, config.Redis.Namespace, ttl) } rpcCache = newRPCCache(newCacheWithCompression(cache)) } diff --git a/proxyd/proxyd/server.go b/proxyd/proxyd/server.go index 4c27785..3fa3e00 100644 --- a/proxyd/proxyd/server.go +++ b/proxyd/proxyd/server.go @@ -42,6 +42,7 @@ const ( defaultWSHandshakeTimeout = 10 * time.Second defaultWSReadTimeout = 2 * time.Minute defaultWSWriteTimeout = 10 * time.Second + defaultCacheTtl = 1 * time.Hour maxRequestBodyLogLen = 2000 defaultMaxUpstreamBatchSize = 10 defaultRateLimitHeader = "X-Forwarded-For"