diff --git a/crates/db/src/pool.rs b/crates/db/src/pool.rs index 98934a5a..0a64cb9a 100644 --- a/crates/db/src/pool.rs +++ b/crates/db/src/pool.rs @@ -56,10 +56,17 @@ fn acquire_time_level() -> log::LevelFilter { /// The `BEGIN` therefore runs in a task of its own, which finishes even when /// the caller is dropped; the transaction nobody took is then dropped, and /// rolled back, like any other. +/// +/// The connection is acquired here, in the caller, before that task starts: +/// dropping an acquire is safe, and a caller dropped while it waits for a +/// connection of a saturated pool leaves the pool's queue, instead of taking +/// a connection later for a `BEGIN` nobody waits for (#602). pub async fn begin(pool: &PgPool) -> sqlx::Result> { use tracing::Instrument as _; - let pool = pool.clone(); - let begin = async move { pool.begin().await }; + let connection = pool.acquire().await?; + // What `Pool::begin` does with the connection it acquired; sqlx has no + // other way to an owned (`'static`) transaction on a pool's connection. + let begin = sqlx::Transaction::begin(connection, None); match tokio::spawn(begin.in_current_span()).await { Ok(begun) => begun, Err(e) if e.is_panic() => std::panic::resume_unwind(e.into_panic()), diff --git a/crates/db/tests/begin.rs b/crates/db/tests/begin.rs index af0da57b..a30cdd3f 100644 --- a/crates/db/tests/begin.rs +++ b/crates/db/tests/begin.rs @@ -3,12 +3,18 @@ //! inside a transaction, as sqlx's own `Pool::begin` does. //! //! A relay between the pool and Postgres holds the answer back, so the caller -//! is dropped exactly then. Requires Docker (see `common`). +//! is dropped exactly then. +//! +//! A caller dropped earlier, while it waits for a connection of an exhausted +//! pool, gives up its place in the queue (#602). Requires Docker (see +//! `common`). mod common; use std::future::Future; +use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; +use std::task::Poll; use std::time::Duration; use sqlx::postgres::PgPoolOptions; @@ -138,3 +144,45 @@ async fn a_caller_dropped_mid_begin_leaves_no_transaction_open() { ); assert!(!leaves_a_transaction(|pool| async move { forge_db::begin(&pool).await }).await); } + +/// Poll `future` once. +async fn poll_once(future: &mut F) -> Poll { + std::future::poll_fn(|cx| Poll::Ready(std::pin::Pin::new(&mut *future).poll(cx))).await +} + +#[tokio::test] +async fn a_caller_dropped_while_the_pool_is_exhausted_gives_up_its_place() { + // One connection, and a count of the times the pool hands it out. + let handed_out = Arc::new(AtomicUsize::new(0)); + let counter = handed_out.clone(); + let pool = PgPoolOptions::new() + .max_connections(1) + .before_acquire(move |_conn, _meta| { + counter.fetch_add(1, Ordering::SeqCst); + Box::pin(async { Ok(true) }) + }) + .connect(&common::url().await) + .await + .unwrap(); + let held = pool.acquire().await.unwrap(); + let before = handed_out.load(Ordering::SeqCst); + + // Callers that queue for the connection, and are dropped there: polled + // once, each is in the pool's queue (which is first in, first out). + for _ in 0..3 { + let mut dropped = Box::pin(forge_db::begin(&pool)); + assert!(poll_once(&mut dropped).await.is_pending()); + } + // A caller that stays, queued behind where they were. + let mut live = Box::pin(forge_db::begin(&pool)); + assert!(poll_once(&mut live).await.is_pending()); + + drop(held); + let tx = tokio::time::timeout(Duration::from_secs(10), live) + .await + .expect("the caller that stayed gets the connection") + .unwrap(); + // The connection went to it alone, not to the dropped callers before it. + assert_eq!(handed_out.load(Ordering::SeqCst) - before, 1); + tx.rollback().await.unwrap(); +} diff --git a/docs/changes/2.2.0/602.fixed.md b/docs/changes/2.2.0/602.fixed.md new file mode 100644 index 00000000..1362d3e9 --- /dev/null +++ b/docs/changes/2.2.0/602.fixed.md @@ -0,0 +1,5 @@ +A request that was cancelled (its client went away, or its deadline passed) +while it waited for a database connection to begin a transaction kept its +place in the pool's queue, and later took a connection to begin a transaction +nobody used; under a saturated pool the requests still waiting queued behind +it, which stretched the overload. Such a request now leaves the queue at once.