From c35c5c9c6d68abaeab7c2614226f464831bfe06f Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Wed, 17 Jul 2024 20:28:46 +0200 Subject: [PATCH] wip --- src/storage/mod.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/storage/mod.rs b/src/storage/mod.rs index b6a770feb..65bf83675 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -16,12 +16,13 @@ use std::{ collections::{HashMap, HashSet}, ffi::OsStr, fmt, fs, + future::Future, io::{self, BufReader}, ops::RangeInclusive, path::{Path, PathBuf}, - sync::Arc, + sync::{Arc, Mutex}, }; -use tokio::{io::AsyncWriteExt, runtime::Runtime}; +use tokio::{io::AsyncWriteExt, runtime::Runtime, sync::RwLock}; use tracing::{error, info_span, instrument, trace}; type FileRange = RangeInclusive; @@ -111,6 +112,7 @@ enum StorageBackend { pub struct AsyncStorage { backend: StorageBackend, config: Arc, + archive_index_locks: Mutex>>, } impl AsyncStorage { @@ -121,6 +123,7 @@ impl AsyncStorage { ) -> Result { Ok(Self { config: config.clone(), + archive_index_locks: Mutex::new(HashMap::new()), backend: match config.storage_backend { StorageKind::Database => { StorageBackend::Database(DatabaseBackend::new(pool, metrics)) @@ -303,6 +306,22 @@ impl AsyncStorage { Ok(blob) } + // async fn with_archive_index_lock(&self, index_path: impl AsRef, f: F) + // where + // Fut: Future> + Send, + // F: Fn() -> Fut + Send + 'static, + // { + // let mut locks = self.archive_index_locks.lock().unwrap(); + + // if let Some(lock) = locks.get(index_path.as_ref()) { + // f(&lock); + // } else { + // f(&locks + // .entry(index_path.as_ref().to_path_buf()) + // .or_insert_with(|| RwLock::new(()))); + // } + // } + #[instrument] pub async fn download_archive_index( &self,