1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-12-02 19:52:24 +01:00

use static dispatch on signal handling. reduce allocation (#272)

This commit is contained in:
fakeshadow 2021-02-05 19:38:11 -08:00 committed by GitHub
parent a77b70aed2
commit ebb9cd055f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 24 deletions

View File

@ -26,8 +26,8 @@ macros = ["actix-macros"]
actix-macros = { version = "0.2.0", optional = true } actix-macros = { version = "0.2.0", optional = true }
futures-core = { version = "0.3", default-features = false } futures-core = { version = "0.3", default-features = false }
tokio = { version = "1", features = ["rt", "net", "parking_lot", "signal", "sync", "time"] } tokio = { version = "1.2", features = ["rt", "net", "parking_lot", "signal", "sync", "time"] }
[dev-dependencies] [dev-dependencies]
tokio = { version = "1", features = ["full"] } tokio = { version = "1.2", features = ["full"] }
hyper = { version = "0.14", default-features = false, features = ["server", "tcp", "http1"] } hyper = { version = "0.14", default-features = false, features = ["server", "tcp", "http1"] }

View File

@ -2,8 +2,6 @@ use std::future::Future;
use std::pin::Pin; use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use futures_core::future::LocalBoxFuture;
use crate::server::Server; use crate::server::Server;
/// Different types of process signals /// Different types of process signals
@ -23,9 +21,9 @@ pub(crate) enum Signal {
pub(crate) struct Signals { pub(crate) struct Signals {
srv: Server, srv: Server,
#[cfg(not(unix))] #[cfg(not(unix))]
signals: LocalBoxFuture<'static, std::io::Result<()>>, signals: futures_core::future::LocalBoxFuture<'static, std::io::Result<()>>,
#[cfg(unix)] #[cfg(unix)]
signals: Vec<(Signal, LocalBoxFuture<'static, ()>)>, signals: Vec<(Signal, actix_rt::signal::unix::Signal)>,
} }
impl Signals { impl Signals {
@ -48,23 +46,21 @@ impl Signals {
(unix::SignalKind::quit(), Signal::Quit), (unix::SignalKind::quit(), Signal::Quit),
]; ];
let mut signals = Vec::new(); let signals = sig_map
.iter()
for (kind, sig) in sig_map.iter() { .filter_map(|(kind, sig)| {
match unix::signal(*kind) { unix::signal(*kind)
Ok(mut stream) => { .map(|tokio_sig| (*sig, tokio_sig))
let fut = Box::pin(async move { .map_err(|e| {
let _ = stream.recv().await; log::error!(
}) as _;
signals.push((*sig, fut));
}
Err(e) => log::error!(
"Can not initialize stream handler for {:?} err: {}", "Can not initialize stream handler for {:?} err: {}",
sig, sig,
e e
), )
} })
} .ok()
})
.collect::<Vec<_>>();
actix_rt::spawn(Signals { srv, signals }); actix_rt::spawn(Signals { srv, signals });
} }
@ -86,7 +82,7 @@ impl Future for Signals {
#[cfg(unix)] #[cfg(unix)]
{ {
for (sig, fut) in self.signals.iter_mut() { for (sig, fut) in self.signals.iter_mut() {
if fut.as_mut().poll(cx).is_ready() { if Pin::new(fut).poll_recv(cx).is_ready() {
let sig = *sig; let sig = *sig;
self.srv.signal(sig); self.srv.signal(sig);
return Poll::Ready(()); return Poll::Ready(());