From 0926739d3cf18cca77e75278a708dfa88560dd59 Mon Sep 17 00:00:00 2001 From: asonix Date: Sun, 24 Sep 2023 15:32:00 -0500 Subject: [PATCH] Remove once_cell --- Cargo.lock | 1 - Cargo.toml | 1 - src/generate.rs | 2 +- src/ingest.rs | 2 +- src/lib.rs | 23 +++++++++++++---------- src/tmp_file.rs | 23 +++++++++++++---------- 6 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4741660..33e4a68 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1827,7 +1827,6 @@ dependencies = [ "metrics", "metrics-exporter-prometheus", "mime", - "once_cell", "opentelemetry", "opentelemetry-otlp", "pin-project-lite", diff --git a/Cargo.toml b/Cargo.toml index bea4346..4d21f23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,6 @@ md-5 = "0.10.5" metrics = "0.21.1" metrics-exporter-prometheus = { version = "0.12.1", default-features = false, features = ["http-listener"] } mime = "0.3.1" -once_cell = "1.4.0" opentelemetry = { version = "0.20", features = ["rt-tokio"] } opentelemetry-otlp = "0.13" pin-project-lite = "0.2.7" diff --git a/src/generate.rs b/src/generate.rs index 318ee20..33c8d8a 100644 --- a/src/generate.rs +++ b/src/generate.rs @@ -84,7 +84,7 @@ async fn process( hash: Hash, ) -> Result<(Details, Bytes), Error> { let guard = MetricsGuard::guard(); - let permit = crate::PROCESS_SEMAPHORE.acquire().await; + let permit = crate::process_semaphore().acquire().await?; let identifier = input_identifier( repo, diff --git a/src/ingest.rs b/src/ingest.rs index a55e267..b3b168b 100644 --- a/src/ingest.rs +++ b/src/ingest.rs @@ -57,7 +57,7 @@ pub(crate) async fn ingest( where S: Store, { - let permit = crate::PROCESS_SEMAPHORE.acquire().await; + let permit = crate::process_semaphore().acquire().await?; let bytes = aggregate(stream).await?; diff --git a/src/lib.rs b/src/lib.rs index c121177..b4da77d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,7 +43,6 @@ use future::WithTimeout; use futures_core::Stream; use metrics_exporter_prometheus::PrometheusBuilder; use middleware::Metrics; -use once_cell::sync::Lazy; use repo::ArcRepo; use reqwest_middleware::{ClientBuilder, ClientWithMiddleware}; use reqwest_tracing::TracingMiddleware; @@ -52,7 +51,7 @@ use std::{ marker::PhantomData, path::Path, path::PathBuf, - sync::Arc, + sync::{Arc, OnceLock}, time::{Duration, SystemTime}, }; use streem::IntoStreamer; @@ -88,15 +87,19 @@ const DAYS: u32 = 24 * HOURS; const NOT_FOUND_KEY: &str = "404-alias"; -static PROCESS_SEMAPHORE: Lazy = Lazy::new(|| { - let permits = std::thread::available_parallelism() - .map(usize::from) - .unwrap_or(1) - .saturating_sub(1) - .max(1); +static PROCESS_SEMAPHORE: OnceLock = OnceLock::new(); - crate::sync::bare_semaphore(permits) -}); +fn process_semaphore() -> &'static Semaphore { + PROCESS_SEMAPHORE.get_or_init(|| { + let permits = std::thread::available_parallelism() + .map(usize::from) + .unwrap_or(1) + .saturating_sub(1) + .max(1); + + crate::sync::bare_semaphore(permits) + }) +} async fn ensure_details( repo: &ArcRepo, diff --git a/src/tmp_file.rs b/src/tmp_file.rs index edcb861..f9e3d3b 100644 --- a/src/tmp_file.rs +++ b/src/tmp_file.rs @@ -1,13 +1,16 @@ -use once_cell::sync::Lazy; -use std::path::PathBuf; +use std::{path::PathBuf, sync::OnceLock}; use tokio::io::AsyncRead; use uuid::Uuid; -static TMP_DIR: Lazy = Lazy::new(|| { - let dir = std::env::temp_dir().join(Uuid::new_v4().to_string()); - std::fs::create_dir(&dir).unwrap(); - dir -}); +static TMP_DIR: OnceLock = OnceLock::new(); + +fn tmp_dir() -> &'static PathBuf { + TMP_DIR.get_or_init(|| { + let dir = std::env::temp_dir().join(Uuid::new_v4().to_string()); + std::fs::create_dir(&dir).unwrap(); + dir + }) +} struct TmpFile(PathBuf); @@ -28,14 +31,14 @@ pin_project_lite::pin_project! { pub(crate) fn tmp_file(ext: Option<&str>) -> PathBuf { if let Some(ext) = ext { - TMP_DIR.join(format!("{}{}", Uuid::new_v4(), ext)) + tmp_dir().join(format!("{}{}", Uuid::new_v4(), ext)) } else { - TMP_DIR.join(Uuid::new_v4().to_string()) + tmp_dir().join(Uuid::new_v4().to_string()) } } pub(crate) async fn remove_tmp_dir() -> std::io::Result<()> { - tokio::fs::remove_dir_all(&*TMP_DIR).await + tokio::fs::remove_dir_all(tmp_dir()).await } pub(crate) fn cleanup_tmpfile(reader: R, file: PathBuf) -> TmpFileCleanup {