Remove once_cell

This commit is contained in:
asonix 2023-09-24 15:32:00 -05:00
parent e0968dcce3
commit 0926739d3c
6 changed files with 28 additions and 24 deletions

1
Cargo.lock generated
View File

@ -1827,7 +1827,6 @@ dependencies = [
"metrics", "metrics",
"metrics-exporter-prometheus", "metrics-exporter-prometheus",
"mime", "mime",
"once_cell",
"opentelemetry", "opentelemetry",
"opentelemetry-otlp", "opentelemetry-otlp",
"pin-project-lite", "pin-project-lite",

View File

@ -38,7 +38,6 @@ md-5 = "0.10.5"
metrics = "0.21.1" metrics = "0.21.1"
metrics-exporter-prometheus = { version = "0.12.1", default-features = false, features = ["http-listener"] } metrics-exporter-prometheus = { version = "0.12.1", default-features = false, features = ["http-listener"] }
mime = "0.3.1" mime = "0.3.1"
once_cell = "1.4.0"
opentelemetry = { version = "0.20", features = ["rt-tokio"] } opentelemetry = { version = "0.20", features = ["rt-tokio"] }
opentelemetry-otlp = "0.13" opentelemetry-otlp = "0.13"
pin-project-lite = "0.2.7" pin-project-lite = "0.2.7"

View File

@ -84,7 +84,7 @@ async fn process<S: Store + 'static>(
hash: Hash, hash: Hash,
) -> Result<(Details, Bytes), Error> { ) -> Result<(Details, Bytes), Error> {
let guard = MetricsGuard::guard(); let guard = MetricsGuard::guard();
let permit = crate::PROCESS_SEMAPHORE.acquire().await; let permit = crate::process_semaphore().acquire().await?;
let identifier = input_identifier( let identifier = input_identifier(
repo, repo,

View File

@ -57,7 +57,7 @@ pub(crate) async fn ingest<S>(
where where
S: Store, S: Store,
{ {
let permit = crate::PROCESS_SEMAPHORE.acquire().await; let permit = crate::process_semaphore().acquire().await?;
let bytes = aggregate(stream).await?; let bytes = aggregate(stream).await?;

View File

@ -43,7 +43,6 @@ use future::WithTimeout;
use futures_core::Stream; use futures_core::Stream;
use metrics_exporter_prometheus::PrometheusBuilder; use metrics_exporter_prometheus::PrometheusBuilder;
use middleware::Metrics; use middleware::Metrics;
use once_cell::sync::Lazy;
use repo::ArcRepo; use repo::ArcRepo;
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware}; use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_tracing::TracingMiddleware; use reqwest_tracing::TracingMiddleware;
@ -52,7 +51,7 @@ use std::{
marker::PhantomData, marker::PhantomData,
path::Path, path::Path,
path::PathBuf, path::PathBuf,
sync::Arc, sync::{Arc, OnceLock},
time::{Duration, SystemTime}, time::{Duration, SystemTime},
}; };
use streem::IntoStreamer; use streem::IntoStreamer;
@ -88,7 +87,10 @@ const DAYS: u32 = 24 * HOURS;
const NOT_FOUND_KEY: &str = "404-alias"; const NOT_FOUND_KEY: &str = "404-alias";
static PROCESS_SEMAPHORE: Lazy<Semaphore> = Lazy::new(|| { static PROCESS_SEMAPHORE: OnceLock<Semaphore> = OnceLock::new();
fn process_semaphore() -> &'static Semaphore {
PROCESS_SEMAPHORE.get_or_init(|| {
let permits = std::thread::available_parallelism() let permits = std::thread::available_parallelism()
.map(usize::from) .map(usize::from)
.unwrap_or(1) .unwrap_or(1)
@ -96,7 +98,8 @@ static PROCESS_SEMAPHORE: Lazy<Semaphore> = Lazy::new(|| {
.max(1); .max(1);
crate::sync::bare_semaphore(permits) crate::sync::bare_semaphore(permits)
}); })
}
async fn ensure_details<S: Store + 'static>( async fn ensure_details<S: Store + 'static>(
repo: &ArcRepo, repo: &ArcRepo,

View File

@ -1,13 +1,16 @@
use once_cell::sync::Lazy; use std::{path::PathBuf, sync::OnceLock};
use std::path::PathBuf;
use tokio::io::AsyncRead; use tokio::io::AsyncRead;
use uuid::Uuid; use uuid::Uuid;
static TMP_DIR: Lazy<PathBuf> = Lazy::new(|| { static TMP_DIR: OnceLock<PathBuf> = OnceLock::new();
fn tmp_dir() -> &'static PathBuf {
TMP_DIR.get_or_init(|| {
let dir = std::env::temp_dir().join(Uuid::new_v4().to_string()); let dir = std::env::temp_dir().join(Uuid::new_v4().to_string());
std::fs::create_dir(&dir).unwrap(); std::fs::create_dir(&dir).unwrap();
dir dir
}); })
}
struct TmpFile(PathBuf); struct TmpFile(PathBuf);
@ -28,14 +31,14 @@ pin_project_lite::pin_project! {
pub(crate) fn tmp_file(ext: Option<&str>) -> PathBuf { pub(crate) fn tmp_file(ext: Option<&str>) -> PathBuf {
if let Some(ext) = ext { if let Some(ext) = ext {
TMP_DIR.join(format!("{}{}", Uuid::new_v4(), ext)) tmp_dir().join(format!("{}{}", Uuid::new_v4(), ext))
} else { } 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<()> { 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<R: AsyncRead>(reader: R, file: PathBuf) -> TmpFileCleanup<R> { pub(crate) fn cleanup_tmpfile<R: AsyncRead>(reader: R, file: PathBuf) -> TmpFileCleanup<R> {