Skip to content

Commit 2dd0195

Browse files
authored
Merge pull request #42 from weiznich/customize_connection_setup_for_pools
Allow to configure a custom connection step handler for pooled connections
2 parents 5717b2f + c12f47f commit 2dd0195

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

src/pooled_connection/bb8.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ where
6262
type Error = PoolError;
6363

6464
async fn connect(&self) -> Result<Self::Connection, Self::Error> {
65-
C::establish(&self.connection_url)
65+
(self.setup)(&self.connection_url)
6666
.await
6767
.map_err(PoolError::ConnectionError)
6868
}

src/pooled_connection/deadpool.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ where
7070
type Error = super::PoolError;
7171

7272
async fn create(&self) -> Result<Self::Type, Self::Error> {
73-
C::establish(&self.connection_url)
73+
(self.setup)(&self.connection_url)
7474
.await
7575
.map_err(super::PoolError::ConnectionError)
7676
}

src/pooled_connection/mobc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ where
5959
type Error = PoolError;
6060

6161
async fn connect(&self) -> Result<Self::Connection, Self::Error> {
62-
C::establish(&self.connection_url)
62+
(self.setup)(&self.connection_url)
6363
.await
6464
.map_err(PoolError::ConnectionError)
6565
}

src/pooled_connection/mod.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
//! * [deadpool](self::deadpool)
66
//! * [bb8](self::bb8)
77
//! * [mobc](self::mobc)
8-
98
use crate::TransactionManager;
109
use crate::{AsyncConnection, SimpleAsyncConnection};
10+
use futures::FutureExt;
1111
use std::fmt;
12-
use std::marker::PhantomData;
1312
use std::ops::DerefMut;
14-
use std::sync::{Arc, Mutex};
1513

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

5754
impl<C> AsyncDieselConnectionManager<C> {
5855
/// Returns a new connection manager,
5956
/// which establishes connections to the given database URL.
60-
pub fn new(connection_url: impl Into<String>) -> Self {
57+
pub fn new(connection_url: impl Into<String>) -> Self
58+
where
59+
C: AsyncConnection + 'static,
60+
{
61+
Self::new_with_setup(connection_url, |url| C::establish(url).boxed())
62+
}
63+
64+
/// Construct a new connection manger
65+
/// with a custom setup procedure
66+
///
67+
/// This can be used to for example establish a SSL secured
68+
/// postgres connection
69+
pub fn new_with_setup(
70+
connection_url: impl Into<String>,
71+
setup: impl Fn(&str) -> futures::future::BoxFuture<diesel::ConnectionResult<C>>
72+
+ Send
73+
+ Sync
74+
+ 'static,
75+
) -> Self {
6176
Self {
62-
p: PhantomData,
77+
setup: Box::new(setup),
6378
connection_url: connection_url.into(),
6479
}
6580
}

0 commit comments

Comments
 (0)