Skip to content
Merged
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
24 changes: 14 additions & 10 deletions src/storage_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,28 @@ impl StorageMapping {
&self,
storage: u32,
) -> Result<Ref<'_, u32, CachedAccess>, DatabaseError> {
// Note: the `Ref` handed out by `DashMap::get` is a read guard on the map shard, and
// taking the write lock for `insert` blocks the calling *thread*, not just the task.
// Holding the guard across the `.await` below therefore parks any runtime worker that
// tries to insert into the same shard while this query is in flight; if that starves
// the runtime, nothing polls the reactor, the query never completes and the guard is
// never released. Keep every guard in this function strictly await-free.
if let Some(cached) = self.cache.get(&storage) {
if cached.is_valid() {
return Ok(cached);
}

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?;
let users = self
.load_storage_mapping(storage)
.await
.inspect_err(|_| {
if let Some(cached) = self.cache.get(&storage) {
cached.prepare_update(false);
}
})?;

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