diff --git a/cache/redis_cache.go b/cache/redis_cache.go index 2325531d5060..fac33bb4589b 100644 --- a/cache/redis_cache.go +++ b/cache/redis_cache.go @@ -22,13 +22,16 @@ type RedisCache struct { // RedisConfig defines how a RedisCache should be constructed. type RedisConfig struct { - Endpoint string `yaml:"endpoint"` - Timeout time.Duration `yaml:"timeout"` - Expiration time.Duration `yaml:"expiration"` - MaxIdleConns int `yaml:"max_idle_conns"` - MaxActiveConns int `yaml:"max_active_conns"` - Password flagext.Secret `yaml:"password"` - EnableTLS bool `yaml:"enable_tls"` + Endpoint string `yaml:"endpoint"` + Timeout time.Duration `yaml:"timeout"` + Expiration time.Duration `yaml:"expiration"` + MaxIdleConns int `yaml:"max_idle_conns"` + MaxActiveConns int `yaml:"max_active_conns"` + Password flagext.Secret `yaml:"password"` + EnableTLS bool `yaml:"enable_tls"` + IdleTimeout time.Duration `yaml:"idle_timeout"` + WaitOnPoolExhaustion bool `yaml:"wait_on_pool_exhaustion"` + MaxConnLifetime time.Duration `yaml:"max_conn_lifetime"` } // RegisterFlagsWithPrefix adds the flags required to config this to the given FlagSet @@ -40,6 +43,9 @@ func (cfg *RedisConfig) RegisterFlagsWithPrefix(prefix, description string, f *f f.IntVar(&cfg.MaxActiveConns, prefix+"redis.max-active-conns", 0, description+"Maximum number of active connections in pool.") f.Var(&cfg.Password, prefix+"redis.password", description+"Password to use when connecting to redis.") f.BoolVar(&cfg.EnableTLS, prefix+"redis.enable-tls", false, description+"Enables connecting to redis with TLS.") + f.DurationVar(&cfg.IdleTimeout, prefix+"redis.idle-timeout", 0, description+"Close connections after remaining idle for this duration. If the value is zero, then idle connections are not closed.") + f.BoolVar(&cfg.WaitOnPoolExhaustion, prefix+"redis.wait-on-pool-exhaustion", false, description+"Enables waiting if there are no idle connections. If the value is false and the pool is at the max_active_conns limit, the pool will return a connection with ErrPoolExhausted error and not wait for idle connections.") + f.DurationVar(&cfg.MaxConnLifetime, prefix+"redis.max-conn-lifetime", 0, description+"Close connections older than this duration. If the value is zero, then the pool does not close connections based on age.") } // NewRedisCache creates a new RedisCache @@ -48,8 +54,6 @@ func NewRedisCache(cfg RedisConfig, name string, pool *redis.Pool) *RedisCache { // pool != nil only in unit tests if pool == nil { pool = &redis.Pool{ - MaxIdle: cfg.MaxIdleConns, - MaxActive: cfg.MaxActiveConns, Dial: func() (redis.Conn, error) { options := make([]redis.DialOption, 0, 2) if cfg.EnableTLS { @@ -65,6 +69,11 @@ func NewRedisCache(cfg RedisConfig, name string, pool *redis.Pool) *RedisCache { } return c, err }, + MaxIdle: cfg.MaxIdleConns, + MaxActive: cfg.MaxActiveConns, + IdleTimeout: cfg.IdleTimeout, + Wait: cfg.WaitOnPoolExhaustion, + MaxConnLifetime: cfg.MaxConnLifetime, } }