diff --git a/config.toml b/config.toml index 03d1ae4..1f36df7 100644 --- a/config.toml +++ b/config.toml @@ -22,6 +22,11 @@ description = "A newly created nostr-rs-relay.\n\nCustomize this with your own i # line option. data_directory = "." + +# Use an in-memory database instead of 'nostr.db'. +# Caution; this will not survive a process restart! +#in_memory = false + # Database connection pool settings for subscribers: # Minimum number of SQLite reader connections diff --git a/src/config.rs b/src/config.rs index 09add90..900eb04 100644 --- a/src/config.rs +++ b/src/config.rs @@ -25,6 +25,7 @@ pub struct Info { #[allow(unused)] pub struct Database { pub data_directory: String, + pub in_memory: bool, pub min_conn: u32, pub max_conn: u32, } @@ -189,6 +190,7 @@ impl Default for Settings { }, database: Database { data_directory: ".".to_owned(), + in_memory: false, min_conn: 4, max_conn: 128, }, diff --git a/src/db.rs b/src/db.rs index e5d13d6..b00a0ac 100644 --- a/src/db.rs +++ b/src/db.rs @@ -54,13 +54,20 @@ pub fn build_pool( // small hack; if the database doesn't exist yet, that means the // writer thread hasn't finished. Give it a chance to work. This // is only an issue with the first time we run. - while !full_path.exists() && wait_for_db { - debug!("Database reader pool is waiting on the database to be created..."); - thread::sleep(Duration::from_millis(500)); + if !settings.database.in_memory { + while !full_path.exists() && wait_for_db { + debug!("Database reader pool is waiting on the database to be created..."); + thread::sleep(Duration::from_millis(500)); + } } - let manager = SqliteConnectionManager::file(&full_path) - .with_flags(flags) - .with_init(|c| c.execute_batch(STARTUP_SQL)); + let manager = match settings.database.in_memory { + true => SqliteConnectionManager::memory() + .with_flags(flags) + .with_init(|c| c.execute_batch(STARTUP_SQL)), + false => SqliteConnectionManager::file(&full_path) + .with_flags(flags) + .with_init(|c| c.execute_batch(STARTUP_SQL)), + }; let pool: SqlitePool = r2d2::Pool::builder() .test_on_check_out(true) // no noticeable performance hit .min_idle(Some(min_size))