Skip to content

Commit ac79665

Browse files
authored
Add Redis cache options used by Redigo (#2550)
* add Redis cache flags used by redigo: idle-timeout, wait, max-conn-lifetime Signed-off-by: Kyeongwon Seo <[email protected]> * update CHANGELOG.md Signed-off-by: Kyeongwon Seo <[email protected]> * apply check-doc comments Signed-off-by: Kyeongwon Seo <[email protected]> * remove white-noise Signed-off-by: Kyeongwon Seo <[email protected]> * rename the Redis flag: wait -> wait-on-pool-exhaustion Signed-off-by: Kyeongwon Seo <[email protected]>
1 parent 3462eb6 commit ac79665

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* [ENHANCEMENT] Add de-duplicated chunks counter `cortex_chunk_store_deduped_chunks_total` which counts every chunk not sent to the store because it was already sent by another replica. #2485
4848
* [ENHANCEMENT] query-frontend now also logs the POST data of long queries. #2481
4949
* [ENHANCEMENT] Experimental WAL: Ingester WAL records now have type header and the custom WAL records have been replaced by Prometheus TSDB's WAL records. Old records will not be supported from 1.3 onwards. Note: once this is deployed, you cannot downgrade without data loss. #2436
50+
* [ENHANCEMENT] Redis Cache: Added `idle_timeout`, `wait_on_pool_exhaustion` and `max_conn_lifetime` options to redis cache configuration. #2550
5051
* [BUGFIX] Ruler: Ensure temporary rule files with special characters are properly mapped and cleaned up. #2506
5152
* [BUGFIX] Fixes #2411, Ensure requests are properly routed to the prometheus api embedded in the query if `-server.path-prefix` is set. #2372
5253
* [BUGFIX] Experimental TSDB: fixed chunk data corruption when querying back series using the experimental blocks storage. #2400

docs/configuration/config-file-reference.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,6 +2363,22 @@ The `redis_config` configures the Redis backend cache. The supported CLI flags `
23632363
# Enables connecting to redis with TLS.
23642364
# CLI flag: -<prefix>.redis.enable-tls
23652365
[enable_tls: <boolean> | default = false]
2366+
2367+
# Close connections after remaining idle for this duration. If the value is
2368+
# zero, then idle connections are not closed.
2369+
# CLI flag: -<prefix>.redis.idle-timeout
2370+
[idle_timeout: <duration> | default = 0s]
2371+
2372+
# Enables waiting if there are no idle connections. If the value is false and
2373+
# the pool is at the max_active_conns limit, the pool will return a connection
2374+
# with ErrPoolExhausted error and not wait for idle connections.
2375+
# CLI flag: -<prefix>.redis.wait-on-pool-exhaustion
2376+
[wait_on_pool_exhaustion: <boolean> | default = false]
2377+
2378+
# Close connections older than this duration. If the value is zero, then the
2379+
# pool does not close connections based on age.
2380+
# CLI flag: -<prefix>.redis.max-conn-lifetime
2381+
[max_conn_lifetime: <duration> | default = 0s]
23662382
```
23672383

23682384
### `memcached_config`

pkg/chunk/cache/redis_cache.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ type RedisCache struct {
2222

2323
// RedisConfig defines how a RedisCache should be constructed.
2424
type RedisConfig struct {
25-
Endpoint string `yaml:"endpoint"`
26-
Timeout time.Duration `yaml:"timeout"`
27-
Expiration time.Duration `yaml:"expiration"`
28-
MaxIdleConns int `yaml:"max_idle_conns"`
29-
MaxActiveConns int `yaml:"max_active_conns"`
30-
Password flagext.Secret `yaml:"password"`
31-
EnableTLS bool `yaml:"enable_tls"`
25+
Endpoint string `yaml:"endpoint"`
26+
Timeout time.Duration `yaml:"timeout"`
27+
Expiration time.Duration `yaml:"expiration"`
28+
MaxIdleConns int `yaml:"max_idle_conns"`
29+
MaxActiveConns int `yaml:"max_active_conns"`
30+
Password flagext.Secret `yaml:"password"`
31+
EnableTLS bool `yaml:"enable_tls"`
32+
IdleTimeout time.Duration `yaml:"idle_timeout"`
33+
WaitOnPoolExhaustion bool `yaml:"wait_on_pool_exhaustion"`
34+
MaxConnLifetime time.Duration `yaml:"max_conn_lifetime"`
3235
}
3336

3437
// 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
4043
f.IntVar(&cfg.MaxActiveConns, prefix+"redis.max-active-conns", 0, description+"Maximum number of active connections in pool.")
4144
f.Var(&cfg.Password, prefix+"redis.password", description+"Password to use when connecting to redis.")
4245
f.BoolVar(&cfg.EnableTLS, prefix+"redis.enable-tls", false, description+"Enables connecting to redis with TLS.")
46+
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.")
47+
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.")
48+
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.")
4349
}
4450

4551
// NewRedisCache creates a new RedisCache
@@ -48,8 +54,6 @@ func NewRedisCache(cfg RedisConfig, name string, pool *redis.Pool) *RedisCache {
4854
// pool != nil only in unit tests
4955
if pool == nil {
5056
pool = &redis.Pool{
51-
MaxIdle: cfg.MaxIdleConns,
52-
MaxActive: cfg.MaxActiveConns,
5357
Dial: func() (redis.Conn, error) {
5458
options := make([]redis.DialOption, 0, 2)
5559
if cfg.EnableTLS {
@@ -65,6 +69,11 @@ func NewRedisCache(cfg RedisConfig, name string, pool *redis.Pool) *RedisCache {
6569
}
6670
return c, err
6771
},
72+
MaxIdle: cfg.MaxIdleConns,
73+
MaxActive: cfg.MaxActiveConns,
74+
IdleTimeout: cfg.IdleTimeout,
75+
Wait: cfg.WaitOnPoolExhaustion,
76+
MaxConnLifetime: cfg.MaxConnLifetime,
6877
}
6978
}
7079

0 commit comments

Comments
 (0)