commit e48bae10e68c536bdf6fe9d38771f592abe01588
parent 8774416b925a07b63e2d712d7f415d7c27ab04a0
Author: Greg Heartsfield <scsibug@imap.cc>
Date: Tue, 6 Sep 2022 06:06:01 -0500
feat: support in-memory SQLite database
Diffstat:
3 files changed, 20 insertions(+), 6 deletions(-)
diff --git 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
@@ -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
@@ -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))