mirror of
https://github.com/chatmail/core.git
synced 2026-04-19 14:36:29 +03:00
config_cache fixes (#3145)
* add simple backup export/import test this test fails on current master until the context is recrated. * avoid config_cache races adds needed SQL-statements to config_cache locking. otherwise, another thread may alter the database eg. between SELECT and the config_cache update - resulting in the wrong value being written to config_cache. * also update config_cache on initializing tables VERSION_CFG is also set later, however, not doing it here will result in bugs when we change DBVERSION at some point. as this alters only VERSION_CFG and that is executed sequentially anyway, race conditions between SQL and config_cache seems not to be an issue in this case. * clear config_cache after backup import import replaces the whole database, so config_cache needs to be invalidated as well. we do that before import, so in case a backup is imported only partly, the cache does not add additional problems. * update CHANGELOG
This commit is contained in:
@@ -48,7 +48,7 @@ pub struct Sql {
|
||||
/// open without a passphrase.
|
||||
is_encrypted: RwLock<Option<bool>>,
|
||||
|
||||
config_cache: RwLock<HashMap<String, Option<String>>>,
|
||||
pub(crate) config_cache: RwLock<HashMap<String, Option<String>>>,
|
||||
}
|
||||
|
||||
impl Sql {
|
||||
@@ -501,6 +501,7 @@ impl Sql {
|
||||
pub async fn set_raw_config(&self, key: impl AsRef<str>, value: Option<&str>) -> Result<()> {
|
||||
let key = key.as_ref();
|
||||
|
||||
let mut lock = self.config_cache.write().await;
|
||||
if let Some(value) = value {
|
||||
let exists = self
|
||||
.exists(
|
||||
@@ -526,8 +527,6 @@ impl Sql {
|
||||
self.execute("DELETE FROM config WHERE keyname=?;", paramsv![key])
|
||||
.await?;
|
||||
}
|
||||
|
||||
let mut lock = self.config_cache.write().await;
|
||||
lock.insert(key.to_string(), value.map(|s| s.to_string()));
|
||||
drop(lock);
|
||||
|
||||
@@ -544,6 +543,7 @@ impl Sql {
|
||||
return Ok(c);
|
||||
}
|
||||
|
||||
let mut lock = self.config_cache.write().await;
|
||||
let value = self
|
||||
.query_get_value(
|
||||
"SELECT value FROM config WHERE keyname=?;",
|
||||
@@ -551,8 +551,6 @@ impl Sql {
|
||||
)
|
||||
.await
|
||||
.context(format!("failed to fetch raw config: {}", key.as_ref()))?;
|
||||
|
||||
let mut lock = self.config_cache.write().await;
|
||||
lock.insert(key.as_ref().to_string(), value.clone());
|
||||
drop(lock);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user