Replies: 2 comments 14 replies
-
@oeed check this example out. |
Beta Was this translation helpful? Give feedback.
-
Hey @weiznich I thought I'd piggy back on this discussion rather than opening "yet another TLS related" question/discussion/issue. While having a poke around in the I get that this is by design, but the error handling as implemented by you vs. the error handling as suggested by tokio_postgres is night and day, that is, it's a lot more pleasant in diesel_async's API, and losing out on this or having to reimplement the whole connection to get this error handling with TLS feels like throwing the baby out with the bath water. Before opening a PR I wanted to check if adding an extra method that accepts both a I get that it's a bit of a clunky API and a little esoteric so definitely understand if it's something you're not interested in adding, but it feels like an alright compromise between supporting TLS (and making it easier to use) and not wanting to take on extra dependencies. Suggestion (naming and documentation to be improved): diff --git a/examples/postgres/pooled-with-rustls/src/main.rs b/examples/postgres/pooled-with-rustls/src/main.rs
index a18451c..87a8eb4 100644
--- a/examples/postgres/pooled-with-rustls/src/main.rs
+++ b/examples/postgres/pooled-with-rustls/src/main.rs
@@ -49,12 +49,8 @@ fn establish_connection(config: &str) -> BoxFuture<ConnectionResult<AsyncPgConne
let (client, conn) = tokio_postgres::connect(config, tls)
.await
.map_err(|e| ConnectionError::BadConnection(e.to_string()))?;
- tokio::spawn(async move {
- if let Err(e) = conn.await {
- eprintln!("Database connection: {e}");
- }
- });
- AsyncPgConnection::try_from(client).await
+
+ AsyncPgConnection::try_from_client_and_connection(client, conn).await
};
fut.boxed()
}
diff --git a/src/pg/mod.rs b/src/pg/mod.rs
index d39e803..c030e09 100644
--- a/src/pg/mod.rs
+++ b/src/pg/mod.rs
@@ -156,16 +156,8 @@ impl AsyncConnection for AsyncPgConnection {
let (client, connection) = tokio_postgres::connect(database_url, tokio_postgres::NoTls)
.await
.map_err(ErrorHelper)?;
- let (tx, rx) = tokio::sync::broadcast::channel(1);
- let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
- tokio::spawn(async move {
- match futures_util::future::select(shutdown_rx, connection).await {
- Either::Left(_) | Either::Right((Ok(_), _)) => {}
- Either::Right((Err(e), _)) => {
- let _ = tx.send(Arc::new(e));
- }
- }
- });
+
+ let (rx, shutdown_tx) = drive_connection(connection);
let r = Self::setup(
client,
@@ -174,6 +166,7 @@ impl AsyncConnection for AsyncPgConnection {
Arc::clone(&instrumentation),
)
.await;
+
instrumentation
.lock()
.unwrap_or_else(|e| e.into_inner())
@@ -367,6 +360,28 @@ impl AsyncPgConnection {
.await
}
+ /// Constructs a new `AsyncPgConnection` from an existing [`tokio_postgres::Client`] and
+ /// [`tokio_postgres::Connection`].
+ pub async fn try_from_client_and_connection<S>(
+ client: tokio_postgres::Client,
+ conn: tokio_postgres::Connection<tokio_postgres::Socket, S>,
+ ) -> ConnectionResult<Self>
+ where
+ S: tokio_postgres::tls::TlsStream + Unpin + Send + 'static,
+ {
+ let (rx, shutdown_tx) = drive_connection(conn);
+
+ Self::setup(
+ client,
+ Some(rx),
+ Some(shutdown_tx),
+ Arc::new(std::sync::Mutex::new(
+ diesel::connection::get_default_instrumentation(),
+ )),
+ )
+ .await
+ }
+
async fn setup(
conn: tokio_postgres::Client,
connection_future: Option<broadcast::Receiver<Arc<tokio_postgres::Error>>>,
@@ -826,6 +841,30 @@ async fn drive_future<R>(
}
}
+fn drive_connection<S>(
+ conn: tokio_postgres::Connection<tokio_postgres::Socket, S>,
+) -> (
+ broadcast::Receiver<Arc<tokio_postgres::Error>>,
+ oneshot::Sender<()>,
+)
+where
+ S: tokio_postgres::tls::TlsStream + Unpin + Send + 'static,
+{
+ let (tx, rx) = tokio::sync::broadcast::channel(1);
+ let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
+
+ tokio::spawn(async move {
+ match futures_util::future::select(shutdown_rx, conn).await {
+ Either::Left(_) | Either::Right((Ok(_), _)) => {}
+ Either::Right((Err(e), _)) => {
+ let _ = tx.send(Arc::new(e));
+ }
+ }
+ });
+
+ (rx, shutdown_tx)
+}
+
#[cfg(any(
feature = "deadpool",
feature = "bb8",
|
Beta Was this translation helpful? Give feedback.
-
I'm in need of TLS support for Postgres and can see it currently is hard coded to
NoTls
.Adding TLS support to the connection seems fairly straight forward and I'm happy to PR it, but a little unsure what the prefered interface is, as
AsyncConnection::establish
just takes a string, whereas it'd ideally also takeT: tokio_postgres::tls::MakeTlsConnect<Socket>
– but that doesn't make sense to add to the agnosticAsyncConnection
trait.Potentially, a dedicated
AsyncPgConnection::establish_with_tls
function whichAsyncConnection::establish
calls would work. But not sure if you'd prefer a builder pattern, etc. for future compatibility.Beta Was this translation helpful? Give feedback.
All reactions