Skip to content

Commit 1523351

Browse files
authored
ref(redis): Configure min_idle for the Redis pool (#3861)
- Fixes a bug in the calculation of `max_connections` (`min` instead of `max`) - Adds support for `min_idle` which defaults to 20% of `max_connections`. The pool still has an idle timeout of 1 minute by default, which may need to be adjusted later.
1 parent 5f46bf7 commit 1523351

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

relay-config/src/config.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,12 +2285,20 @@ impl Config {
22852285

22862286
let redis = self.values.processing.redis.as_ref()?;
22872287

2288+
let max_connections = redis
2289+
.options
2290+
.max_connections
2291+
.unwrap_or(cpu_concurrency as u32 * 2)
2292+
.max(crate::redis::DEFAULT_MIN_MAX_CONNECTIONS);
2293+
2294+
let min_idle = redis
2295+
.options
2296+
.min_idle
2297+
.unwrap_or_else(|| max_connections.div_ceil(crate::redis::DEFAULT_MIN_IDLE_RATIO));
2298+
22882299
let options = RedisConfigOptions {
2289-
max_connections: redis
2290-
.options
2291-
.max_connections
2292-
.unwrap_or(cpu_concurrency as u32 * 2)
2293-
.min(crate::redis::DEFAULT_MIN_MAX_CONNECTIONS),
2300+
max_connections,
2301+
min_idle: Some(min_idle),
22942302
connection_timeout: redis.options.connection_timeout,
22952303
max_lifetime: redis.options.max_lifetime,
22962304
idle_timeout: redis.options.idle_timeout,

relay-config/src/redis.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ use serde::{Deserialize, Serialize};
44
/// In this case, we fall back to the old default.
55
pub(crate) const DEFAULT_MIN_MAX_CONNECTIONS: u32 = 24;
66

7+
/// By default the `min_idle` count of the Redis pool is set to the calculated
8+
/// amount of max connections divided by this value and rounded up.
9+
///
10+
/// To express this value as a percentage of max connections,
11+
/// use this formula: `100 / DEFAULT_MIN_IDLE_RATIO`.
12+
pub(crate) const DEFAULT_MIN_IDLE_RATIO: u32 = 5;
13+
714
/// Additional configuration options for a redis client.
815
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
916
#[serde(default)]
@@ -13,6 +20,11 @@ pub struct PartialRedisConfigOptions {
1320
/// Defaults to 2x `limits.max_thread_count` or a minimum of 24.
1421
#[serde(skip_serializing_if = "Option::is_none")]
1522
pub max_connections: Option<u32>,
23+
/// Minimum amount of idle connections kept alive in the pool.
24+
///
25+
/// If not set it will default to 20% of [`Self::max_connections`].
26+
#[serde(skip_serializing_if = "Option::is_none")]
27+
pub min_idle: Option<u32>,
1628
/// Sets the connection timeout used by the pool, in seconds.
1729
///
1830
/// Calls to `Pool::get` will wait this long for a connection to become available before returning an error.
@@ -31,6 +43,7 @@ impl Default for PartialRedisConfigOptions {
3143
fn default() -> Self {
3244
Self {
3345
max_connections: None,
46+
min_idle: None,
3447
connection_timeout: 5,
3548
max_lifetime: 300,
3649
idle_timeout: 60,

relay-redis/src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ use serde::{Deserialize, Serialize};
55
pub struct RedisConfigOptions {
66
/// Maximum number of connections managed by the pool.
77
pub max_connections: u32,
8+
/// Minimum amount of idle connections kept alive in the pool.
9+
///
10+
/// If not set it will default to [`Self::max_connections`].
11+
pub min_idle: Option<u32>,
812
/// Sets the connection timeout used by the pool, in seconds.
913
///
1014
/// Calls to `Pool::get` will wait this long for a connection to become available before returning an error.
@@ -23,6 +27,7 @@ impl Default for RedisConfigOptions {
2327
fn default() -> Self {
2428
Self {
2529
max_connections: 24,
30+
min_idle: None,
2631
connection_timeout: 5,
2732
max_lifetime: 300,
2833
idle_timeout: 60,

relay-redis/src/real.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ impl RedisPool {
151151
) -> Result<Self, RedisError> {
152152
let pool = Pool::builder()
153153
.max_size(opts.max_connections)
154+
.min_idle(opts.min_idle)
154155
.test_on_check_out(false)
155156
.max_lifetime(Some(Duration::from_secs(opts.max_lifetime)))
156157
.idle_timeout(Some(Duration::from_secs(opts.idle_timeout)))
@@ -166,6 +167,7 @@ impl RedisPool {
166167
pub fn single(server: &str, opts: RedisConfigOptions) -> Result<Self, RedisError> {
167168
let pool = Pool::builder()
168169
.max_size(opts.max_connections)
170+
.min_idle(opts.min_idle)
169171
.test_on_check_out(false)
170172
.max_lifetime(Some(Duration::from_secs(opts.max_lifetime)))
171173
.idle_timeout(Some(Duration::from_secs(opts.idle_timeout)))

0 commit comments

Comments
 (0)