feat(proxyd): change default cache ttl to 2 hours and make it configurable (#9645)

This commit is contained in:
felipe 2024-02-23 13:58:00 -08:00 committed by GitHub
parent a6c9489376
commit f44f147313
4 changed files with 12 additions and 7 deletions

@ -21,8 +21,6 @@ type Cache interface {
const ( const (
// assuming an average RPCRes size of 3 KB // assuming an average RPCRes size of 3 KB
memoryCacheLimit = 4096 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 { type cache struct {
@ -49,10 +47,11 @@ func (c *cache) Put(ctx context.Context, key string, value string) error {
type redisCache struct { type redisCache struct {
rdb *redis.Client rdb *redis.Client
prefix string prefix string
ttl time.Duration
} }
func newRedisCache(rdb *redis.Client, prefix string) *redisCache { func newRedisCache(rdb *redis.Client, prefix string, ttl time.Duration) *redisCache {
return &redisCache{rdb, prefix} return &redisCache{rdb, prefix, ttl}
} }
func (c *redisCache) namespaced(key string) string { 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 { func (c *redisCache) Put(ctx context.Context, key string, value string) error {
start := time.Now() 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())) redisCacheDurationSumm.WithLabelValues("SETEX").Observe(float64(time.Since(start).Milliseconds()))
if err != nil { if err != nil {

@ -29,7 +29,8 @@ type ServerConfig struct {
} }
type CacheConfig struct { type CacheConfig struct {
Enabled bool `toml:"enabled"` Enabled bool `toml:"enabled"`
TTL TOMLDuration `toml:"ttl"`
} }
type RedisConfig struct { type RedisConfig struct {

@ -235,7 +235,11 @@ func Start(config *Config) (*Server, func(), error) {
log.Warn("redis is not configured, using in-memory cache") log.Warn("redis is not configured, using in-memory cache")
cache = newMemoryCache() cache = newMemoryCache()
} else { } 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)) rpcCache = newRPCCache(newCacheWithCompression(cache))
} }

@ -42,6 +42,7 @@ const (
defaultWSHandshakeTimeout = 10 * time.Second defaultWSHandshakeTimeout = 10 * time.Second
defaultWSReadTimeout = 2 * time.Minute defaultWSReadTimeout = 2 * time.Minute
defaultWSWriteTimeout = 10 * time.Second defaultWSWriteTimeout = 10 * time.Second
defaultCacheTtl = 1 * time.Hour
maxRequestBodyLogLen = 2000 maxRequestBodyLogLen = 2000
defaultMaxUpstreamBatchSize = 10 defaultMaxUpstreamBatchSize = 10
defaultRateLimitHeader = "X-Forwarded-For" defaultRateLimitHeader = "X-Forwarded-For"