diff --git a/DEPLOY.md b/DEPLOY.md index 7028648..60650f6 100644 --- a/DEPLOY.md +++ b/DEPLOY.md @@ -111,26 +111,8 @@ trusted_servers = ["matrix.org"] address = "127.0.0.1" # This makes sure Conduit can only be reached using the reverse proxy -## sqlite - -# The amount of memory that the database will use, with the following formula; -# (db_cache_capacity * (sqlite_read_pool_size + 1)), in bytes -#db_cache_capacity = 1073741824 # in bytes, 1024 * 1024 * 1024 - -# How many permanent read connections will be open to the database, -# increase this if you see "creating spillover reader" in your logs. -#sqlite_read_pool_size = 2 # default: max(cpu core count, 1) - -# If the database WAL (conduit.db-wal file) should be cleaned on a timer. -#sqlite_wal_clean_timer = false - -# How many seconds should pass before the WAL clean task should fire. -# Note: Is dependant on sqlite_wal_clean_timer being true. -#sqlite_wal_clean_second_interval = 60 - -# How long the WAL clean task should (in seconds) try to wait while -# getting exclusive access to the database (before giving up). -#sqlite_wal_clean_second_timeout = 2 +# The total amount of memory that the database will use. +#db_cache_capacity_mb = 10 ``` ## Setting the correct file permissions diff --git a/conduit-example.toml b/conduit-example.toml index cbcc58c..6fb4d5f 100644 --- a/conduit-example.toml +++ b/conduit-example.toml @@ -43,23 +43,5 @@ address = "127.0.0.1" # This makes sure Conduit can only be reached using the re proxy = "none" # more examples can be found at src/database/proxy.rs:6 -## sqlite - -# The amount of memory that the database will use, with the following formula; -# (db_cache_capacity * (sqlite_read_pool_size + 1)), in bytes -#db_cache_capacity = 1073741824 # in bytes, 1024 * 1024 * 1024 - -# How many permanent read connections will be open to the database, -# increase this if you see "creating spillover reader" in your logs. -#sqlite_read_pool_size = 2 # default: max(cpu core count, 1) - -# If the database WAL (conduit.db-wal file) should be cleaned on a timer. -#sqlite_wal_clean_timer = false - -# How many seconds should pass before the WAL clean task should fire. -# Note: Is dependant on sqlite_wal_clean_timer being true. -#sqlite_wal_clean_second_interval = 60 - -# How long the WAL clean task should (in seconds) try to wait while -# getting exclusive access to the database (before giving up). -#sqlite_wal_clean_second_timeout = 2 \ No newline at end of file +# The total amount of memory that the database will use. +#db_cache_capacity_mb = 10 \ No newline at end of file diff --git a/debian/postinst b/debian/postinst index 5a05485..79b7e73 100644 --- a/debian/postinst +++ b/debian/postinst @@ -77,26 +77,8 @@ max_request_size = 20_000_000 # in bytes #log = "info,state_res=warn,rocket=off,_=off,sled=off" #workers = 4 # default: cpu core count * 2 -## sqlite - -# The amount of memory that the database will use, with the following formula; -# (db_cache_capacity * (sqlite_read_pool_size + 1)), in bytes -#db_cache_capacity = 1073741824 # in bytes, 1024 * 1024 * 1024 - -# How many permanent read connections will be open to the database, -# increase this if you see "creating spillover reader" in your logs. -#sqlite_read_pool_size = 2 # default: max(cpu core count, 1) - -# If the database WAL (conduit.db-wal file) should be cleaned on a timer. -#sqlite_wal_clean_timer = false - -# How many seconds should pass before the WAL clean task should fire. -# Note: Is dependant on sqlite_wal_clean_timer being true. -#sqlite_wal_clean_second_interval = 60 - -# How long the WAL clean task should (in seconds) try to wait while -# getting exclusive access to the database (before giving up). -#sqlite_wal_clean_second_timeout = 2 +# The total amount of memory that the database will use. +#db_cache_capacity_mb = 10 EOF fi ;; diff --git a/src/database.rs b/src/database.rs index 31deafd..c498841 100644 --- a/src/database.rs +++ b/src/database.rs @@ -43,8 +43,8 @@ use self::proxy::ProxyConfig; pub struct Config { server_name: Box, database_path: String, - #[serde(default = "default_db_cache_capacity")] - db_cache_capacity: u32, + #[serde(default = "default_db_cache_capacity_mb")] + db_cache_capacity_mb: f64, #[serde(default = "default_sqlite_read_pool_size")] sqlite_read_pool_size: usize, #[serde(default = "false_fn")] @@ -105,8 +105,8 @@ fn true_fn() -> bool { true } -fn default_db_cache_capacity() -> u32 { - 1024 * 1024 * 1024 +fn default_db_cache_capacity_mb() -> f64 { + 10.0 } fn default_sqlite_read_pool_size() -> usize { diff --git a/src/database/abstraction/sled.rs b/src/database/abstraction/sled.rs index bf5aa2b..271be1e 100644 --- a/src/database/abstraction/sled.rs +++ b/src/database/abstraction/sled.rs @@ -14,7 +14,7 @@ impl DatabaseEngine for Engine { Ok(Arc::new(Engine( sled::Config::default() .path(&config.database_path) - .cache_capacity(config.db_cache_capacity as u64) + .cache_capacity((config.db_cache_capacity_mb * 1024 * 1024) as u64) .use_compression(true) .open()?, ))) diff --git a/src/database/abstraction/sqlite.rs b/src/database/abstraction/sqlite.rs index d4ab9ad..22a5559 100644 --- a/src/database/abstraction/sqlite.rs +++ b/src/database/abstraction/sqlite.rs @@ -58,7 +58,13 @@ impl<'a> Deref for HoldingConn<'a> { } impl Pool { - fn new>(path: P, num_readers: usize, cache_size: u32) -> Result { + fn new>(path: P, num_readers: usize, total_cache_size_mb: f64) -> Result { + // calculates cache-size per permanent connection + // 1. convert MB to KiB + // 2. divide by permanent connections + // 3. round down to nearest integer + let cache_size: u32 = ((total_cache_size_mb * 1024.0) / (num_readers + 1) as f64) as u32; + let writer = Mutex::new(Self::prepare_conn(&path, Some(cache_size))?); let mut readers = Vec::new(); @@ -128,7 +134,7 @@ impl DatabaseEngine for Engine { let pool = Pool::new( Path::new(&config.database_path).join("conduit.db"), config.sqlite_read_pool_size, - config.db_cache_capacity / 1024, // bytes -> kb + config.db_cache_capacity_mb, )?; pool.write_lock()