Skip to content

Commit

Permalink
ref(redis): Cleanup code around Redis (#4151)
Browse files Browse the repository at this point in the history
  • Loading branch information
iambriccardo authored Oct 18, 2024
1 parent 7e49639 commit 6a76476
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
9 changes: 3 additions & 6 deletions relay-config/src/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ fn build_redis_config_options(
let max_connections = options.max_connections.unwrap_or(default_connections);
let min_idle = options
.min_idle
.unwrap_or_else(|| max_connections.div_ceil(crate::redis::DEFAULT_MIN_IDLE_RATIO));
.unwrap_or_else(|| max_connections.div_ceil(DEFAULT_MIN_IDLE_RATIO));

RedisConfigOptions {
max_connections,
Expand All @@ -254,7 +254,6 @@ pub(super) fn create_redis_pool(
RedisConfig::Cluster {
cluster_nodes,
options,
..
} => RedisConfigRef::Cluster {
cluster_nodes,
options: build_redis_config_options(options, default_connections),
Expand All @@ -281,10 +280,8 @@ pub(super) fn create_redis_pool(
pub(super) fn create_redis_pools(configs: &RedisConfigs, cpu_concurrency: u32) -> RedisPoolConfigs {
// Default `max_connections` for the `project_configs` pool.
// In a unified config, this is used for all pools.
let project_configs_default_connections = std::cmp::max(
cpu_concurrency * 2,
crate::redis::DEFAULT_MIN_MAX_CONNECTIONS,
);
let project_configs_default_connections =
std::cmp::max(cpu_concurrency * 2, DEFAULT_MIN_MAX_CONNECTIONS);

match configs {
RedisConfigs::Unified(cfg) => {
Expand Down
22 changes: 7 additions & 15 deletions relay-redis/src/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl PooledClient {
/// Recursively computes the [`ConnectionInner`] from a [`PooledClient`].
fn connection_inner(&mut self) -> Result<ConnectionInner<'_>, RedisError> {
let inner = match self {
PooledClient::Cluster(ref mut connection, opts) => {
PooledClient::Cluster(connection, opts) => {
connection
.set_read_timeout(Some(Duration::from_secs(opts.read_timeout)))
.map_err(RedisError::Redis)?;
Expand All @@ -220,7 +220,7 @@ impl PooledClient {
secondaries: secondary_connections,
}
}
PooledClient::Single(ref mut connection, opts) => {
PooledClient::Single(connection, opts) => {
connection
.set_read_timeout(Some(Duration::from_secs(opts.read_timeout)))
.map_err(RedisError::Redis)?;
Expand Down Expand Up @@ -295,15 +295,17 @@ impl RedisPool {

/// Creates a [`RedisPool`] in single-node configuration.
pub fn single(server: &str, opts: RedisConfigOptions) -> Result<Self, RedisError> {
let pool = Self::client_pool(server, &opts)?;
let pool = Self::base_pool_builder(&opts)
.build(redis::Client::open(server).map_err(RedisError::Redis)?)
.map_err(RedisError::Pool)?;

Ok(RedisPool::Single(pool, opts))
}

/// Returns a pooled connection to a client.
pub fn client(&self) -> Result<PooledClient, RedisError> {
let pool = match self {
RedisPool::Cluster(ref pool, opts) => PooledClient::Cluster(
RedisPool::Cluster(pool, opts) => PooledClient::Cluster(
Box::new(pool.get().map_err(RedisError::Pool)?),
opts.clone(),
),
Expand All @@ -323,7 +325,7 @@ impl RedisPool {
secondaries: secondary_clients,
}
}
RedisPool::Single(ref pool, opts) => PooledClient::Single(
RedisPool::Single(pool, opts) => PooledClient::Single(
Box::new(pool.get().map_err(RedisError::Pool)?),
opts.clone(),
),
Expand All @@ -341,16 +343,6 @@ impl RedisPool {
}
}

/// Returns a [`Pool`] with a [`redis::Client`].
fn client_pool(
server: &str,
opts: &RedisConfigOptions,
) -> Result<Pool<redis::Client>, RedisError> {
Self::base_pool_builder(opts)
.build(redis::Client::open(server).map_err(RedisError::Redis)?)
.map_err(RedisError::Pool)
}

/// Returns the base builder for the pool with the options applied.
fn base_pool_builder<M: ManageConnection>(opts: &RedisConfigOptions) -> Builder<M> {
Pool::builder()
Expand Down

0 comments on commit 6a76476

Please sign in to comment.