expression

Database readiness: probe with a bounded query

A readiness check can verify that the application can obtain a connection and run a minimal query within a budget. A timeout is a failed check, not proof the server is down. Keep checks inexpensive and avoid sensitive detail in public responses. Uses a disposable in-memory SQLite database, with no external database. Expected stdout (not executed): ready: true Common mistake: Making readiness a costly business query or exposing the underlying error publicly.

Formal Syntax & Structure

rust syntaxrust
tokio::time::timeout(limit, sqlx::query_scalar::<_, i64>("SELECT 1").fetch_one(&pool))

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() -> Result<(), Box<dyn std::error::Error>> {    use std::time::Duration;    let pool = sqlx::sqlite::SqlitePoolOptions::new().max_connections(1).connect("sqlite::memory:").await?;    let result = tokio::time::timeout(Duration::from_secs(3), sqlx::query_scalar::<_, i64>("SELECT 1").fetch_one(&pool)).await;    let ready = matches!(result, Ok(Ok(1)));    assert!(ready);    println!("ready: {ready}");    pool.close().await;    Ok(())}
Rust Reference