Skip to content

Allow to configure a custom connection step handler for pooled connections #42

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

Merged
merged 1 commit into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/pooled_connection/bb8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ where
type Error = PoolError;

async fn connect(&self) -> Result<Self::Connection, Self::Error> {
C::establish(&self.connection_url)
(self.setup)(&self.connection_url)
.await
.map_err(PoolError::ConnectionError)
}
Expand Down
2 changes: 1 addition & 1 deletion src/pooled_connection/deadpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ where
type Error = super::PoolError;

async fn create(&self) -> Result<Self::Type, Self::Error> {
C::establish(&self.connection_url)
(self.setup)(&self.connection_url)
.await
.map_err(super::PoolError::ConnectionError)
}
Expand Down
2 changes: 1 addition & 1 deletion src/pooled_connection/mobc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ where
type Error = PoolError;

async fn connect(&self) -> Result<Self::Connection, Self::Error> {
C::establish(&self.connection_url)
(self.setup)(&self.connection_url)
.await
.map_err(PoolError::ConnectionError)
}
Expand Down
31 changes: 23 additions & 8 deletions src/pooled_connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
//! * [deadpool](self::deadpool)
//! * [bb8](self::bb8)
//! * [mobc](self::mobc)

use crate::TransactionManager;
use crate::{AsyncConnection, SimpleAsyncConnection};
use futures::FutureExt;
use std::fmt;
use std::marker::PhantomData;
use std::ops::DerefMut;
use std::sync::{Arc, Mutex};

#[cfg(feature = "bb8")]
pub mod bb8;
Expand Down Expand Up @@ -48,18 +46,35 @@ impl std::error::Error for PoolError {}
/// * [bb8](self::bb8)
/// * [mobc](self::mobc)
pub struct AsyncDieselConnectionManager<C> {
// use arc/mutex here to make `C` always `Send` + `Sync`
// so that this type is send + sync
p: PhantomData<Arc<Mutex<C>>>,
setup:
Box<dyn Fn(&str) -> futures::future::BoxFuture<diesel::ConnectionResult<C>> + Send + Sync>,
connection_url: String,
}

impl<C> AsyncDieselConnectionManager<C> {
/// Returns a new connection manager,
/// which establishes connections to the given database URL.
pub fn new(connection_url: impl Into<String>) -> Self {
pub fn new(connection_url: impl Into<String>) -> Self
where
C: AsyncConnection + 'static,
{
Self::new_with_setup(connection_url, |url| C::establish(url).boxed())
}

/// Construct a new connection manger
/// with a custom setup procedure
///
/// This can be used to for example establish a SSL secured
/// postgres connection
pub fn new_with_setup(
connection_url: impl Into<String>,
setup: impl Fn(&str) -> futures::future::BoxFuture<diesel::ConnectionResult<C>>
+ Send
+ Sync
+ 'static,
) -> Self {
Self {
p: PhantomData,
setup: Box::new(setup),
connection_url: connection_url.into(),
}
}
Expand Down