Skip to content

Commit

Permalink
Avoid trying to fetch the same project in parallel with a map of mutexes
Browse files Browse the repository at this point in the history
  • Loading branch information
kylewlacy committed Sep 27, 2024
1 parent 3ad419a commit b3de44c
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions crates/brioche-core/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,17 @@ async fn fetch_project_from_registry(
brioche: &Brioche,
project_hash: ProjectHash,
) -> anyhow::Result<PathBuf> {
// Use a mutex to ensure we don't try to fetch the same project more
// than once at a time
static FETCH_PROJECTS_MUTEX: tokio::sync::Mutex<
BTreeMap<ProjectHash, Arc<tokio::sync::Mutex<()>>>,
> = tokio::sync::Mutex::const_new(BTreeMap::new());
let project_mutex = {
let mut fetch_projects = FETCH_PROJECTS_MUTEX.lock().await;
fetch_projects.entry(project_hash).or_default().clone()
};
let _guard = project_mutex.lock().await;

let local_path = brioche.home.join("projects").join(project_hash.to_string());

if tokio::fs::try_exists(&local_path).await? {
Expand Down

0 comments on commit b3de44c

Please sign in to comment.