diff --git a/src/cache/fs.rs b/src/cache/fs.rs index a81702a..e60de4d 100644 --- a/src/cache/fs.rs +++ b/src/cache/fs.rs @@ -9,10 +9,10 @@ use std::time::Duration; use bytes::{Bytes, BytesMut}; use futures::{Future, Stream, StreamExt}; use once_cell::sync::Lazy; -use parking_lot::RwLock; use reqwest::Error; use tokio::fs::{remove_file, File}; use tokio::io::{AsyncRead, AsyncWriteExt, ReadBuf}; +use tokio::sync::RwLock; use tokio::time::Sleep; /// Keeps track of files that are currently being written to. @@ -39,6 +39,7 @@ pub async fn read_file(path: &Path) -> Option Option> + Unpin + Send + 'static, ) -> Result { let done_writing_flag = Arc::new(CacheStatus::new()); let mut file = { - let mut write_lock = WRITING_STATUS.write(); + let mut write_lock = WRITING_STATUS.write().await; let file = File::create(path).await?; // we need to make sure the file exists and is truncated. write_lock.insert(path.to_path_buf(), Arc::clone(&done_writing_flag)); file @@ -87,7 +88,7 @@ pub async fn transparent_file_stream( file.sync_all().await?; // we need metadata } - let mut write_lock = WRITING_STATUS.write(); + let mut write_lock = WRITING_STATUS.write().await; // This needs to be written atomically with the write lock, else // it's possible we have an inconsistent state if errored { diff --git a/src/cache/low_mem.rs b/src/cache/low_mem.rs index eb17794..d3ff8a4 100644 --- a/src/cache/low_mem.rs +++ b/src/cache/low_mem.rs @@ -37,8 +37,11 @@ impl Cache for LowMemCache { } } - async fn put_stream(&mut self, key: CacheKey, image: ByteStream) { - // this call has a side effect and the returned future is for reading - let _ = super::fs::transparent_file_stream(&PathBuf::from(key), image); + async fn put_stream( + &mut self, + key: CacheKey, + image: ByteStream, + ) -> Result { + super::fs::write_file(&PathBuf::from(key), image).await } } diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 060766f..55819b0 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -147,7 +147,11 @@ pub trait Cache: Send + Sync { unimplemented!() } - async fn put_stream(&mut self, _key: CacheKey, _image: ByteStream) { + async fn put_stream( + &mut self, + _key: CacheKey, + _image: ByteStream, + ) -> Result { unimplemented!() } } diff --git a/src/config.rs b/src/config.rs index 9d507c7..6d936e9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -37,4 +37,6 @@ pub struct CliArgs { pub enable_server_string: bool, #[clap(short, long, conflicts_with("memory-quota"), env = "LOW_MEMORY_MODE")] pub low_memory: bool, + #[clap(short, long, parse(from_occurrences))] + pub verbose: usize, } diff --git a/src/main.rs b/src/main.rs index 09a8701..5d20e51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ #![warn(clippy::pedantic, clippy::nursery)] // We're end users, so these is ok -#![allow(clippy::future_not_send, clippy::module_name_repetitions)] +#![allow(clippy::module_name_repetitions)] use std::env::{self, VarError}; use std::process; @@ -53,7 +53,9 @@ async fn main() -> Result<(), std::io::Error> { println!(concat!( env!("CARGO_PKG_NAME"), - " Copyright (C) 2021 Edward Shen\n\n", + " Copyright (C) 2021 ", + env!("CARGO_PKG_AUTHORS"), + "\n\n", env!("CARGO_PKG_NAME"), " is free software: you can redistribute it and/or modify\n\ it under the terms of the GNU General Public License as published by\n\ @@ -76,10 +78,13 @@ async fn main() -> Result<(), std::io::Error> { let cache_path = cli_args.cache_path.clone(); let low_mem_mode = cli_args.low_memory; - SimpleLogger::new() - .with_level(LevelFilter::Info) - .init() - .unwrap(); + match cli_args.verbose { + 0 => SimpleLogger::new().with_level(LevelFilter::Info), + 1 => SimpleLogger::new().with_level(LevelFilter::Debug), + _ => SimpleLogger::new().with_level(LevelFilter::Trace), + } + .init() + .unwrap(); let client_secret = if let Ok(v) = env::var("CLIENT_SECRET") { v