Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions crates/db/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<sqlx::Transaction<'static, sqlx::Postgres>> {
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()),
Expand Down
50 changes: 49 additions & 1 deletion crates/db/tests/begin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<F: Future + Unpin>(future: &mut F) -> Poll<F::Output> {
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();
}
5 changes: 5 additions & 0 deletions docs/changes/2.2.0/602.fixed.md
Original file line number Diff line number Diff line change
@@ -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.
Loading