Add before_acquire passthrough and idle-ping shorthand to ConnectOptions#3143
Merged
Conversation
Expose SQLx's `before_acquire` pool hook, which SeaORM previously only
reachable through the raw `map_sqlx_<db>_pool_opts` escape hatch.
- `test_before_acquire_if_idle_for(Duration)`: DB-agnostic shorthand that
pings a pooled connection before handing it out only once it has been idle
for at least the given duration, instead of on every acquire. Sets
`test_before_acquire(false)`.
- `map_sqlx_{postgres,mysql,sqlite}_before_acquire`: per-backend passthrough of
SQLx's `before_acquire` callback.
- `get_test_before_acquire` / `get_test_before_acquire_if_idle_for` getters.
Because SQLx's single `before_acquire` slot replaces rather than composes and
has no getter, a generic `apply_before_acquire` helper composes the idle-ping
shorthand with any user-provided callback into one closure (idle-ping first,
then the user callback). The helper returns the pool options untouched when
neither option is set, so existing behavior is unchanged.
Purely additive: no existing signature, default, or behavior is changed.
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.
What
Adds first-class access to SQLx's
before_acquirepool hook, plus a shorthand for the common "only ping stale connections" pattern. Previouslybefore_acquirewas reachable only through the rawmap_sqlx_<db>_pool_optsescape hatch, where you had to hand-write the idle-ping logic and couldn't compose with anything SeaORM set.API (all additive)
ConnectOptions::test_before_acquire_if_idle_for(Duration)— DB-agnostic shorthand. Pings a pooled connection before handing it out only once it has been idle for at least the given duration, instead of on every acquire (test_before_acquire). Calling it setstest_before_acquire(false).map_sqlx_postgres_before_acquire/map_sqlx_mysql_before_acquire/map_sqlx_sqlite_before_acquire— per-backend passthrough of SQLx'sbefore_acquirecallback (per-DB because it operates on the raw&mut DB::Connection).get_test_before_acquire/get_test_before_acquire_if_idle_forgetters.How it composes
SQLx's
before_acquireslot replaces rather than composes and offers no getter to read it back, so when both the shorthand and a user callback are set they cannot be combined by the caller. A genericConnectOptions::apply_before_acquire<DB>helper composes them into one closure — idle-ping first, then the user callback — and is called once per driver with that backend's concrete callback. When neither option is set it returns the pool options untouched, so pools built by existing users are byte-for-byte unchanged.The shorthand alone reduces exactly to:
Non-breaking
No existing signature, default, or behavior is changed, renamed, or removed. The per-driver insertion is a no-op unless the new options are opted into.
Limitations
Applies only to pools built via
Database::connect. Thefrom_sqlx_<db>_poolconstructors adopt a pre-built pool and bypassConnectOptionsentirely; configurebefore_acquireon your ownPoolOptionsthere.Tests
sqlx_common.rs(gated tosqlx-postgres).sqlx-postgres,sqlx-mysql,sqlx-sqlite,sqlx-all, and--no-default-features; clippy and rustfmt clean on the changed files.