diff --git a/README.md b/README.md index 1913dab..fbfdde6 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ server: info: "Powered by Rust" root: . error_page: 404.html # optional - tick: 256 # optional (ms) + interval: 256 # optional (ms) cache: # optional index_capacity: 16 file_capacity: 32 @@ -51,13 +51,13 @@ logging: # optional Running 10s test @ http://localhost:8080 4 threads and 10 connections Thread Stats Avg Stdev Max +/- Stdev - Latency 264.01us 114.49us 2.60ms 81.90% - Req/Sec 4.37k 235.68 4.75k 81.93% - 175701 requests in 10.10s, 139.08MB read - Socket errors: connect 0, read 175699, write 0, timeout 0 -Requests/sec: 17396.48 -Transfer/sec: 13.77MB -wrk http://localhost:8080 -t 4 -d 10s 1.45s user 12.12s system 133% cpu 10.154 total + Latency 260.35us 110.32us 3.49ms 82.65% + Req/Sec 4.38k 195.17 4.70k 76.73% + 176010 requests in 10.10s, 139.32MB read + Socket errors: connect 0, read 176006, write 0, timeout 0 +Requests/sec: 17427.53 +Transfer/sec: 13.79MB +wrk http://localhost:8080 -t 4 -d 10s 1.50s user 11.99s system 132% cpu 10.155 total ``` + python -m http.server 8080 diff --git a/src/config.rs b/src/config.rs index fdbaaad..a697555 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,7 +17,7 @@ lazy_static! { pub static ref DEFAULT_CONFIG: Config = init_config(); pub static ref CONFIG: ArcSwap<Config> = ArcSwap::from(Arc::new((*DEFAULT_CONFIG).clone())); pub static ref ARGS: Args = Args::parse(); - pub static ref DEFAULT_TICK: Duration = Duration::from_millis(1024); + pub static ref DEFAULT_INTERVAL: Duration = Duration::from_millis(1024); } #[derive(Serialize, Deserialize, Clone)] @@ -42,7 +42,7 @@ impl Default for Config { info: "Powered by Rust".to_owned(), root: current_dir().unwrap_or(".".into()), error_page: Some("404.html".to_owned().into()), - tick: Some(*DEFAULT_TICK), + interval: Some(*DEFAULT_INTERVAL), cache: Some(CacheConfig::default()), }, allowlist: None, @@ -65,7 +65,7 @@ pub struct ServerConfig { pub info: String, pub root: PathBuf, pub error_page: Option<PathBuf>, - pub tick: Option<Duration>, + pub interval: Option<Duration>, pub cache: Option<CacheConfig>, } diff --git a/src/init.rs b/src/init.rs index d0fbe46..98a1daf 100644 --- a/src/init.rs +++ b/src/init.rs @@ -1,6 +1,6 @@ -use crate::config::{init_config, CONFIG, DEFAULT_CONFIG, DEFAULT_TICK}; -use async_rwlock::RwLock; +use crate::config::{init_config, CONFIG, DEFAULT_CONFIG, DEFAULT_INTERVAL}; use async_mutex::Mutex; +use async_rwlock::RwLock; use lazy_static::lazy_static; use log4rs::Handle; use lru::LruCache; @@ -26,7 +26,7 @@ lazy_static! { #[cfg(feature = "lru_cache")] lazy_static! { - pub static ref INDEX_CACHE: Mutex<LruCache<String, String>> = { + pub static ref INDEX_CACHE: RwLock<LruCache<String, String>> = { let cache = LruCache::new( NonZeroUsize::new( DEFAULT_CONFIG @@ -39,9 +39,9 @@ lazy_static! { ) .unwrap(), ); - Mutex::new(cache) + RwLock::new(cache) }; - pub static ref FILE_CACHE: Mutex<LruCache<String, Vec<u8>>> = { + pub static ref FILE_CACHE: RwLock<LruCache<String, Vec<u8>>> = { let cache = LruCache::new( NonZeroUsize::new( DEFAULT_CONFIG @@ -54,7 +54,7 @@ lazy_static! { ) .unwrap(), ); - Mutex::new(cache) + RwLock::new(cache) }; } @@ -170,19 +170,19 @@ pub async fn init_signal() -> io::Result<()> { (cache.index_capacity.unwrap(), cache.file_capacity.unwrap()); INDEX_CACHE - .lock() + .write() .await .resize(NonZero::new(index_capacity).unwrap()); FILE_CACHE - .lock() + .write() .await .resize(NonZero::new(file_capacity).unwrap()); let mut t = T.write().await; *t = None; drop(t); - } + } } }); @@ -192,20 +192,20 @@ pub async fn init_signal() -> io::Result<()> { #[cfg(feature = "lru_cache")] pub async fn init_cache() -> io::Result<()> { let config = CONFIG.load(); - let tick = config.clone().server.tick.unwrap_or(*DEFAULT_TICK); + let interval = config.clone().server.interval.unwrap_or(*DEFAULT_INTERVAL); let mut _b: bool = false; tokio::spawn(async move { loop { if _b { - if let Some(mut index_cache) = INDEX_CACHE.try_lock() { + if let Some(mut index_cache) = INDEX_CACHE.try_write() { index_cache.clear(); } - } else if let Some(mut file_cache) = FILE_CACHE.try_lock() { + } else if let Some(mut file_cache) = FILE_CACHE.try_write() { file_cache.clear(); } _b = !_b; - thread::sleep(tick); + thread::sleep(interval); } }); diff --git a/src/server.rs b/src/server.rs index cc0b0b8..5ff3bfb 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,5 +1,5 @@ use crate::{ - config::{Config, ARGS, CONFIG, CONFIG_PATH, DEFAULT_CONFIG, DEFAULT_TICK}, + config::{Config, ARGS, CONFIG, CONFIG_PATH, DEFAULT_CONFIG, DEFAULT_INTERVAL}, init::{init_cache, init_signal, DATE_FORMAT, FILE_CACHE, INDEX_CACHE, T}, route::{location_index, mime_match, root_relative, status_page}, }; @@ -134,7 +134,7 @@ where let mut html: String = String::new(); #[cfg(feature = "lru_cache")] { - let mut cache = INDEX_CACHE.lock().await; + let mut cache = INDEX_CACHE.write().await; if let Some(ctx) = cache.get(&location) { html.clone_from(ctx); } else if let Ok(index) = location_index(path, &location).await { @@ -166,7 +166,7 @@ where #[cfg(feature = "lru_cache")] { - let mut cache = FILE_CACHE.lock().await; + let mut cache = FILE_CACHE.write().await; if let Some(content) = cache.get(&location) { buffer = content.to_vec(); } else { @@ -230,7 +230,7 @@ where #[allow(unused_labels)] 'handle: loop { - if T.try_read().unwrap().is_none() { + if T.read().await.is_none() { #[cfg(feature = "log")] info!("config reloaded!"); @@ -238,7 +238,7 @@ where } #[allow(unused_mut)] if let Ok(Ok((mut stream, _addr))) = timeout( - config.server.tick.unwrap_or(*DEFAULT_TICK), + config.server.interval.unwrap_or(*DEFAULT_INTERVAL), listener.accept(), ) .await