mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-04 21:37:58 +03:00
Fix signals support #324
This commit is contained in:
parent
f8d03f1c8e
commit
d6feeec933
3 changed files with 70 additions and 76 deletions
|
@ -1,8 +1,12 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [1.0.4] - 2024-03-30
|
||||||
|
|
||||||
|
* Fix signals support #324
|
||||||
|
|
||||||
## [1.0.3] - 2024-03-29
|
## [1.0.3] - 2024-03-29
|
||||||
|
|
||||||
* Fix windows signals support
|
* Fix windows signals support #322
|
||||||
|
|
||||||
## [1.0.2] - 2024-03-25
|
## [1.0.2] - 2024-03-25
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "ntex-server"
|
name = "ntex-server"
|
||||||
version = "1.0.3"
|
version = "1.0.4"
|
||||||
authors = ["ntex contributors <team@ntex.rs>"]
|
authors = ["ntex contributors <team@ntex.rs>"]
|
||||||
description = "Server for ntex framework"
|
description = "Server for ntex framework"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{cell::RefCell, thread};
|
use std::{cell::RefCell, sync::Mutex, thread};
|
||||||
|
|
||||||
use ntex_rt::System;
|
use ntex_rt::System;
|
||||||
|
|
||||||
|
@ -8,6 +8,9 @@ thread_local! {
|
||||||
static HANDLERS: RefCell<Vec<oneshot::Sender<Signal>>> = Default::default();
|
static HANDLERS: RefCell<Vec<oneshot::Sender<Signal>>> = Default::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CB = Box<dyn Fn(Signal) + Send>;
|
||||||
|
static CUR_SYS: Mutex<RefCell<Option<(System, CB)>>> = Mutex::new(RefCell::new(None));
|
||||||
|
|
||||||
/// Different types of process signals
|
/// Different types of process signals
|
||||||
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
||||||
pub enum Signal {
|
pub enum Signal {
|
||||||
|
@ -34,49 +37,73 @@ pub fn signal() -> oneshot::Receiver<Signal> {
|
||||||
rx
|
rx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn register_system<T: Send + 'static>(srv: Server<T>) -> bool {
|
||||||
|
let guard = match CUR_SYS.lock() {
|
||||||
|
Ok(guard) => guard,
|
||||||
|
Err(_) => {
|
||||||
|
log::error!("Cannot lock mutex");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut sys = guard.borrow_mut();
|
||||||
|
let started = sys.is_some();
|
||||||
|
*sys = Some((System::current(), Box::new(move |sig| srv.signal(sig))));
|
||||||
|
started
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_signal(sig: Signal) {
|
||||||
|
if let Ok(guard) = CUR_SYS.lock() {
|
||||||
|
if let Some((sys, srv)) = &*guard.borrow() {
|
||||||
|
(*srv)(sig);
|
||||||
|
sys.arbiter().exec_fn(move || {
|
||||||
|
HANDLERS.with(|handlers| {
|
||||||
|
for tx in handlers.borrow_mut().drain(..) {
|
||||||
|
let _ = tx.send(sig);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(target_family = "unix")]
|
#[cfg(target_family = "unix")]
|
||||||
/// Register signal handler.
|
/// Register signal handler.
|
||||||
///
|
///
|
||||||
/// Signals are handled by oneshots, you have to re-register
|
/// Signals are handled by oneshots, you have to re-register
|
||||||
/// after each signal.
|
/// after each signal.
|
||||||
pub(crate) fn start<T: Send + 'static>(srv: Server<T>) {
|
pub(crate) fn start<T: Send + 'static>(srv: Server<T>) {
|
||||||
let _ = thread::Builder::new()
|
if !register_system(srv) {
|
||||||
.name("ntex-server signals".to_string())
|
let _ = thread::Builder::new()
|
||||||
.spawn(move || {
|
.name("ntex-server signals".to_string())
|
||||||
use signal_hook::consts::signal::*;
|
.spawn(move || {
|
||||||
use signal_hook::iterator::Signals;
|
use signal_hook::consts::signal::*;
|
||||||
|
use signal_hook::iterator::Signals;
|
||||||
|
|
||||||
let sigs = vec![SIGHUP, SIGINT, SIGTERM, SIGQUIT];
|
let sigs = vec![SIGHUP, SIGINT, SIGTERM, SIGQUIT];
|
||||||
let mut signals = match Signals::new(sigs) {
|
let mut signals = match Signals::new(sigs) {
|
||||||
Ok(signals) => signals,
|
Ok(signals) => signals,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Cannot initialize signals handler: {}", e);
|
log::error!("Cannot initialize signals handler: {}", e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
for info in &mut signals {
|
|
||||||
let sig = match info {
|
|
||||||
SIGHUP => Signal::Hup,
|
|
||||||
SIGTERM => Signal::Term,
|
|
||||||
SIGINT => Signal::Int,
|
|
||||||
SIGQUIT => Signal::Quit,
|
|
||||||
_ => continue,
|
|
||||||
};
|
};
|
||||||
|
for info in &mut signals {
|
||||||
|
let sig = match info {
|
||||||
|
SIGHUP => Signal::Hup,
|
||||||
|
SIGTERM => Signal::Term,
|
||||||
|
SIGINT => Signal::Int,
|
||||||
|
SIGQUIT => Signal::Quit,
|
||||||
|
_ => continue,
|
||||||
|
};
|
||||||
|
handle_signal(sig);
|
||||||
|
|
||||||
srv.signal(sig);
|
if matches!(sig, Signal::Int | Signal::Quit) {
|
||||||
System::current().arbiter().exec_fn(move || {
|
return;
|
||||||
HANDLERS.with(|handlers| {
|
}
|
||||||
for tx in handlers.borrow_mut().drain(..) {
|
|
||||||
let _ = tx.send(sig);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
if matches!(sig, Signal::Int | Signal::Quit) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_family = "windows")]
|
#[cfg(target_family = "windows")]
|
||||||
|
@ -85,49 +112,12 @@ pub(crate) fn start<T: Send + 'static>(srv: Server<T>) {
|
||||||
/// Signals are handled by oneshots, you have to re-register
|
/// Signals are handled by oneshots, you have to re-register
|
||||||
/// after each signal.
|
/// after each signal.
|
||||||
pub(crate) fn start<T: Send + 'static>(srv: Server<T>) {
|
pub(crate) fn start<T: Send + 'static>(srv: Server<T>) {
|
||||||
use std::sync::Mutex;
|
if !register_system(srv) {
|
||||||
|
|
||||||
use ntex_rt::spawn;
|
|
||||||
|
|
||||||
static CUR_SYS: Mutex<RefCell<Option<System>>> = Mutex::new(RefCell::new(None));
|
|
||||||
|
|
||||||
let guard = match CUR_SYS.lock() {
|
|
||||||
Ok(guard) => guard,
|
|
||||||
Err(_) => {
|
|
||||||
log::error!("Cannot lock mutex");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut sys = guard.borrow_mut();
|
|
||||||
let started = sys.is_some();
|
|
||||||
*sys = Some(System::current());
|
|
||||||
|
|
||||||
let rx = signal();
|
|
||||||
let _ = spawn(async move {
|
|
||||||
if let Ok(sig) = rx.await {
|
|
||||||
srv.signal(sig);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if !started {
|
|
||||||
let _ = thread::Builder::new()
|
let _ = thread::Builder::new()
|
||||||
.name("ntex-server signals".to_string())
|
.name("ntex-server signals".to_string())
|
||||||
.spawn(move || {
|
.spawn(move || {
|
||||||
ctrlc::set_handler(move || {
|
ctrlc::set_handler(|| handle_signal(Signal::Int))
|
||||||
if let Ok(guard) = CUR_SYS.lock() {
|
.expect("Error setting Ctrl-C handler");
|
||||||
if let Some(sys) = &*guard.borrow() {
|
|
||||||
sys.arbiter().exec_fn(|| {
|
|
||||||
HANDLERS.with(|handlers| {
|
|
||||||
for tx in handlers.borrow_mut().drain(..) {
|
|
||||||
let _ = tx.send(Signal::Int);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.expect("Error setting Ctrl-C handler");
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue