From c04c0a9fc6a45435cc3f4a0b016eb455793b3fcc Mon Sep 17 00:00:00 2001 From: Vladislav Ivanov Date: Fri, 29 Sep 2023 22:36:41 +0200 Subject: [PATCH 1/2] Upgrade Rust edition All crates in workspace are now 2021 Rust commit-id:e6fac661 --- hyper_cgi/Cargo.toml | 2 +- josh-core/Cargo.toml | 2 +- josh-filter/Cargo.toml | 2 +- josh-proxy/Cargo.toml | 2 +- josh-proxy/src/lib.rs | 1 - josh-rpc/Cargo.toml | 2 +- josh-ssh-shell/Cargo.toml | 2 +- 7 files changed, 6 insertions(+), 7 deletions(-) diff --git a/hyper_cgi/Cargo.toml b/hyper_cgi/Cargo.toml index f5e3c4fbe..3b05902be 100644 --- a/hyper_cgi/Cargo.toml +++ b/hyper_cgi/Cargo.toml @@ -2,7 +2,7 @@ name = "hyper_cgi" version = "22.10.25" authors = ["Christian Schilling ", "Louis-Marie Givel "] -edition = "2018" +edition = "2021" license-file = "LICENSE" description = "Run CGI scripts with hyper" repository = "https://github.com/josh-project/josh" diff --git a/josh-core/Cargo.toml b/josh-core/Cargo.toml index ccffeada9..2e8d64981 100644 --- a/josh-core/Cargo.toml +++ b/josh-core/Cargo.toml @@ -7,7 +7,7 @@ license-file = "LICENSE" description = "GIT virtualization proxy" keywords = ["git", "monorepo", "workflow", "scm"] readme = "README.md" -edition = "2018" +edition = "2021" [dependencies] backtrace = "0.3.69" diff --git a/josh-filter/Cargo.toml b/josh-filter/Cargo.toml index 30f76a08a..84a9687ac 100644 --- a/josh-filter/Cargo.toml +++ b/josh-filter/Cargo.toml @@ -1,7 +1,7 @@ [package] authors = ["Christian Schilling "] description = "GIT filter cli" -edition = "2018" +edition = "2021" keywords = ["git", "monorepo", "workflow", "scm"] license-file = "LICENSE" name = "josh-filter" diff --git a/josh-proxy/Cargo.toml b/josh-proxy/Cargo.toml index 62141572e..5dd0c44d9 100644 --- a/josh-proxy/Cargo.toml +++ b/josh-proxy/Cargo.toml @@ -1,7 +1,7 @@ [package] authors = ["Christian Schilling "] description = "GIT virtualization proxy" -edition = "2018" +edition = "2021" keywords = ["git", "monorepo", "workflow", "scm"] license-file = "LICENSE" name = "josh-proxy" diff --git a/josh-proxy/src/lib.rs b/josh-proxy/src/lib.rs index 556955732..029c92586 100644 --- a/josh-proxy/src/lib.rs +++ b/josh-proxy/src/lib.rs @@ -510,7 +510,6 @@ fn create_repo_base(path: &PathBuf) -> josh::JoshResult { .cloned() .try_for_each(|(key, value)| -> JoshResult<()> { use gix::config::parse::section::Key; - use std::convert::TryFrom; let key = Key::try_from(key) .map_err(|_| josh_error("unable to create config section"))?; diff --git a/josh-rpc/Cargo.toml b/josh-rpc/Cargo.toml index 1292845da..a6acd43bd 100644 --- a/josh-rpc/Cargo.toml +++ b/josh-rpc/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "josh-rpc" version = "0.1.0" -edition = "2018" +edition = "2021" [dependencies] serde = { workspace = true } diff --git a/josh-ssh-shell/Cargo.toml b/josh-ssh-shell/Cargo.toml index ce57d5704..95502efb6 100644 --- a/josh-ssh-shell/Cargo.toml +++ b/josh-ssh-shell/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "josh-ssh-shell" version = "0.1.0" -edition = "2018" +edition = "2021" [dependencies] clap = { workspace = true } From e542b98f76445031bc28e6098bae1202c1031531 Mon Sep 17 00:00:00 2001 From: Vladislav Ivanov Date: Fri, 29 Sep 2023 23:08:58 +0200 Subject: [PATCH 2/2] Fix check-then-act race condition The semaphore is aquired after the conditions for fetching are already evaluated. It means that multiple threads/runners can decide to fetch simultaneously and then wait on the same semaphore, even though only one fetch is needed. Therefore, fetch would happen more often than what is specified in fetch times, and requests can be slow under load. commit-id:a4cd58e6 --- josh-proxy/src/bin/josh-proxy.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/josh-proxy/src/bin/josh-proxy.rs b/josh-proxy/src/bin/josh-proxy.rs index 4e387b45d..6fac078c4 100644 --- a/josh-proxy/src/bin/josh-proxy.rs +++ b/josh-proxy/src/bin/josh-proxy.rs @@ -129,6 +129,15 @@ async fn fetch_upstream( let refs_to_fetch: Vec<_> = refs_to_fetch.iter().map(|x| x.to_string()).collect(); + let us = upstream_repo.clone(); + let semaphore = service + .fetch_permits + .lock()? + .entry(us.clone()) + .or_insert(Arc::new(tokio::sync::Semaphore::new(1))) + .clone(); + let permit = semaphore.acquire().await; + let fetch_timer_ok = { if let Some(last) = service.fetch_timers.read()?.get(&key) { let since = std::time::Instant::now().duration_since(*last); @@ -183,15 +192,7 @@ async fn fetch_upstream( let br_path = service.repo_path.join("mirror"); let span = tracing::span!(tracing::Level::TRACE, "fetch worker"); - let us = upstream_repo.clone(); let ru = remote_url.clone(); - let semaphore = service - .fetch_permits - .lock()? - .entry(us.clone()) - .or_insert(Arc::new(tokio::sync::Semaphore::new(1))) - .clone(); - let permit = semaphore.acquire().await; let task_remote_auth = remote_auth.clone(); let fetch_result = tokio::task::spawn_blocking(move || { let _span_guard = span.enter();