Skip to content

Commit

Permalink
rework signal handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
zh-jq-b committed May 10, 2024
1 parent 746649d commit 1dc578b
Show file tree
Hide file tree
Showing 21 changed files with 182 additions and 262 deletions.
19 changes: 0 additions & 19 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ members = [
"lib/g3-daemon",
"lib/g3-ctl",
"lib/g3-socket",
"lib/g3-signal",
"lib/g3-compat",
"lib/g3-clap",
"lib/g3-yaml",
Expand Down Expand Up @@ -52,7 +51,6 @@ members = [
"g3keymess",
"g3keymess/proto",
"g3keymess/utils/ctl",
"demo/test-int-signal",
"demo/test-tcp-relay",
"demo/test-resolver",
"demo/test-copy-yield",
Expand Down Expand Up @@ -208,7 +206,6 @@ g3-msgpack = { version = "0.2", path = "lib/g3-msgpack" }
g3-hickory-client = { version = "0.1", path = "lib/g3-hickory-client" }
g3-resolver = { version = "0.5", path = "lib/g3-resolver" }
g3-runtime = { version = "0.3", path = "lib/g3-runtime" }
g3-signal = { version = "0.3", path = "lib/g3-signal" }
g3-socket = { version = "0.4", path = "lib/g3-socket" }
g3-socks = { version = "0.1", path = "lib/g3-socks" }
g3-openssl = { version = "0.1", path = "lib/g3-openssl" }
Expand Down
11 changes: 0 additions & 11 deletions demo/test-int-signal/Cargo.toml

This file was deleted.

39 changes: 0 additions & 39 deletions demo/test-int-signal/src/main.rs

This file was deleted.

3 changes: 1 addition & 2 deletions g3bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ anyhow.workspace = true
clap.workspace = true
clap_complete.workspace = true
indicatif = "0.17"
tokio = { workspace = true, features = ["rt", "net", "macros"] }
tokio = { workspace = true, features = ["rt", "net", "macros", "signal"] }
http.workspace = true
url.workspace = true
h2.workspace = true
Expand All @@ -39,7 +39,6 @@ governor = { workspace = true, features = ["std", "jitter"] }
hickory-client.workspace = true
hickory-proto.workspace = true
g3-runtime.workspace = true
g3-signal.workspace = true
g3-types = { workspace = true, features = ["openssl", "rustls"] }
g3-clap.workspace = true
g3-socket.workspace = true
Expand Down
17 changes: 8 additions & 9 deletions g3bench/src/target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ use std::time::Duration;
use anyhow::{anyhow, Context};
use governor::RateLimiter;
use hdrhistogram::Histogram;
use tokio::signal::unix::SignalKind;
use tokio::sync::{mpsc, Barrier, Semaphore};
use tokio::time::{Instant, MissedTickBehavior};

use g3_signal::{ActionSignal, SigResult};
use g3_statsd_client::StatsdClient;

use super::ProcArgs;
Expand Down Expand Up @@ -169,9 +167,13 @@ where
fn notify_finish(&mut self) {}
}

fn quit_at_sigint(_count: u32) -> SigResult {
stats::mark_force_quit();
SigResult::Break
fn register_signal_handler() {
tokio::spawn(async move {
if let Err(e) = tokio::signal::ctrl_c().await {
eprintln!("error when waiting Ctrl-C: {e}");
}
stats::mark_force_quit();
});
}

async fn run<RS, H, C, T>(mut target: T, proc_args: &ProcArgs) -> anyhow::Result<()>
Expand All @@ -188,10 +190,7 @@ where
let progress_counter = progress.as_ref().map(|p| p.counter());

stats::init_global_state(proc_args.requests, proc_args.log_error_count);
tokio::spawn(
ActionSignal::new(SignalKind::interrupt(), &quit_at_sigint)
.map_err(|e| anyhow!("failed to set handler for SIGINT: {e:?}"))?,
);
register_signal_handler();

let rate_limit = proc_args
.rate_limit
Expand Down
1 change: 0 additions & 1 deletion g3keymess/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ itoa.workspace = true
arc-swap.workspace = true
serde_json.workspace = true
g3-daemon = { workspace = true, features = ["register"] }
g3-signal.workspace = true
g3-yaml = { workspace = true, features = ["histogram"] }
g3-types = { workspace = true, features = [] }
g3-socket.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion g3keymess/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn tokio_run(args: &ProcArgs) -> anyhow::Result<()> {
});
}

g3keymess::signal::setup_and_spawn().context("failed to setup signal handler")?;
g3keymess::signal::register().context("failed to setup signal handler")?;

g3keymess::store::load_all()
.await
Expand Down
56 changes: 30 additions & 26 deletions g3keymess/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,12 @@
*/

use log::{error, info, warn};
use tokio::signal::unix::SignalKind;
use tokio::sync::Mutex;

use g3_signal::{ActionSignal, SigResult};
use g3_daemon::signal::AsyncSignalAction;

static RELOAD_MUTEX: Mutex<()> = Mutex::const_new(());

fn do_quit(_: u32) -> SigResult {
info!("got quit signal");
tokio::spawn(crate::control::UniqueController::abort_immediately());
SigResult::Break
}

fn go_offline(_: u32) -> SigResult {
info!("got offline signal");
tokio::spawn(crate::control::DaemonController::abort());
SigResult::Break
}

fn call_reload(_: u32) -> SigResult {
info!("got reload signal");
tokio::spawn(do_reload());
SigResult::Continue
}

async fn do_reload() {
let _guard = RELOAD_MUTEX.lock().await;
info!("reloading config");
Expand All @@ -59,10 +40,33 @@ async fn do_reload() {
info!("reload finished");
}

pub fn setup_and_spawn() -> anyhow::Result<()> {
tokio::spawn(ActionSignal::new(SignalKind::quit(), &do_quit)?);
tokio::spawn(ActionSignal::new(SignalKind::interrupt(), &do_quit)?);
tokio::spawn(ActionSignal::new(SignalKind::terminate(), &go_offline)?);
tokio::spawn(ActionSignal::new(SignalKind::hangup(), &call_reload)?);
Ok(())
#[derive(Clone, Copy)]
struct QuitAction {}

impl AsyncSignalAction for QuitAction {
async fn run(&self) {
crate::control::UniqueController::abort_immediately().await
}
}

#[derive(Clone, Copy)]
struct OfflineAction {}

impl AsyncSignalAction for OfflineAction {
async fn run(&self) {
crate::control::DaemonController::abort().await
}
}

#[derive(Clone, Copy)]
struct ReloadAction {}

impl AsyncSignalAction for ReloadAction {
async fn run(&self) {
do_reload().await
}
}

pub fn register() -> anyhow::Result<()> {
g3_daemon::signal::register(QuitAction {}, OfflineAction {}, ReloadAction {})
}
1 change: 0 additions & 1 deletion g3proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ pyo3 = { workspace = true, features = ["auto-initialize"], optional = true }
g3-types = { workspace = true, features = ["auth-crypt", "rustls", "openssl", "acl-rule", "http", "route", "async-log"] }
g3-socket.workspace = true
g3-daemon.workspace = true
g3-signal.workspace = true
g3-datetime.workspace = true
g3-statsd-client.workspace = true
g3-histogram.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion g3proxy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn tokio_run(args: &ProcArgs) -> anyhow::Result<()> {
});
}

g3proxy::signal::setup_and_spawn().context("failed to setup signal handler")?;
g3proxy::signal::register().context("failed to setup signal handler")?;
g3proxy::resolve::spawn_all()
.await
.context("failed to spawn all resolvers")?;
Expand Down
56 changes: 30 additions & 26 deletions g3proxy/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,12 @@
*/

use log::{error, info, warn};
use tokio::signal::unix::SignalKind;
use tokio::sync::Mutex;

use g3_signal::{ActionSignal, SigResult};
use g3_daemon::signal::AsyncSignalAction;

static RELOAD_MUTEX: Mutex<()> = Mutex::const_new(());

fn do_quit(_: u32) -> SigResult {
info!("got quit signal");
tokio::spawn(crate::control::UniqueController::abort_immediately());
SigResult::Break
}

fn go_offline(_: u32) -> SigResult {
info!("got offline signal");
tokio::spawn(crate::control::DaemonController::abort());
SigResult::Break
}

fn call_reload(_: u32) -> SigResult {
info!("got reload signal");
tokio::spawn(do_reload());
SigResult::Continue
}

async fn do_reload() {
let _guard = RELOAD_MUTEX.lock().await;
info!("reloading config");
Expand Down Expand Up @@ -68,10 +49,33 @@ async fn do_reload() {
info!("reload finished");
}

pub fn setup_and_spawn() -> anyhow::Result<()> {
tokio::spawn(ActionSignal::new(SignalKind::quit(), &do_quit)?);
tokio::spawn(ActionSignal::new(SignalKind::interrupt(), &do_quit)?);
tokio::spawn(ActionSignal::new(SignalKind::terminate(), &go_offline)?);
tokio::spawn(ActionSignal::new(SignalKind::hangup(), &call_reload)?);
Ok(())
#[derive(Clone, Copy)]
struct QuitAction {}

impl AsyncSignalAction for QuitAction {
async fn run(&self) {
crate::control::UniqueController::abort_immediately().await
}
}

#[derive(Clone, Copy)]
struct OfflineAction {}

impl AsyncSignalAction for OfflineAction {
async fn run(&self) {
crate::control::DaemonController::abort().await
}
}

#[derive(Clone, Copy)]
struct ReloadAction {}

impl AsyncSignalAction for ReloadAction {
async fn run(&self) {
do_reload().await
}
}

pub fn register() -> anyhow::Result<()> {
g3_daemon::signal::register(QuitAction {}, OfflineAction {}, ReloadAction {})
}
1 change: 0 additions & 1 deletion g3tiles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ bitflags.workspace = true
flume.workspace = true
rustc-hash.workspace = true
g3-daemon.workspace = true
g3-signal.workspace = true
g3-yaml = { workspace = true, features = ["acl-rule", "route", "openssl", "rustls", "histogram"] }
g3-types = { workspace = true, features = ["acl-rule", "route", "openssl", "rustls"] }
g3-socket.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion g3tiles/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn tokio_run(args: &ProcArgs) -> anyhow::Result<()> {
});
}

g3tiles::signal::setup_and_spawn().context("failed to setup signal handler")?;
g3tiles::signal::register().context("failed to setup signal handler")?;

g3tiles::discover::load_all()
.await
Expand Down
Loading

0 comments on commit 1dc578b

Please sign in to comment.