feat: add custom socket transport support for Postgres and MySQL#4187
Open
jowparks wants to merge 1 commit intolaunchbadge:mainfrom
Open
feat: add custom socket transport support for Postgres and MySQL#4187jowparks wants to merge 1 commit intolaunchbadge:mainfrom
jowparks wants to merge 1 commit intolaunchbadge:mainfrom
Conversation
cbedb56 to
9655878
Compare
Add `connect_socket()` methods to `PgConnection` and `MySqlConnection` that accept any pre-connected socket implementing the `Socket` trait. This enables using custom transport layers (e.g., vsock for AWS Nitro Enclaves, QUIC, or other non-TCP/UDS transports) without forking sqlx. Re-export `Socket` and `ReadBuf` traits from `sqlx::net` so users can implement custom socket types.
9655878 to
f7dcf2d
Compare
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.
Summary
Add
connect_socket()methods toPgConnectionandMySqlConnectionthat accept any pre-connected socket implementing theSockettrait. This enables using custom transport layers without forking sqlx.Motivation: In environments like AWS Nitro Enclaves, vsock is the only available networking transport. Currently, sqlx hardcodes its connection establishment to TCP and Unix domain sockets, making it impossible to use with non-standard transports without forking. This PR makes the
Sockettrait usable as a public extension point.Before / After
Before
There is no way to provide a custom socket to sqlx. The only connection paths are TCP and Unix domain sockets, hardcoded in
PgStream::connect()andMySqlConnection::establish():After
Users can connect over any transport by providing a pre-connected socket:
Changes
New public API
PgConnection::connect_socket(socket, &options)— connect to PostgreSQL over anySocketimplMySqlConnection::connect_socket(socket, &options)— connect to MySQL over anySocketimplsqlx::net::Socket— re-exported trait for implementing custom transportssqlx::net::ReadBuf— re-exported trait needed bySocketimplementationsFiles changed
sqlx-core/src/net/socket/mod.rs: Addconnect_socket()helper functionsqlx-core/src/net/mod.rs: Re-exportconnect_socketsqlx-postgres/src/connection/stream.rs: AddPgStream::connect_socket()sqlx-postgres/src/connection/establish.rs: Refactorestablish()to extractestablish_with_stream(), addestablish_with_socket()sqlx-postgres/src/connection/mod.rs: Add publicPgConnection::connect_socket()sqlx-mysql/src/connection/establish.rs: Refactor to extractestablish_with_stream(), addestablish_with_socket()sqlx-mysql/src/connection/mod.rs: Add publicMySqlConnection::connect_socket()sqlx-mysql/src/options/connect.rs: Extractconfigure_session()fromConnectOptions::connect()for reuse byconnect_socketsrc/lib.rs: Addsqlx::netmodule re-exportingSocketandReadBufDesign decisions
PgConnectOptions/MySqlConnectOptionsderiveClone + Debug, which prevents storingBox<dyn Socket>. A separateconnect_socket()method is cleaner and more ergonomic.MaybeUpgradeTls/DoHandshakeflow, so TLS upgrade negotiation works out of the box.establish_with_stream()to share code between the TCP/UDS path and the custom socket path. No behavioral changes to existing code.Sockettrait.Use cases
tokio-vsock)Checklist
cargo check --all-featurespasses