Repository navigation
Conversation
`std::time::Instant::now()` panics on `wasm32-unknown-unknown` ("time not
implemented on this platform"), and `QueryLogger::new` calls it for every
query, so no driver can run a query there, even over a transport the host
provides.
Add `sqlx_core::rt::Instant`: `std::time::Instant` everywhere else, and
`web_time::Instant`, which reads the host's `performance.now()`, on that
target. Use it in the query logger and the Postgres, MySQL and SQLite
migrators. `web-time` is only a dependency on that target; nothing changes
elsewhere.
sqlx opens its own sockets through `sqlx_core::net::connect_tcp`, which needs a supported async runtime; without one it panics in `missing_rt`. Hosts that hand out their own socket type, such as Cloudflare Workers (`wasm32-unknown-unknown`, with `connect()` sockets), have no way to connect. `connect_with_socket` takes any `sqlx_core::net::Socket` the caller has already connected. The host, port and socket path in the options are ignored; everything else, including `ssl_mode`, applies as it does for `connect_with`. `establish` now goes through the same path after opening its own socket.
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.
Does your PR solve an issue?
fixes #4426
This is option 1 from the discussion there: socket-only, Postgres only. A caller-supplied TLS upgrade, which a MySQL
connect_with_socketwould need, is left for a follow-up if this direction is accepted.Two commits, each usable on its own. Happy to split them into separate PRs if that's easier to review.
fix(core): use a wasm-safe Instant on wasm32-unknown-unknown.QueryLogger::newcallsstd::time::Instant::now()for every query, and that panics onwasm32-unknown-unknownwithtime not implemented on this platform. This addssqlx_core::rt::Instant, which isstd::time::Instanteverywhere else andweb_time::Instant(the host'sperformance.now()) on that target. It's used in the query logger and the Postgres, MySQL and SQLite migrators.web-timeis a dependency on that target only.feat(postgres): add PgConnection::connect_with_socket. This takes 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.The motivating host is Cloudflare Workers, which hands out its own TCP sockets (e.g. to reach Postgres through Hyperdrive) and has no tokio. TLS doesn't need a hook for Postgres: a caller whose host upgrades the socket itself can send the
SSLRequest, upgrade, and pass the result in withssl_mode = Disable.Tests
it_connects_with_socketintests/postgres/postgres.rs: a tokioTcpStreamopened by the test, thenconnect_with_socket.tests/postgres/postgres.rs(58 passed, 2 ignored) andtests/postgres/migrate.rs(4 passed).wasm32-unknown-unknown, a Worker underwrangler dev --localagainst Postgres 17 with SCRAM auth: connect, bound parameters and scalar types, 2,000 rows over many socket reads, unique-violation mapping, rollback and commit, prepared-statement reuse,sqlx::migrate!(), ping and close. Reverting only theInstantcommit makes the same Worker panic.A downstream crate built on this branch, with those scenarios in CI: sqlx-contrib/sqlx-cloudflare#16 (
sqlx-cloudflare-hd).Is this a breaking change?
No. Both changes only add public items (
PgConnection::connect_with_socket,sqlx_core::rt::Instant).sqlx_core::rt::Instantisstd::time::Instanton every target exceptwasm32-unknown-unknown, whereInstant::now()panicked before. Existingconnect_withbehaviour is unchanged; it now delegates to the same code path after opening its socket.