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-exporter-prometheus",
"mime",
"once_cell",
"opentelemetry",
"opentelemetry-otlp",
"pin-project-lite",

View file

@ -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"

View file

@ -84,7 +84,7 @@ async fn process<S: Store + 'static>(
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,

View file

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

View file

@ -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<Semaphore> = Lazy::new(|| {
let permits = std::thread::available_parallelism()
.map(usize::from)
.unwrap_or(1)
.saturating_sub(1)
.max(1);
static PROCESS_SEMAPHORE: OnceLock<Semaphore> = 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<S: Store + 'static>(
repo: &ArcRepo,

View file

@ -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<PathBuf> = 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<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());
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<R: AsyncRead>(reader: R, file: PathBuf) -> TmpFileCleanup<R> {