Skip to content

Commit

Permalink
wait a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
ibigbug committed Dec 30, 2023
1 parent 661c8c5 commit 76a1104
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
15 changes: 11 additions & 4 deletions clash_lib/src/app/api/handlers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ async fn update_configs(
State(state): State<ConfigState>,
Json(req): Json<UpdateConfigRequest>,
) -> impl IntoResponse {
let (done, wait) = tokio::sync::oneshot::channel();
let g = state.global_state.lock().await;
match (req.path, req.payload) {
(_, Some(payload)) => {
let msg = "config reloading from payload".to_string();
let cfg = crate::Config::Str(payload);
match g.reload_tx.send(cfg).await {
Ok(_) => (StatusCode::NO_CONTENT, msg).into_response(),
match g.reload_tx.send((cfg, done)).await {
Ok(_) => {
wait.await.unwrap();
(StatusCode::NO_CONTENT, msg).into_response()
}
Err(_) => (
StatusCode::INTERNAL_SERVER_ERROR,
"could not signal config reload",
Expand All @@ -127,8 +131,11 @@ async fn update_configs(

let msg = format!("config reloading from file {}", path);
let cfg: crate::Config = crate::Config::File(path);
match g.reload_tx.send(cfg).await {
Ok(_) => (StatusCode::NO_CONTENT, msg).into_response(),
match g.reload_tx.send((cfg, done)).await {
Ok(_) => {
wait.await.unwrap();
(StatusCode::NO_CONTENT, msg).into_response()
}

Err(_) => (
StatusCode::INTERNAL_SERVER_ERROR,
Expand Down
3 changes: 2 additions & 1 deletion clash_lib/src/app/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ pub fn setup_logging(
.with(
tracing_subscriber::fmt::Layer::new()
.with_ansi(std::io::stdout().is_terminal())
.pretty()
.compact()
.with_target(false)
.with_file(true)
.with_line_number(true)
.with_writer(move || -> Box<dyn std::io::Write> { Box::new(W(appender.clone())) })
Expand Down
29 changes: 18 additions & 11 deletions clash_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::io;
use std::path::PathBuf;
use std::sync::{Arc, OnceLock};
use thiserror::Error;
use tokio::sync::{broadcast, mpsc, Mutex};
use tokio::sync::{broadcast, mpsc, oneshot, Mutex};
use tokio::task::JoinHandle;
use tracing::debug;
use tracing::error;
Expand Down Expand Up @@ -96,7 +96,7 @@ pub struct GlobalState {
tunnel_listener_handle: Option<JoinHandle<Result<(), Error>>>,
api_listener_handle: Option<JoinHandle<Result<(), Error>>>,
dns_listener_handle: Option<JoinHandle<Result<(), Error>>>,
reload_tx: mpsc::Sender<Config>,
reload_tx: mpsc::Sender<(Config, oneshot::Sender<()>)>,
cwd: String,
}

Expand Down Expand Up @@ -301,7 +301,7 @@ async fn start_async(opts: Options) -> Result<(), Error> {
}));

tasks.push(Box::pin(async move {
while let Some(config) = reload_rx.recv().await {
while let Some((config, done)) = reload_rx.recv().await {
info!("reloading config");
let config = match config.try_parse() {
Ok(c) => c,
Expand Down Expand Up @@ -395,6 +395,9 @@ async fn start_async(opts: Options) -> Result<(), Error> {
authenticator,
)?));

done.send(()).unwrap();

debug!("stopping listeners");
let mut g = global_state.lock().await;
if let Some(h) = g.inbound_listener_handle.take() {
h.abort();
Expand All @@ -409,19 +412,23 @@ async fn start_async(opts: Options) -> Result<(), Error> {
h.abort();
}

let inbound_runner = inbound_manager.lock().await.get_runner()?;
let inbound_listener_handle = tokio::spawn(inbound_runner);
let inbound_listener_handle = inbound_manager
.lock()
.await
.get_runner()
.map(tokio::spawn)?;

let tun_runner = get_tun_runner(config.tun, dispatcher.clone(), dns_resolver.clone())?;
let tun_runner_handle = tun_runner.map(tokio::spawn);
let tun_runner_handle =
get_tun_runner(config.tun, dispatcher.clone(), dns_resolver.clone())?
.map(tokio::spawn);

debug!("reloading dns listener");
let dns_listener_handle = dns::get_dns_listener(config.dns, dns_resolver.clone())
.await
.map(tokio::spawn);

debug!("reloading api listener");
let api_runner = app::api::get_api_runner(
let api_listener_handle = app::api::get_api_runner(
config.general.controller,
log_tx.clone(),
inbound_manager.clone(),
Expand All @@ -433,13 +440,13 @@ async fn start_async(opts: Options) -> Result<(), Error> {
cache_store,
router,
cwd.to_string_lossy().to_string(),
);
let api_listener_handle = api_runner.map(tokio::spawn);
)
.map(tokio::spawn);

g.inbound_listener_handle = Some(inbound_listener_handle);
g.tunnel_listener_handle = tun_runner_handle;
g.dns_listener_handle = dns_listener_handle;
g.api_listener_handle = api_listener_handle
g.api_listener_handle = api_listener_handle;
}
Ok(())
}));
Expand Down

0 comments on commit 76a1104

Please sign in to comment.