From ebb9cd055f9dfb19fabdcedb506ba78c43cb43dd Mon Sep 17 00:00:00 2001 From: fakeshadow <24548779@qq.com> Date: Fri, 5 Feb 2021 19:38:11 -0800 Subject: [PATCH] use static dispatch on signal handling. reduce allocation (#272) --- actix-rt/Cargo.toml | 4 ++-- actix-server/src/signals.rs | 40 +++++++++++++++++-------------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/actix-rt/Cargo.toml b/actix-rt/Cargo.toml index f8f3984d..db232090 100644 --- a/actix-rt/Cargo.toml +++ b/actix-rt/Cargo.toml @@ -26,8 +26,8 @@ macros = ["actix-macros"] actix-macros = { version = "0.2.0", optional = true } 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] -tokio = { version = "1", features = ["full"] } +tokio = { version = "1.2", features = ["full"] } hyper = { version = "0.14", default-features = false, features = ["server", "tcp", "http1"] } diff --git a/actix-server/src/signals.rs b/actix-server/src/signals.rs index ea1de47e..0e3cef13 100644 --- a/actix-server/src/signals.rs +++ b/actix-server/src/signals.rs @@ -2,8 +2,6 @@ use std::future::Future; use std::pin::Pin; use std::task::{Context, Poll}; -use futures_core::future::LocalBoxFuture; - use crate::server::Server; /// Different types of process signals @@ -23,9 +21,9 @@ pub(crate) enum Signal { pub(crate) struct Signals { srv: Server, #[cfg(not(unix))] - signals: LocalBoxFuture<'static, std::io::Result<()>>, + signals: futures_core::future::LocalBoxFuture<'static, std::io::Result<()>>, #[cfg(unix)] - signals: Vec<(Signal, LocalBoxFuture<'static, ()>)>, + signals: Vec<(Signal, actix_rt::signal::unix::Signal)>, } impl Signals { @@ -48,23 +46,21 @@ impl Signals { (unix::SignalKind::quit(), Signal::Quit), ]; - let mut signals = Vec::new(); - - for (kind, sig) in sig_map.iter() { - match unix::signal(*kind) { - Ok(mut stream) => { - let fut = Box::pin(async move { - let _ = stream.recv().await; - }) as _; - signals.push((*sig, fut)); - } - Err(e) => log::error!( - "Can not initialize stream handler for {:?} err: {}", - sig, - e - ), - } - } + let signals = sig_map + .iter() + .filter_map(|(kind, sig)| { + unix::signal(*kind) + .map(|tokio_sig| (*sig, tokio_sig)) + .map_err(|e| { + log::error!( + "Can not initialize stream handler for {:?} err: {}", + sig, + e + ) + }) + .ok() + }) + .collect::>(); actix_rt::spawn(Signals { srv, signals }); } @@ -86,7 +82,7 @@ impl Future for Signals { #[cfg(unix)] { 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; self.srv.signal(sig); return Poll::Ready(());