Skip to content

Commit

Permalink
update init.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
kfatyuip committed Aug 7, 2024
1 parent 0abd38a commit df9d6e8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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,
Expand All @@ -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>,
}

Expand Down
26 changes: 13 additions & 13 deletions src/init.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -54,7 +54,7 @@ lazy_static! {
)
.unwrap(),
);
Mutex::new(cache)
RwLock::new(cache)
};
}

Expand Down Expand Up @@ -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);
}
}
}
});

Expand All @@ -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);
}
});

Expand Down
10 changes: 5 additions & 5 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -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},
};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -230,15 +230,15 @@ 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!");

return Ok(());
}
#[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
Expand Down

0 comments on commit df9d6e8

Please sign in to comment.