Lines
80 %
Functions
75 %
Branches
100 %
//! A pool whose only connection is held, so the next acquire can only time out.
use sqlx::pool::PoolConnection;
use sqlx::postgres::PgPoolOptions;
use sqlx::{PgPool, Postgres};
use std::time::Duration;
const WARM_DEADLINE: Duration = Duration::from_secs(60);
const WARM_POLL: Duration = Duration::from_millis(5);
/// A pool of one connection and the holder of that connection.
///
/// The connection is opened by the pool's maintenance task (spawned at once only without
/// idle and lifetime timers), outside the tiny
/// `acquire_timeout`, so a loaded host cannot fail the setup.
/// # Errors
/// [`sqlx::Error`] when the connection is not idle within `WARM_DEADLINE`.
pub async fn exhausted(
like: &PgPool,
acquire_timeout: Duration,
) -> Result<(PgPool, PoolConnection<Postgres>), sqlx::Error> {
let pool = PgPoolOptions::new()
.max_connections(1)
.min_connections(1)
.test_before_acquire(false)
.idle_timeout(None)
.max_lifetime(None)
.acquire_timeout(acquire_timeout)
.connect_lazy_with((*like.connect_options()).clone());
tokio::time::timeout(WARM_DEADLINE, async {
while pool.num_idle() == 0 {
tokio::time::sleep(WARM_POLL).await;
}
})
.await
.map_err(|_| {
sqlx::Error::Protocol(format!(
"the pool did not open its connection within {WARM_DEADLINE:?}"
))
})?;
let held = pool.acquire().await?;
Ok((pool, held))