expression
Connection pool metrics: size and idle count
Size and idle counts are instantaneous observations. They may change independently between reads, so a derived active count is only an approximation. Use saturating subtraction for a non-atomic sample and do not assert exact counts during concurrent activity. Uses a disposable in-memory SQLite database, with no external database. Expected stdout (not executed): sample within configured bound Common mistake: Interpreting two separate metric reads as an atomic snapshot.
Formal Syntax & Structure
rust syntaxrust
let size = pool.size(); let idle = pool.num_idle();Working Example
Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() -> Result<(), Box<dyn std::error::Error>> { let pool = sqlx::sqlite::SqlitePoolOptions::new().max_connections(1).connect("sqlite::memory:").await?; let size = pool.size() as usize; let idle = pool.num_idle(); let busy_estimate = size.saturating_sub(idle); assert!(busy_estimate <= 1); println!("sample within configured bound"); pool.close().await; Ok(())}