Postgres: connect over a caller-provided socket, and a wasm-safe `Instant`, for hosts like Cloudflare Workers
I have found these related issues/pull requests
Relates to #1145, #2985, #4056
Description
Hosts that give you their own sockets have no way to run the Postgres driver, even though the protocol code itself compiles fine for them. My case is Cloudflare Workers (wasm32-unknown-unknown): the runtime hands out TCP sockets through its connect() API (for example, to reach Postgres through Hyperdrive), and sqlx-postgres 0.9 builds for that target once getrandom's wasm_js feature is on. Two things then panic at runtime:
- Connecting.
PgStream::connectalways goes throughsqlx_core::net::connect_tcp(orconnect_uds). With no tokio or async-io runtime, that ends inrt::missing_rt, and there is no public way to handPgConnectiona socket opened some other way. - Every query.
QueryLogger::newcallsstd::time::Instant::now(), which panics onwasm32-unknown-unknownwithtime not implemented on this platform. The migrators call it too.
Prefered solution
Two small, additive changes, both on a branch: https://github.com/transact-rs/sqlx/compare/main...iamralch:sqlx:wasm-custom-socket
PgConnection::connect_with_socket(&options, socket), taking anysqlx_core::net::Socketthe caller has already connected. The host, port and socket path in the options are ignored; everything else, includingssl_mode, applies as it does forconnect_with.establishnow goes through the same path after opening its own socket, so existing connections are unchanged.sqlx_core::rt::Instant:std::time::Instanteverywhere exceptwasm32-unknown-unknown, where it isweb_time::Instant(the host'sperformance.now()). Used in the query logger and the Postgres, MySQL and SQLite migrators.web-timeis a dependency on that target only.
This is deliberately smaller than #4056. It doesn't add a runtime to sqlx; it lets the caller bring the transport, which should also cover WASI sockets or a WebSocket tunnel like #1145, without sqlx knowing about any of them. It leaves Pool and PgListener alone: both need a runtime to spawn tasks, and on hosts like Workers you want one connection per request anyway.
Tested:
- A Worker under
wrangler dev --local, against Postgres 17 with SCRAM auth: connect, bound parameters and scalar types, 2,000 rows spanning many socket reads, unique-violation mapping, rollback and commit, prepared-statement reuse,sqlx::migrate!()migrations, ping and close. All pass. Reverting only theInstantchange makes the same Worker panic withtime not implemented on this platform. - On the host: a new
it_connects_with_sockettest (a tokioTcpStreamopened by the test, thenconnect_with_socket), plus the existingtests/postgres/postgres.rs(58 passed, 2 ignored) andtests/postgres/migrate.rs(4 passed).
TLS doesn't need a hook: a caller whose host upgrades the socket itself (Workers' startTls()) can send the SSLRequest, upgrade, and pass the result in with ssl_mode = Disable.
If this direction is acceptable, I'll open the PR from that branch. The same shape would work for MySQL (MySqlConnection::connect_with_socket), which Hyperdrive also supports; I've kept this to Postgres to start.
Is this a breaking change? Why or why not?
No. Both changes only add public items (PgConnection::connect_with_socket, sqlx_core::rt::Instant). sqlx_core::rt::Instant is std::time::Instant on every target except wasm32-unknown-unknown, where Instant::now() panicked before.
Source: transact-rs/sqlx