expression

r2d2: bound the wait for a connection

An exhausted pool should not make callers wait indefinitely. Hold the only checkout, request another with a short timeout, then return the first. The fixture checks outcomes, not elapsed milliseconds. Checkout timeout is distinct from query execution timeout. Run in the accompanying Cargo project. Expected stdout (not executed): exhausted: true available: true Common mistake: Treating the checkout timeout as a deadline for the SQL query itself.

Formal Syntax & Structure

rust syntaxrust
pool.get_timeout(Duration::from_millis(20))

Working Example

Example Snippetrust
fn main() -> Result<(), Box<dyn std::error::Error>> {    use std::time::Duration;    use r2d2_sqlite::SqliteConnectionManager;    let pool = r2d2::Pool::builder().max_size(1).build(SqliteConnectionManager::memory())?;    let held = pool.get()?;    let waiting = pool.get_timeout(Duration::from_millis(20));    println!("exhausted: {}", waiting.is_err());    drop(held);    let _again = pool.get_timeout(Duration::from_secs(2))?;    println!("available: true");    Ok(())}
Rust Reference