Skip to content

Commit

Permalink
Allow configuring Redis connections via AsyncConnectionConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis-Tarvin committed Sep 25, 2024
1 parent ed5b13b commit 6304ddd
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions redis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use std::{
use deadpool::managed;
use redis::{
aio::{ConnectionLike, MultiplexedConnection},
Client, IntoConnectionInfo, RedisError, RedisResult,
AsyncConnectionConfig, Client, IntoConnectionInfo, RedisError, RedisResult,
};

pub use redis;
Expand Down Expand Up @@ -127,10 +127,20 @@ impl ConnectionLike for Connection {
/// [`Manager`] for creating and recycling [`redis`] connections.
///
/// [`Manager`]: managed::Manager
#[derive(Debug)]
pub struct Manager {
client: Client,
ping_number: AtomicUsize,
connection_config: AsyncConnectionConfig,
}

// `redis::AsyncConnectionConfig: !Debug`
impl std::fmt::Debug for Manager {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Manager")
.field("client", &self.client)
.field("ping_number", &self.ping_number)
.finish()
}
}

impl Manager {
Expand All @@ -140,9 +150,22 @@ impl Manager {
///
/// If establishing a new [`Client`] fails.
pub fn new<T: IntoConnectionInfo>(params: T) -> RedisResult<Self> {
Self::from_config(params, AsyncConnectionConfig::default())
}

/// Creates a new [`Manager`] from the given `params` and [`AsyncConnectionConfig`].
///
/// # Errors
///
/// If establishing a new [`Client`] fails.
pub fn from_config<T: IntoConnectionInfo>(
params: T,
connection_config: AsyncConnectionConfig,
) -> RedisResult<Self> {
Ok(Self {
client: Client::open(params)?,
ping_number: AtomicUsize::new(0),
connection_config,
})
}
}
Expand All @@ -152,7 +175,10 @@ impl managed::Manager for Manager {
type Error = RedisError;

async fn create(&self) -> Result<MultiplexedConnection, RedisError> {
let conn = self.client.get_multiplexed_async_connection().await?;
let conn = self
.client
.get_multiplexed_async_connection_with_config(&self.connection_config)
.await?;
Ok(conn)
}

Expand Down

0 comments on commit 6304ddd

Please sign in to comment.