Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions src/storage_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use log::debug;
use rand::{thread_rng, Rng};
use sqlx::any::AnyConnectOptions;
use sqlx::{query_as, Any, AnyPool, FromRow};
use std::time::Instant;
use std::{sync::RwLock, time::Instant};
use tokio::time::Duration;

#[derive(Debug, Clone, FromRow)]
Expand All @@ -27,6 +27,7 @@ pub struct UserStorageAccess {
struct CachedAccess {
access: Vec<UserStorageAccess>,
valid_till: Instant,
updating: RwLock<bool>,
}

impl CachedAccess {
Expand All @@ -36,11 +37,18 @@ impl CachedAccess {
access,
valid_till: Instant::now()
+ Duration::from_millis(rng.gen_range((4 * 60 * 1000)..(5 * 60 * 1000))),
updating: RwLock::new(false),
}
}

pub fn is_valid(&self) -> bool {
self.valid_till > Instant::now()
self.valid_till > Instant::now() || self.updating.try_read().is_ok_and(|value| *value)
}

pub fn prepare_update(&self, value: bool) {
if let Ok(mut updating) = self.updating.try_write() {
*updating = value
}
}
}

Expand Down Expand Up @@ -71,14 +79,27 @@ impl StorageMapping {
&self,
storage: u32,
) -> Result<Ref<'_, u32, CachedAccess>, DatabaseError> {
if let Some(cached) = self.cache.get(&storage).filter(|cached| cached.is_valid()) {
Ok(cached)
} else {
let users = self.load_storage_mapping(storage).await?;
if let Some(cached) = self.cache.get(&storage) {
if cached.is_valid() {
return Ok(cached);
}

self.cache.insert(storage, CachedAccess::new(users));
Ok(self.cache.get(&storage).unwrap())
cached.prepare_update(true);
let users = self
.load_storage_mapping(storage)
.await
.inspect_err(|_| cached.prepare_update(false))?;

drop(cached);
let cached = CachedAccess::new(users);
self.cache.insert(storage, cached);
return Ok(self.cache.get(&storage).unwrap());
}

let users = self.load_storage_mapping(storage).await?;

self.cache.insert(storage, CachedAccess::new(users));
Ok(self.cache.get(&storage).unwrap())
}

pub async fn get_users_for_storage_path(
Expand Down
Loading