Skip to content

Commit b109195

Browse files
committed
config: change Mutex -> RwLock
1 parent cab9839 commit b109195

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ use std::{
88
path::PathBuf,
99
process::{Command, Stdio},
1010
str::FromStr,
11+
sync::Arc,
1112
};
13+
use tokio::sync::RwLock;
1214
use toml_edit::{value, DocumentMut};
1315

1416
// module name for logging engine
1517
const NAME: &str = "<i><bright-black> config: </>";
1618

19+
pub type SharedConfig = Arc<RwLock<AppConfig>>;
20+
1721
#[derive(
1822
clap::ValueEnum, Default, Debug, PartialEq, PartialOrd, Clone, Copy, Deserialize, Serialize,
1923
)]

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use tokio::sync::Notify;
2828
use tokio::time::Instant;
2929

3030
use std::net::SocketAddr;
31-
use std::sync::Mutex;
31+
use tokio::sync::RwLock;
3232

3333
// module name for logging engine
3434
const NAME: &str = "<i><bright-black> main: </>";
@@ -168,7 +168,7 @@ async fn tokio_main(
168168
if let Some(ref bindaddr) = config.webserver {
169169
// preparing AppState and starting webserver
170170
let state = web::AppState {
171-
config: Arc::new(Mutex::new(config.clone())),
171+
config: Arc::new(RwLock::new(config.clone())),
172172
config_file: config_file.into(),
173173
};
174174
let app = web::app(state.into());

src/web.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::config::AppConfig;
2+
use crate::config::SharedConfig;
23
use axum::{
34
body::Body,
45
extract::{Query, State},
@@ -10,15 +11,15 @@ use axum::{
1011
use chrono::Local;
1112
use std::collections::HashMap;
1213
use std::path::PathBuf;
13-
use std::sync::{Arc, Mutex};
14+
use std::sync::Arc;
1415
use tokio::fs;
1516

1617
const TEMPLATE: &str = include_str!("../static/index.html");
1718
const PICO_CSS: &str = include_str!("../static/pico.min.css");
1819

1920
#[derive(Clone)]
2021
pub struct AppState {
21-
pub config: Arc<Mutex<AppConfig>>,
22+
pub config: SharedConfig,
2223
pub config_file: Arc<PathBuf>,
2324
}
2425

@@ -48,7 +49,7 @@ async fn download_handler(
4849
State(state): State<Arc<AppState>>,
4950
Query(params): Query<HashMap<String, String>>,
5051
) -> impl IntoResponse {
51-
let file_path = state.config.lock().unwrap().logfile.clone();
52+
let file_path = state.config.read().await.logfile.clone();
5253
// if we have filename parameter, use it; default otherwise
5354
let filename = params
5455
.get("filename")
@@ -73,7 +74,7 @@ async fn download_handler(
7374
}
7475

7576
async fn get_config(State(state): State<Arc<AppState>>) -> impl IntoResponse {
76-
let cfg = state.config.lock().unwrap().clone();
77+
let cfg = state.config.read().await.clone();
7778
Json(cfg)
7879
}
7980

@@ -82,7 +83,7 @@ async fn set_config(
8283
Json(new_cfg): Json<AppConfig>,
8384
) -> impl IntoResponse {
8485
{
85-
let mut cfg = state.config.lock().unwrap();
86+
let mut cfg = state.config.write().await;
8687
*cfg = new_cfg.clone();
8788
cfg.save((&state.config_file).to_path_buf());
8889
}

0 commit comments

Comments
 (0)