move storage logic into the storage module

This commit is contained in:
Quentin 2023-12-21 15:36:05 +01:00
parent a3a9f87d2c
commit e9aabe8e82
Signed by: quentin
GPG key ID: E9602264D639FF68
2 changed files with 20 additions and 16 deletions

View file

@ -24,7 +24,7 @@ pub struct UserDatabase {
pub struct StaticLoginProvider { pub struct StaticLoginProvider {
user_db: watch::Receiver<UserDatabase>, user_db: watch::Receiver<UserDatabase>,
in_memory_store: tokio::sync::Mutex<HashMap<String, Arc<storage::in_memory::MemBuilder>>>, in_memory_store: storage::in_memory::MemDb,
} }
pub async fn update_user_list(config: PathBuf, up: watch::Sender<UserDatabase>) -> Result<()> { pub async fn update_user_list(config: PathBuf, up: watch::Sender<UserDatabase>) -> Result<()> {
@ -71,7 +71,7 @@ impl StaticLoginProvider {
tokio::spawn(update_user_list(config.user_list, tx)); tokio::spawn(update_user_list(config.user_list, tx));
rx.changed().await?; rx.changed().await?;
Ok(Self { user_db: rx, in_memory_store: tokio::sync::Mutex::new(HashMap::new()) }) Ok(Self { user_db: rx, in_memory_store: storage::in_memory::MemDb::new() })
} }
} }
@ -94,13 +94,7 @@ impl LoginProvider for StaticLoginProvider {
tracing::debug!(user=%username, "fetch keys"); tracing::debug!(user=%username, "fetch keys");
let storage: storage::Builder = match &user.config.storage { let storage: storage::Builder = match &user.config.storage {
StaticStorage::InMemory => { StaticStorage::InMemory => self.in_memory_store.builder(username).await,
let mut global_storage = self.in_memory_store.lock().await;
global_storage
.entry(username.to_string())
.or_insert(storage::in_memory::MemBuilder::new(username))
.clone()
},
StaticStorage::Garage(grgconf) => storage::garage::GarageBuilder::new(storage::garage::GarageConf { StaticStorage::Garage(grgconf) => storage::garage::GarageBuilder::new(storage::garage::GarageConf {
region: grgconf.aws_region.clone(), region: grgconf.aws_region.clone(),
k2v_endpoint: grgconf.k2v_endpoint.clone(), k2v_endpoint: grgconf.k2v_endpoint.clone(),
@ -129,13 +123,7 @@ impl LoginProvider for StaticLoginProvider {
tracing::debug!(user=%user.username, "public_login"); tracing::debug!(user=%user.username, "public_login");
let storage: storage::Builder = match &user.config.storage { let storage: storage::Builder = match &user.config.storage {
StaticStorage::InMemory => { StaticStorage::InMemory => self.in_memory_store.builder(&user.username).await,
let mut global_storage = self.in_memory_store.lock().await;
global_storage
.entry(user.username.to_string())
.or_insert(storage::in_memory::MemBuilder::new(&user.username))
.clone()
},
StaticStorage::Garage(grgconf) => storage::garage::GarageBuilder::new(storage::garage::GarageConf { StaticStorage::Garage(grgconf) => storage::garage::GarageBuilder::new(storage::garage::GarageConf {
region: grgconf.aws_region.clone(), region: grgconf.aws_region.clone(),
k2v_endpoint: grgconf.k2v_endpoint.clone(), k2v_endpoint: grgconf.k2v_endpoint.clone(),

View file

@ -9,6 +9,22 @@ use tokio::sync::Notify;
/// It means that when a user disconnects, its data are lost. /// It means that when a user disconnects, its data are lost.
/// It's intended only for basic debugging, do not use it for advanced tests... /// It's intended only for basic debugging, do not use it for advanced tests...
#[derive(Debug, Default)]
pub struct MemDb(tokio::sync::Mutex<HashMap<String, Arc<MemBuilder>>>);
impl MemDb {
pub fn new() -> Self {
Self(tokio::sync::Mutex::new(HashMap::new()))
}
pub async fn builder(&self, username: &str) -> Arc<MemBuilder> {
let mut global_storage = self.0.lock().await;
global_storage
.entry(username.to_string())
.or_insert(MemBuilder::new(username))
.clone()
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
enum InternalData { enum InternalData {
Tombstone, Tombstone,