1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-25 00:12:59 +01:00
actix-extras/src/server/worker.rs

406 lines
13 KiB
Rust
Raw Normal View History

2018-08-03 08:17:10 +02:00
use std::rc::Rc;
use std::sync::{atomic::AtomicUsize, atomic::Ordering, Arc};
use std::{net, time};
use futures::sync::mpsc::{unbounded, SendError, UnboundedSender};
2018-05-29 19:31:37 +02:00
use futures::sync::oneshot;
2018-04-29 07:55:47 +02:00
use futures::Future;
2018-04-14 01:02:01 +02:00
use net2::TcpStreamExt;
use slab::Slab;
2018-05-29 19:31:37 +02:00
use tokio::executor::current_thread;
2018-05-25 06:03:16 +02:00
use tokio_reactor::Handle;
use tokio_tcp::TcpStream;
2017-12-28 21:38:37 +01:00
2018-07-29 08:43:04 +02:00
#[cfg(any(feature = "tls", feature = "alpn", feature = "rust-tls"))]
2017-12-29 01:25:47 +01:00
use futures::future;
2018-02-10 20:39:12 +01:00
2018-04-14 01:02:01 +02:00
#[cfg(feature = "tls")]
2017-12-28 22:07:29 +01:00
use native_tls::TlsAcceptor;
2018-04-14 01:02:01 +02:00
#[cfg(feature = "tls")]
2017-12-28 22:07:29 +01:00
use tokio_tls::TlsAcceptorExt;
2018-04-14 01:02:01 +02:00
#[cfg(feature = "alpn")]
2017-12-28 22:07:29 +01:00
use openssl::ssl::SslAcceptor;
2018-04-14 01:02:01 +02:00
#[cfg(feature = "alpn")]
2017-12-28 22:07:29 +01:00
use tokio_openssl::SslAcceptorExt;
2018-07-29 08:43:04 +02:00
#[cfg(feature = "rust-tls")]
use rustls::{ServerConfig, Session};
#[cfg(feature = "rust-tls")]
use tokio_rustls::ServerConfigExt;
2018-06-01 18:36:16 +02:00
use actix::msgs::StopArbiter;
2018-08-03 08:17:10 +02:00
use actix::{Actor, Addr, Arbiter, AsyncContext, Context, Handler, Message, Response};
2017-12-28 21:38:37 +01:00
2018-08-03 08:17:10 +02:00
use super::accept::AcceptNotify;
use super::channel::HttpChannel;
use super::settings::{ServerSettings, WorkerSettings};
use super::{HttpHandler, IntoHttpHandler, KeepAlive};
2017-12-28 21:38:37 +01:00
#[derive(Message)]
pub(crate) struct Conn<T> {
pub io: T,
pub token: usize,
2017-12-28 21:38:37 +01:00
pub peer: Option<net::SocketAddr>,
pub http2: bool,
}
#[derive(Clone)]
pub(crate) struct SocketInfo {
pub addr: net::SocketAddr,
pub htype: StreamHandlerType,
}
2018-08-03 08:17:10 +02:00
pub(crate) struct WorkersPool<H: IntoHttpHandler + 'static> {
sockets: Slab<SocketInfo>,
pub factory: Arc<Fn() -> Vec<H> + Send + Sync>,
pub host: Option<String>,
pub keep_alive: KeepAlive,
}
impl<H: IntoHttpHandler + 'static> WorkersPool<H> {
pub fn new<F>(factory: F) -> Self
where
F: Fn() -> Vec<H> + Send + Sync + 'static,
{
WorkersPool {
factory: Arc::new(factory),
host: None,
keep_alive: KeepAlive::Os,
sockets: Slab::new(),
}
}
pub fn insert(&mut self, addr: net::SocketAddr, htype: StreamHandlerType) -> usize {
let entry = self.sockets.vacant_entry();
let token = entry.key();
entry.insert(SocketInfo { addr, htype });
token
}
pub fn start(
&mut self, idx: usize, notify: AcceptNotify,
) -> (WorkerClient, Addr<Worker<H::Handler>>) {
let host = self.host.clone();
let addr = self.sockets[0].addr;
let factory = Arc::clone(&self.factory);
let socks = self.sockets.clone();
let ka = self.keep_alive;
let (tx, rx) = unbounded::<Conn<net::TcpStream>>();
let client = WorkerClient::new(idx, tx, self.sockets.clone());
let conn = client.conn.clone();
let sslrate = client.sslrate.clone();
let addr = Arbiter::start(move |ctx: &mut Context<_>| {
let s = ServerSettings::new(Some(addr), &host, false);
let apps: Vec<_> =
(*factory)().into_iter().map(|h| h.into_handler()).collect();
ctx.add_message_stream(rx);
Worker::new(apps, socks, ka, s, conn, sslrate, notify)
});
(client, addr)
}
}
#[derive(Clone)]
pub(crate) struct WorkerClient {
pub idx: usize,
tx: UnboundedSender<Conn<net::TcpStream>>,
info: Slab<SocketInfo>,
pub conn: Arc<AtomicUsize>,
pub sslrate: Arc<AtomicUsize>,
}
impl WorkerClient {
fn new(
idx: usize, tx: UnboundedSender<Conn<net::TcpStream>>, info: Slab<SocketInfo>,
) -> Self {
WorkerClient {
idx,
tx,
info,
conn: Arc::new(AtomicUsize::new(0)),
sslrate: Arc::new(AtomicUsize::new(0)),
}
}
pub fn send(
&self, msg: Conn<net::TcpStream>,
) -> Result<(), SendError<Conn<net::TcpStream>>> {
self.tx.unbounded_send(msg)
}
pub fn available(&self, maxconn: usize, maxsslrate: usize) -> bool {
if maxsslrate <= self.sslrate.load(Ordering::Relaxed) {
false
} else {
maxconn > self.conn.load(Ordering::Relaxed)
}
}
}
2017-12-29 01:25:47 +01:00
/// Stop worker message. Returns `true` on successful shutdown
/// and `false` if some connections still alive.
2017-12-28 22:07:29 +01:00
pub(crate) struct StopWorker {
pub graceful: Option<time::Duration>,
}
2018-02-12 21:17:30 +01:00
impl Message for StopWorker {
type Result = Result<bool, ()>;
}
2017-12-28 21:38:37 +01:00
/// Http worker
///
2018-04-14 01:02:01 +02:00
/// Worker accepts Socket objects via unbounded channel and start requests
/// processing.
pub(crate) struct Worker<H>
where
H: HttpHandler + 'static,
{
2018-01-04 07:43:44 +01:00
settings: Rc<WorkerSettings<H>>,
socks: Slab<SocketInfo>,
tcp_ka: Option<time::Duration>,
2017-12-28 21:38:37 +01:00
}
2018-01-04 07:43:44 +01:00
impl<H: HttpHandler + 'static> Worker<H> {
2018-04-14 01:02:01 +02:00
pub(crate) fn new(
h: Vec<H>, socks: Slab<SocketInfo>, keep_alive: KeepAlive,
2018-08-03 08:17:10 +02:00
settings: ServerSettings, conn: Arc<AtomicUsize>, sslrate: Arc<AtomicUsize>,
notify: AcceptNotify,
2018-04-14 01:02:01 +02:00
) -> Worker<H> {
let tcp_ka = if let KeepAlive::Tcp(val) = keep_alive {
Some(time::Duration::new(val as u64, 0))
} else {
None
};
2017-12-28 21:38:37 +01:00
Worker {
2018-08-03 08:17:10 +02:00
settings: Rc::new(WorkerSettings::new(
h, keep_alive, settings, notify, conn, sslrate,
)),
socks,
tcp_ka,
2017-12-28 21:38:37 +01:00
}
}
fn update_time(&self, ctx: &mut Context<Self>) {
2018-03-18 19:05:44 +01:00
self.settings.update_date();
2018-05-17 21:20:20 +02:00
ctx.run_later(time::Duration::new(1, 0), |slf, ctx| slf.update_time(ctx));
2017-12-28 21:38:37 +01:00
}
2017-12-29 01:25:47 +01:00
2018-04-14 01:02:01 +02:00
fn shutdown_timeout(
2018-04-29 07:55:47 +02:00
&self, ctx: &mut Context<Self>, tx: oneshot::Sender<bool>, dur: time::Duration,
2018-04-14 01:02:01 +02:00
) {
2017-12-29 01:25:47 +01:00
// sleep for 1 second and then check again
ctx.run_later(time::Duration::new(1, 0), move |slf, ctx| {
2018-01-12 03:35:05 +01:00
let num = slf.settings.num_channels();
2017-12-29 01:25:47 +01:00
if num == 0 {
let _ = tx.send(true);
2018-06-14 08:37:19 +02:00
Arbiter::current().do_send(StopArbiter(0));
2017-12-29 01:25:47 +01:00
} else if let Some(d) = dur.checked_sub(time::Duration::new(1, 0)) {
slf.shutdown_timeout(ctx, tx, d);
} else {
info!("Force shutdown http worker, {} connections", num);
slf.settings.head().traverse::<TcpStream, H>();
2017-12-29 01:25:47 +01:00
let _ = tx.send(false);
2018-06-14 08:37:19 +02:00
Arbiter::current().do_send(StopArbiter(0));
2017-12-29 01:25:47 +01:00
}
});
}
2017-12-28 21:38:37 +01:00
}
2018-04-14 01:02:01 +02:00
impl<H: 'static> Actor for Worker<H>
where
H: HttpHandler + 'static,
{
2017-12-28 21:38:37 +01:00
type Context = Context<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
self.update_time(ctx);
}
}
impl<H> Handler<Conn<net::TcpStream>> for Worker<H>
2018-04-14 01:02:01 +02:00
where
H: HttpHandler + 'static,
2017-12-28 21:38:37 +01:00
{
2018-01-05 22:30:21 +01:00
type Result = ();
2018-04-14 01:02:01 +02:00
fn handle(&mut self, msg: Conn<net::TcpStream>, _: &mut Context<Self>) {
if self.tcp_ka.is_some() && msg.io.set_keepalive(self.tcp_ka).is_err() {
2017-12-28 21:38:37 +01:00
error!("Can not set socket keep-alive option");
}
2018-05-25 06:03:16 +02:00
self.socks
.get_mut(msg.token)
.unwrap()
.htype
.handle(Rc::clone(&self.settings), msg);
2017-12-28 21:38:37 +01:00
}
}
2017-12-28 22:07:29 +01:00
/// `StopWorker` message handler
impl<H> Handler<StopWorker> for Worker<H>
2018-04-14 01:02:01 +02:00
where
H: HttpHandler + 'static,
2017-12-28 22:07:29 +01:00
{
2018-02-12 21:17:30 +01:00
type Result = Response<bool, ()>;
2018-01-05 22:30:21 +01:00
fn handle(&mut self, msg: StopWorker, ctx: &mut Context<Self>) -> Self::Result {
2018-01-12 03:35:05 +01:00
let num = self.settings.num_channels();
2017-12-29 01:25:47 +01:00
if num == 0 {
info!("Shutting down http worker, 0 connections");
2018-02-01 10:08:08 +01:00
Response::reply(Ok(true))
2017-12-29 01:25:47 +01:00
} else if let Some(dur) = msg.graceful {
info!("Graceful http worker shutdown, {} connections", num);
let (tx, rx) = oneshot::channel();
self.shutdown_timeout(ctx, tx, dur);
2018-02-12 21:17:30 +01:00
Response::async(rx.map_err(|_| ()))
2017-12-29 01:25:47 +01:00
} else {
info!("Force shutdown http worker, {} connections", num);
self.settings.head().traverse::<TcpStream, H>();
2018-02-01 10:08:08 +01:00
Response::reply(Ok(false))
2017-12-29 01:25:47 +01:00
}
2017-12-28 22:07:29 +01:00
}
}
2017-12-28 21:38:37 +01:00
#[derive(Clone)]
pub(crate) enum StreamHandlerType {
Normal,
2018-04-14 01:02:01 +02:00
#[cfg(feature = "tls")]
2017-12-28 21:38:37 +01:00
Tls(TlsAcceptor),
2018-04-14 01:02:01 +02:00
#[cfg(feature = "alpn")]
2017-12-28 21:38:37 +01:00
Alpn(SslAcceptor),
2018-07-29 08:43:04 +02:00
#[cfg(feature = "rust-tls")]
Rustls(Arc<ServerConfig>),
2017-12-28 21:38:37 +01:00
}
impl StreamHandlerType {
2018-08-03 08:17:10 +02:00
pub fn is_ssl(&self) -> bool {
match *self {
StreamHandlerType::Normal => false,
#[cfg(feature = "tls")]
StreamHandlerType::Tls(_) => true,
#[cfg(feature = "alpn")]
StreamHandlerType::Alpn(_) => true,
#[cfg(feature = "rust-tls")]
StreamHandlerType::Rustls(_) => true,
}
}
2018-04-14 01:02:01 +02:00
fn handle<H: HttpHandler>(
2018-05-25 06:03:16 +02:00
&mut self, h: Rc<WorkerSettings<H>>, msg: Conn<net::TcpStream>,
2018-04-14 01:02:01 +02:00
) {
2017-12-28 21:38:37 +01:00
match *self {
StreamHandlerType::Normal => {
2018-01-04 18:32:47 +01:00
let _ = msg.io.set_nodelay(true);
2018-05-25 06:03:16 +02:00
let io = TcpStream::from_std(msg.io, &Handle::default())
2017-12-28 21:38:37 +01:00
.expect("failed to associate TCP stream");
2018-05-29 19:31:37 +02:00
current_thread::spawn(HttpChannel::new(h, io, msg.peer, msg.http2));
2017-12-28 21:38:37 +01:00
}
2018-04-14 01:02:01 +02:00
#[cfg(feature = "tls")]
2017-12-28 21:38:37 +01:00
StreamHandlerType::Tls(ref acceptor) => {
let Conn {
io, peer, http2, ..
} = msg;
2018-01-04 18:32:47 +01:00
let _ = io.set_nodelay(true);
2018-05-25 06:03:16 +02:00
let io = TcpStream::from_std(io, &Handle::default())
2017-12-28 21:38:37 +01:00
.expect("failed to associate TCP stream");
h.ssl_conn_add();
2017-12-28 21:38:37 +01:00
2018-05-29 19:59:24 +02:00
current_thread::spawn(TlsAcceptorExt::accept_async(acceptor, io).then(
2018-05-25 06:03:16 +02:00
move |res| {
h.ssl_conn_del();
2018-05-25 06:03:16 +02:00
match res {
2018-05-29 19:31:37 +02:00
Ok(io) => current_thread::spawn(HttpChannel::new(
h, io, peer, http2,
)),
2018-05-25 06:03:16 +02:00
Err(err) => {
trace!("Error during handling tls connection: {}", err)
}
};
future::result(Ok(()))
},
));
2017-12-28 21:38:37 +01:00
}
2018-04-14 01:02:01 +02:00
#[cfg(feature = "alpn")]
2017-12-28 21:38:37 +01:00
StreamHandlerType::Alpn(ref acceptor) => {
2018-04-29 18:09:08 +02:00
let Conn { io, peer, .. } = msg;
2018-01-04 18:32:47 +01:00
let _ = io.set_nodelay(true);
2018-05-25 06:03:16 +02:00
let io = TcpStream::from_std(io, &Handle::default())
2017-12-28 21:38:37 +01:00
.expect("failed to associate TCP stream");
h.ssl_conn_add();
2017-12-28 21:38:37 +01:00
2018-05-29 19:59:24 +02:00
current_thread::spawn(SslAcceptorExt::accept_async(acceptor, io).then(
2018-05-25 06:03:16 +02:00
move |res| {
h.ssl_conn_del();
2018-05-25 06:03:16 +02:00
match res {
Ok(io) => {
let http2 = if let Some(p) =
io.get_ref().ssl().selected_alpn_protocol()
{
p.len() == 2 && &p == b"h2"
} else {
false
};
2018-05-29 19:31:37 +02:00
current_thread::spawn(HttpChannel::new(
h, io, peer, http2,
));
2018-05-25 06:03:16 +02:00
}
Err(err) => {
trace!("Error during handling tls connection: {}", err)
}
};
future::result(Ok(()))
},
));
2017-12-28 21:38:37 +01:00
}
2018-07-29 08:43:04 +02:00
#[cfg(feature = "rust-tls")]
StreamHandlerType::Rustls(ref acceptor) => {
let Conn { io, peer, .. } = msg;
let _ = io.set_nodelay(true);
let io = TcpStream::from_std(io, &Handle::default())
.expect("failed to associate TCP stream");
h.ssl_conn_add();
2018-07-29 08:43:04 +02:00
current_thread::spawn(ServerConfigExt::accept_async(acceptor, io).then(
move |res| {
h.ssl_conn_del();
2018-07-29 08:43:04 +02:00
match res {
Ok(io) => {
let http2 = if let Some(p) =
io.get_ref().1.get_alpn_protocol()
{
p.len() == 2 && &p == &"h2"
} else {
false
};
current_thread::spawn(HttpChannel::new(
h, io, peer, http2,
));
}
Err(err) => {
trace!("Error during handling tls connection: {}", err)
}
};
future::result(Ok(()))
},
));
}
2017-12-28 21:38:37 +01:00
}
}
pub(crate) fn scheme(&self) -> &'static str {
match *self {
StreamHandlerType::Normal => "http",
#[cfg(feature = "tls")]
2018-05-21 05:47:20 +02:00
StreamHandlerType::Tls(_) => "https",
#[cfg(feature = "alpn")]
2018-05-21 05:47:20 +02:00
StreamHandlerType::Alpn(_) => "https",
2018-07-29 08:43:04 +02:00
#[cfg(feature = "rust-tls")]
StreamHandlerType::Rustls(_) => "https",
}
}
2017-12-28 21:38:37 +01:00
}