feat: add custom transport support for Postgres and MySQL#4281
Open
Benjscho wants to merge 2 commits into
Open
feat: add custom transport support for Postgres and MySQL#4281Benjscho wants to merge 2 commits into
Benjscho wants to merge 2 commits into
Conversation
aa95915 to
ea6f530
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 maintainer feedback on launchbadge#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.
6fdce0f to
073247c
Compare
073247c to
0704cd9
Compare
Author
|
@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. |
Benjscho
commented
May 25, 2026
| const ADAPTER_BUF_SIZE: usize = 8192; | ||
|
|
||
| /// Generates an adapter struct + `Socket` impl for a given async I/O trait family. | ||
| macro_rules! impl_socket_adapter { |
Author
There was a problem hiding this comment.
This macro was to avoid repeating the code across tokio & futures, but happy to just expand it out if it makes things too unreadable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.