From 8899ce5089091e21eae088da692468565401abba Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Fri, 19 Jan 2024 15:45:42 +0000 Subject: fix: add acquire timeout to sqlite database connection (#1590) * fix: add acquire timeout to sqlite database connection This should fix #1503 I wasn't able to trigger enough IO pressure for the SQL connection to be a problem. This adds `local_timeout` to the client config. This is a float, and represents the number of seconds (units in line with the other timeouts, though those are ints). Users may well want to reduce this if they regularly have issues, but by default I think 2s is fine and avoids a non-responsive system in bad situations. * tests --- atuin-client/src/database.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'atuin-client/src/database.rs') diff --git a/atuin-client/src/database.rs b/atuin-client/src/database.rs index 05ff559a..e61d6ed7 100644 --- a/atuin-client/src/database.rs +++ b/atuin-client/src/database.rs @@ -2,6 +2,7 @@ use std::{ env, path::{Path, PathBuf}, str::FromStr, + time::Duration, }; use async_trait::async_trait; @@ -123,7 +124,7 @@ pub struct Sqlite { } impl Sqlite { - pub async fn new(path: impl AsRef) -> Result { + pub async fn new(path: impl AsRef, timeout: f64) -> Result { let path = path.as_ref(); debug!("opening sqlite database at {:?}", path); @@ -138,7 +139,10 @@ impl Sqlite { .journal_mode(SqliteJournalMode::Wal) .create_if_missing(true); - let pool = SqlitePoolOptions::new().connect_with(opts).await?; + let pool = SqlitePoolOptions::new() + .acquire_timeout(Duration::from_secs_f64(timeout)) + .connect_with(opts) + .await?; Self::setup_db(&pool).await?; @@ -786,7 +790,7 @@ mod test { #[tokio::test(flavor = "multi_thread")] async fn test_search_prefix() { - let mut db = Sqlite::new("sqlite::memory:").await.unwrap(); + let mut db = Sqlite::new("sqlite::memory:", 0.1).await.unwrap(); new_history_item(&mut db, "ls /home/ellie").await.unwrap(); assert_search_eq(&db, SearchMode::Prefix, FilterMode::Global, "ls", 1) @@ -802,7 +806,7 @@ mod test { #[tokio::test(flavor = "multi_thread")] async fn test_search_fulltext() { - let mut db = Sqlite::new("sqlite::memory:").await.unwrap(); + let mut db = Sqlite::new("sqlite::memory:", 0.1).await.unwrap(); new_history_item(&mut db, "ls /home/ellie").await.unwrap(); assert_search_eq(&db, SearchMode::FullText, FilterMode::Global, "ls", 1) @@ -818,7 +822,7 @@ mod test { #[tokio::test(flavor = "multi_thread")] async fn test_search_fuzzy() { - let mut db = Sqlite::new("sqlite::memory:").await.unwrap(); + let mut db = Sqlite::new("sqlite::memory:", 0.1).await.unwrap(); new_history_item(&mut db, "ls /home/ellie").await.unwrap(); new_history_item(&mut db, "ls /home/frank").await.unwrap(); new_history_item(&mut db, "cd /home/Ellie").await.unwrap(); @@ -908,7 +912,7 @@ mod test { #[tokio::test(flavor = "multi_thread")] async fn test_search_reordered_fuzzy() { - let mut db = Sqlite::new("sqlite::memory:").await.unwrap(); + let mut db = Sqlite::new("sqlite::memory:", 0.1).await.unwrap(); // test ordering of results: we should choose the first, even though it happened longer ago. new_history_item(&mut db, "curl").await.unwrap(); @@ -942,7 +946,7 @@ mod test { git_root: None, }; - let mut db = Sqlite::new("sqlite::memory:").await.unwrap(); + let mut db = Sqlite::new("sqlite::memory:", 0.1).await.unwrap(); for _i in 1..10000 { new_history_item(&mut db, "i am a duplicated command") .await -- cgit v1.3.1