Skip to content

feat: add connect_with_socket() for custom transports#4212

Closed
airhorns wants to merge 1 commit intolaunchbadge:mainfrom
airhorns:connect-with-socket
Closed

feat: add connect_with_socket() for custom transports#4212
airhorns wants to merge 1 commit intolaunchbadge:mainfrom
airhorns:connect-with-socket

Conversation

@airhorns
Copy link
Copy Markdown

@airhorns airhorns commented Apr 1, 2026

I want to make use of the turmoil crate to implement deterministic simulation testing for a system I'm working on that makes use of sqlx. Turmoil works by virtualizing all the networking that different elements of a system do so it can inject faults, like in my app's connection to MySQL! sqlx doesn't need to know anything about turmoil, but before this change, it wasn't possible to inject in turmoil's special socket implementation that lets it do its thing. It's weird to say, but sqlx "hardcoded" the use of tokio's socket.

To let folks like me inject alternative implementations of the Socket trait, we need a new high level public function for connecting using our own sockets! This adds that. The same api also would let folks inject sockets that do weird other transport stuff, like tunnels or proxies.

Changes:

  • Add net::connect_with() in sqlx-core as the analog of connect_tcp
    and connect_uds for pre-established sockets
  • Add MySqlConnectOptions::connect_with_socket() and
    MySqlConnection::connect_with_socket() for MySQL
  • Add PgConnectOptions::connect_with_socket() and
    PgConnection::connect_with_socket() for PostgreSQL
  • Re-export Socket trait and connect_with from top-level sqlx::net
  • Refactor MySQL post-connect init into shared after_connect() to
    avoid duplication between connect() and connect_with_socket()

The Socket trait was already public; these changes simply expose a way to pass a pre-connected Socket into the connection establishment flow that was previously only reachable through connect_tcp/connect_uds.

Does your PR solve an issue?

No, no open issue

Is this a breaking change?

No, no breaking change

@airhorns airhorns marked this pull request as draft April 1, 2026 01:35
@airhorns airhorns force-pushed the connect-with-socket branch 3 times, most recently from b68895c to 601227c Compare April 1, 2026 02:43
Add the ability to establish MySQL and PostgreSQL connections over
pre-connected sockets, enabling custom transports such as in-memory
pipes, simulation frameworks (e.g. turmoil for deterministic simulation
testing), SSH tunnels, or SOCKS proxies.

Changes:
- Add `net::connect_with()` in sqlx-core as the analog of `connect_tcp`
  and `connect_uds` for pre-established sockets
- Add `MySqlConnectOptions::connect_with_socket()` and
  `MySqlConnection::connect_with_socket()` for MySQL
- Add `PgConnectOptions::connect_with_socket()` and
  `PgConnection::connect_with_socket()` for PostgreSQL
- Re-export `Socket` trait and `connect_with` from top-level `sqlx::net`
- Refactor MySQL post-connect init into shared `after_connect()` to
  avoid duplication between `connect()` and `connect_with_socket()`

The Socket trait was already public; these changes simply expose a way
to pass a pre-connected Socket into the connection establishment flow
that was previously only reachable through connect_tcp/connect_uds.
@airhorns airhorns force-pushed the connect-with-socket branch from 601227c to cf6ee57 Compare April 1, 2026 02:53
@abonander
Copy link
Copy Markdown
Collaborator

Duplicate of #4187

@abonander abonander marked this as a duplicate of #4187 Apr 1, 2026
@abonander abonander closed this Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants