Repository navigation
Conversation
073247c to
0704cd9
Compare
|
@abonander would you be able to take a look at this when you have time? This was me taking a swing at addressing feedback on #4187. |
0704cd9 to
3bb6d73
Compare
3bb6d73 to
8a080b5
Compare
| #[cfg(feature = "_rt-tokio")] | ||
| pub struct TokioStream<S> { | ||
| inner: S, | ||
| read_buf: Vec<u8>, |
There was a problem hiding this comment.
It doesn't make sense to add yet another layer of buffering here. I know we've historically not focused on performance, but this would be an additional pessimization we don't really need.
I'm not against changing the Socket API to make this work (it could just be a combination of AsyncRead and AsyncWrite), however the problem is native_tls.
We have to wrap the stream in a type that implements the std::io::{Read, Write} traits, but we have to get the &mut Context inside for the .poll_*() calls. tokio-native-tls gets around this with unsafe code: https://docs.rs/tokio-native-tls/latest/src/tokio_native_tls/lib.rs.html#123-129
We could maybe use async-native-tls instead. It's essentially a copy-paste of tokio-native-tls (including the unsafe workaround) but the runtime-smol feature actually works for anything based on futures_io and doesn't directly depend on smol.
There was a problem hiding this comment.
That makes sense! I'll take a look at getting that to work
|
|
||
| /// Adapter that wraps a tokio [`AsyncRead`][tokio::io::AsyncRead] + | ||
| /// [`AsyncWrite`][tokio::io::AsyncWrite] into a [`Socket`] implementation. | ||
| #[cfg(feature = "_rt-tokio")] |
There was a problem hiding this comment.
There's already rt-tokio and rt-async-io modules, I don't know why you chose to put this here with the extra #[cfg]s.
| /// # Ok(()) | ||
| /// # } | ||
| /// ``` | ||
| pub fn connector<F, Fut>(mut self, connector: F) -> Self |
There was a problem hiding this comment.
I already had a connector API I was working on in https://github.com/transact-rs/sqlx/blob/ab/pool-changes/sqlx-core/src/pool/connect.rs but I got bogged down in major changes to the pool internals. I could probably lift that part out into its own branch though.
There was a problem hiding this comment.
That looks interesting, I could try to pull some of that in here, or if you want to pull it out into a branch that works too!
| pub async fn connect_raw_tokio<S>( | ||
| stream: S, | ||
| options: &MySqlConnectOptions, | ||
| ) -> Result<Self, Error> |
There was a problem hiding this comment.
These should be inherent methods on MySqlConnectOptions and PgConnectOptions respectively, where all the other connect() methods already exist.
I'm not particularly enamored with the naming but I need to think about what I'd go with instead.
| serde = { version = "1.0.219", optional = true } | ||
|
|
||
| tokio = { workspace = true, optional = true } | ||
| futures-io = { version = "0.3.32", optional = true } |
There was a problem hiding this comment.
futures-io should be promoted to a workspace dependency instead of repeating the version number everywhere.
| ) | ||
| ))] | ||
| #[sqlx_macros::test] | ||
| async fn it_connects_raw_tokio_with_tls() -> anyhow::Result<()> { |
There was a problem hiding this comment.
This test shouldn't be necessary. If DATABASE_URL contains SSL options it should use TLS automatically.
Add methods to PgConnection and MySqlConnection that accept pre-connected sockets implementing AsyncRead + AsyncWrite, enabling custom transport layers (vsock, QUIC, turmoil, SSH tunnels, etc.) without forking sqlx. Per maintainer feedback on transact-rs#4187, this uses AsyncRead + AsyncWrite traits instead of exposing the internal Socket trait. Two separate methods are provided for each runtime's trait set: - connect_with_custom_tokio(): accepts tokio::io::{AsyncRead, AsyncWrite} - connect_with_custom_futures(): accepts futures_io::{AsyncRead, AsyncWrite} Also adds PoolOptions::connector() so pools can use custom transports: PgPoolOptions::new() .connector(|options| async move { let socket = VsockStream::connect(addr).await?; PgConnection::connect_with_custom_tokio(socket, &options).await }) .connect_with(options) .await? TLS upgrade is negotiated automatically based on the connection options. No new public trait exposure. No behavioral changes to existing code.
8a080b5 to
c336a59
Compare
Add methods to PgConnection and MySqlConnection that accept pre-connected sockets implementing AsyncRead + AsyncWrite, enabling custom transport layers (vsock, QUIC, turmoil, SSH tunnels, etc.) without forking sqlx.
Per feedback on #4187, this uses AsyncRead + AsyncWrite traits instead of exposing the internal Socket trait. Two separate methods are provided for each runtime's trait set:
Also adds PoolOptions::connector() so pools can use custom transports:
TLS upgrade is negotiated automatically based on the connection options. No new public trait exposure. No behavioral changes to existing code.
Does your PR solve an issue?
No
Is this a breaking change?
No, it is additive with no behavior changes.