From b3de44ca0459a620baeba4cca91eb3342a55d5e8 Mon Sep 17 00:00:00 2001 From: Kyle Lacy Date: Fri, 27 Sep 2024 01:25:26 -0700 Subject: [PATCH] Avoid trying to fetch the same project in parallel with a map of mutexes --- crates/brioche-core/src/project.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/brioche-core/src/project.rs b/crates/brioche-core/src/project.rs index 9b7feed..0aa799b 100644 --- a/crates/brioche-core/src/project.rs +++ b/crates/brioche-core/src/project.rs @@ -985,6 +985,17 @@ async fn fetch_project_from_registry( brioche: &Brioche, project_hash: ProjectHash, ) -> anyhow::Result { + // 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>>, + > = 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? {