Skip to content

Commit d9725f3

Browse files
committed
feat: add custom socket transport support for Postgres and MySQL
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 #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.
1 parent 75bc048 commit d9725f3

11 files changed

Lines changed: 791 additions & 7 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ _unstable-docs = [
9494
]
9595

9696
# Base runtime features without TLS
97-
runtime-async-global-executor = ["_rt-async-global-executor", "sqlx-core/_rt-async-global-executor", "sqlx-macros?/_rt-async-global-executor"]
98-
runtime-async-std = ["_rt-async-std", "sqlx-core/_rt-async-std", "sqlx-macros?/_rt-async-std"]
99-
runtime-smol = ["_rt-smol", "sqlx-core/_rt-smol", "sqlx-macros?/_rt-smol"]
100-
runtime-tokio = ["_rt-tokio", "sqlx-core/_rt-tokio", "sqlx-macros?/_rt-tokio"]
97+
runtime-async-global-executor = ["_rt-async-global-executor", "sqlx-core/_rt-async-global-executor", "sqlx-macros?/_rt-async-global-executor", "sqlx-postgres?/_rt-async-io", "sqlx-mysql?/_rt-async-io"]
98+
runtime-async-std = ["_rt-async-std", "sqlx-core/_rt-async-std", "sqlx-macros?/_rt-async-std", "sqlx-postgres?/_rt-async-io", "sqlx-mysql?/_rt-async-io"]
99+
runtime-smol = ["_rt-smol", "sqlx-core/_rt-smol", "sqlx-macros?/_rt-smol", "sqlx-postgres?/_rt-async-io", "sqlx-mysql?/_rt-async-io"]
100+
runtime-tokio = ["_rt-tokio", "sqlx-core/_rt-tokio", "sqlx-macros?/_rt-tokio", "sqlx-postgres?/_rt-tokio", "sqlx-mysql?/_rt-tokio"]
101101

102102
# TLS features
103103
tls-native-tls = ["sqlx-core/_tls-native-tls", "sqlx-macros?/_tls-native-tls"]

sqlx-core/src/net/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ pub mod tls;
44
pub use socket::{
55
connect_tcp, connect_uds, BufferedSocket, Socket, SocketIntoBox, WithSocket, WriteBuffer,
66
};
7+
8+
#[cfg(feature = "_rt-tokio")]
9+
pub use socket::async_rw_adapter::TokioStream;
10+
11+
#[cfg(feature = "_rt-async-io")]
12+
pub use socket::async_rw_adapter::FuturesStream;

0 commit comments

Comments
 (0)