1use sqlx::pool::PoolConnection;
4use sqlx::postgres::PgPoolOptions;
5use sqlx::{PgPool, Postgres};
6use std::time::Duration;
7
8const WARM_DEADLINE: Duration = Duration::from_secs(60);
9const WARM_POLL: Duration = Duration::from_millis(5);
10
11pub async fn exhausted(
20 like: &PgPool,
21 acquire_timeout: Duration,
22) -> Result<(PgPool, PoolConnection<Postgres>), sqlx::Error> {
23 let pool = PgPoolOptions::new()
24 .max_connections(1)
25 .min_connections(1)
26 .test_before_acquire(false)
27 .idle_timeout(None)
28 .max_lifetime(None)
29 .acquire_timeout(acquire_timeout)
30 .connect_lazy_with((*like.connect_options()).clone());
31 tokio::time::timeout(WARM_DEADLINE, async {
32 while pool.num_idle() == 0 {
33 tokio::time::sleep(WARM_POLL).await;
34 }
35 })
36 .await
37 .map_err(|_| {
38 sqlx::Error::Protocol(format!(
39 "the pool did not open its connection within {WARM_DEADLINE:?}"
40 ))
41 })?;
42 let held = pool.acquire().await?;
43 Ok((pool, held))
44}