Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring Redis connections via AsyncConnectionConfig #357

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading