Skip to content

Commit

Permalink
Add Redis cache options used by Redigo (grafana#2550)
Browse files Browse the repository at this point in the history
* add Redis cache flags used by redigo: idle-timeout, wait, max-conn-lifetime

Signed-off-by: Kyeongwon Seo <ruddnjs1230@gmail.com>

* update CHANGELOG.md

Signed-off-by: Kyeongwon Seo <ruddnjs1230@gmail.com>

* apply check-doc comments

Signed-off-by: Kyeongwon Seo <ruddnjs1230@gmail.com>

* remove white-noise

Signed-off-by: Kyeongwon Seo <ruddnjs1230@gmail.com>

* rename the Redis flag: wait -> wait-on-pool-exhaustion

Signed-off-by: Kyeongwon Seo <ruddnjs1230@gmail.com>
  • Loading branch information
kwSeo authored May 2, 2020
1 parent c323f72 commit 024b44e
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions cache/redis_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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,
}
}

Expand Down

0 comments on commit 024b44e

Please sign in to comment.