Convert OnceCell to Lazy, fmt

This commit is contained in:
Aode (lion) 2021-09-06 12:08:13 -05:00
parent 2b878eee80
commit 0d5ce07b20
1 changed files with 12 additions and 12 deletions

View File

@ -7,8 +7,11 @@ use actix_web::{
}; };
use awc::Client; use awc::Client;
use dashmap::{mapref::entry::Entry, DashMap}; use dashmap::{mapref::entry::Entry, DashMap};
use futures_util::{stream::{LocalBoxStream, once}, Stream}; use futures_util::{
use once_cell::sync::{Lazy, OnceCell}; stream::{once, LocalBoxStream},
Stream,
};
use once_cell::sync::Lazy;
use std::{ use std::{
collections::HashSet, collections::HashSet,
future::{ready, Future}, future::{ready, Future},
@ -69,15 +72,12 @@ static TMP_DIR: Lazy<PathBuf> = Lazy::new(|| {
path path
}); });
static CONFIG: Lazy<Config> = Lazy::new(Config::from_args); static CONFIG: Lazy<Config> = Lazy::new(Config::from_args);
static PROCESS_SEMAPHORE: OnceCell<tokio::sync::Semaphore> = OnceCell::new(); static PROCESS_SEMAPHORE: Lazy<tokio::sync::Semaphore> = Lazy::new(|| tokio::sync::Semaphore::new(
num_cpus::get().saturating_sub(1).max(1),
));
static PROCESS_MAP: Lazy<DashMap<PathBuf, Vec<Sender<(Details, web::Bytes)>>>> = static PROCESS_MAP: Lazy<DashMap<PathBuf, Vec<Sender<(Details, web::Bytes)>>>> =
Lazy::new(DashMap::new); Lazy::new(DashMap::new);
fn process_semaphore() -> &'static tokio::sync::Semaphore {
PROCESS_SEMAPHORE
.get_or_init(|| tokio::sync::Semaphore::new(num_cpus::get().saturating_sub(1).max(1)))
}
struct CancelSafeProcessor<F> { struct CancelSafeProcessor<F> {
path: PathBuf, path: PathBuf,
receiver: Option<Receiver<(Details, web::Bytes)>>, receiver: Option<Receiver<(Details, web::Bytes)>>,
@ -329,7 +329,7 @@ async fn download(
let stream = Box::pin(once(fut)); let stream = Box::pin(once(fut));
let permit = process_semaphore().acquire().await?; let permit = PROCESS_SEMAPHORE.acquire().await?;
let alias = manager.upload(stream).await?; let alias = manager.upload(stream).await?;
drop(permit); drop(permit);
let delete_token = manager.delete_token(alias.clone()).await?; let delete_token = manager.delete_token(alias.clone()).await?;
@ -495,7 +495,7 @@ async fn process(
} }
} }
let permit = process_semaphore().acquire().await?; let permit = PROCESS_SEMAPHORE.acquire().await?;
let file = tokio::fs::File::open(original_path.clone()).await?; let file = tokio::fs::File::open(original_path.clone()).await?;
@ -774,7 +774,7 @@ async fn main() -> Result<(), anyhow::Error> {
let span = tracing::info_span!("file-upload", ?filename); let span = tracing::info_span!("file-upload", ?filename);
let entered = span.enter(); let entered = span.enter();
let permit = process_semaphore().acquire().await?; let permit = PROCESS_SEMAPHORE.acquire().await?;
let res = manager.upload(stream).await.map(|alias| { let res = manager.upload(stream).await.map(|alias| {
let mut path = PathBuf::new(); let mut path = PathBuf::new();
@ -807,7 +807,7 @@ async fn main() -> Result<(), anyhow::Error> {
let span = tracing::info_span!("file-import", ?filename); let span = tracing::info_span!("file-import", ?filename);
let entered = span.enter(); let entered = span.enter();
let permit = process_semaphore().acquire().await?; let permit = PROCESS_SEMAPHORE.acquire().await?;
let res = manager let res = manager
.import(filename, content_type, validate_imports, stream) .import(filename, content_type, validate_imports, stream)