Skip to content

Commit

Permalink
use arc_swap for config
Browse files Browse the repository at this point in the history
  • Loading branch information
kfatyuip committed Aug 6, 2024
1 parent 4c45fed commit 0abd38a
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 25 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Release

on:
push:
tags:
- '[0-9]+.[0-9]+.[0-9]+'

permissions:
contents: write

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
- name: Build
run: cargo build --verbose --release
- name: Run tests
run: cargo test --verbose --release
- name: Release binary
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: target/release/zest
asset_name: zest-amd64-release
tag: ${{ github.ref }}
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zest"
version = "0.1.9"
version = "0.2.0"
edition = "2021"

[profile.release]
Expand All @@ -18,6 +18,7 @@ log = ["dep:log"]

[dependencies]
anyhow = "1.0.86"
arc-swap = "1.7.1"
async-mutex = "1.4.0"
async-rwlock = "1.3.0"
chrono = { version = "0.4.38", features = ["clock", "now"] }
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
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 266.78us 120.87us 3.13ms 82.98%
Req/Sec 4.32k 250.25 5.61k 83.42%
173591 requests in 10.10s, 137.41MB read
Socket errors: connect 0, read 173588, write 0, timeout 0
Requests/sec: 17188.22
Transfer/sec: 13.61MB
wrk http://localhost:8080 -t 4 -d 10s 1.54s user 11.87s system 132% cpu 10.107 total
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
```

+ python -m http.server 8080
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use async_rwlock::RwLock;
use arc_swap::ArcSwap;
use clap::{command, Parser};
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
Expand All @@ -15,7 +15,7 @@ use std::{
lazy_static! {
pub static ref CONFIG_PATH: Mutex<String> = Mutex::new("".to_owned());
pub static ref DEFAULT_CONFIG: Config = init_config();
pub static ref CONFIG: Arc<RwLock<Config>> = Arc::new(RwLock::new((*DEFAULT_CONFIG).clone()));
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);
}
Expand Down
12 changes: 4 additions & 8 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_mutex::Mutex;
use async_rwlock::RwLock;
use async_mutex::Mutex;
use lazy_static::lazy_static;
use log4rs::Handle;
use lru::LruCache;
Expand Down Expand Up @@ -153,9 +153,7 @@ pub async fn init_signal() -> io::Result<()> {
if sig == SIGHUP {
let config: crate::config::Config = init_config();

let mut _c = CONFIG.try_write().unwrap();
*_c = config.clone();
drop(_c);
CONFIG.store(Arc::new(config.clone()));

set_current_dir(config.clone().server.root).unwrap();

Expand Down Expand Up @@ -184,7 +182,7 @@ pub async fn init_signal() -> io::Result<()> {
let mut t = T.write().await;
*t = None;
drop(t);
}
}
}
});

Expand All @@ -193,11 +191,9 @@ pub async fn init_signal() -> io::Result<()> {

#[cfg(feature = "lru_cache")]
pub async fn init_cache() -> io::Result<()> {
let config = CONFIG.try_read().unwrap();
let config = CONFIG.load();
let tick = config.clone().server.tick.unwrap_or(*DEFAULT_TICK);

drop(config);

let mut _b: bool = false;
tokio::spawn(async move {
loop {
Expand Down
2 changes: 1 addition & 1 deletion src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn root_relative(p: &str) -> &str {

#[inline]
pub async fn location_index(path: PathBuf, location: &str) -> Result<String> {
let config = CONFIG.try_read().unwrap();
let config = CONFIG.load();

for (s, v) in &config.locations.clone().unwrap_or_default() {
if root_relative(s) == location.trim_end_matches('/') {
Expand Down
8 changes: 3 additions & 5 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async fn handle_connection<S>(mut stream: S) -> Result<(i32, String)>
where
S: AsyncReadExt + AsyncWriteExt + Unpin,
{
let config = CONFIG.try_read().unwrap();
let config = CONFIG.load();

let mut response: Response = Response {
version: "1.1",
Expand Down Expand Up @@ -316,12 +316,10 @@ pub async fn zest_main() -> Result<(), Box<dyn Error>> {
init_cache().await.context("failed to init lru cache")?;

loop {
let _config = CONFIG.try_read().unwrap();
let config = _config.clone();
drop(_config);
let config = CONFIG.load();

let handle = tokio::spawn(async move {
zest_listener(&config.clone()).await.unwrap();
zest_listener(config.clone()).await.unwrap();
});

let mut t = T.try_write().unwrap();
Expand Down

0 comments on commit 0abd38a

Please sign in to comment.