2019-11-14 13:38:24 +01:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
use std::time::{Duration, Instant};
|
2018-09-07 23:34:31 +02:00
|
|
|
use std::{io, mem, net};
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2018-12-10 06:51:35 +01:00
|
|
|
use actix_rt::{spawn, Arbiter, System};
|
2019-11-14 13:38:24 +01:00
|
|
|
use futures::channel::mpsc::{unbounded, UnboundedReceiver};
|
|
|
|
use futures::future::ready;
|
|
|
|
use futures::stream::FuturesUnordered;
|
|
|
|
use futures::{ready, Future, FutureExt, Stream, StreamExt};
|
2018-12-06 23:04:42 +01:00
|
|
|
use log::{error, info};
|
2018-08-19 19:47:04 +02:00
|
|
|
use net2::TcpBuilder;
|
|
|
|
use num_cpus;
|
2019-11-14 13:38:24 +01:00
|
|
|
use tokio_net::tcp::TcpStream;
|
|
|
|
use tokio_timer::delay;
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2018-12-11 06:06:54 +01:00
|
|
|
use crate::accept::{AcceptLoop, AcceptNotify, Command};
|
2019-03-15 04:09:34 +01:00
|
|
|
use crate::config::{ConfiguredService, ServiceConfig};
|
2018-12-11 06:06:54 +01:00
|
|
|
use crate::server::{Server, ServerCommand};
|
2019-11-14 13:38:24 +01:00
|
|
|
use crate::service::{InternalServiceFactory, ServiceFactory, StreamNewService};
|
|
|
|
// use crate::signals::{Signal, Signals};
|
2019-07-18 13:05:40 +02:00
|
|
|
use crate::socket::StdListener;
|
2018-12-11 06:06:54 +01:00
|
|
|
use crate::worker::{self, Worker, WorkerAvailability, WorkerClient};
|
2019-03-13 00:49:57 +01:00
|
|
|
use crate::{ssl, Token};
|
2018-12-10 05:30:04 +01:00
|
|
|
|
2018-12-10 06:51:35 +01:00
|
|
|
/// Server builder
|
2018-12-10 05:30:04 +01:00
|
|
|
pub struct ServerBuilder {
|
2018-08-19 19:47:04 +02:00
|
|
|
threads: usize,
|
2018-11-03 17:09:14 +01:00
|
|
|
token: Token,
|
2019-03-11 20:01:55 +01:00
|
|
|
backlog: i32,
|
2018-09-14 08:46:01 +02:00
|
|
|
workers: Vec<(usize, WorkerClient)>,
|
2019-07-18 13:05:40 +02:00
|
|
|
services: Vec<Box<dyn InternalServiceFactory>>,
|
|
|
|
sockets: Vec<(Token, StdListener)>,
|
2018-08-19 19:47:04 +02:00
|
|
|
accept: AcceptLoop,
|
|
|
|
exit: bool,
|
2018-09-27 05:40:45 +02:00
|
|
|
shutdown_timeout: Duration,
|
2018-08-19 19:47:04 +02:00
|
|
|
no_signals: bool,
|
2018-12-10 06:51:35 +01:00
|
|
|
cmd: UnboundedReceiver<ServerCommand>,
|
|
|
|
server: Server,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ServerBuilder {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
2018-12-10 05:30:04 +01:00
|
|
|
impl ServerBuilder {
|
2018-12-10 06:51:35 +01:00
|
|
|
/// Create new Server builder instance
|
|
|
|
pub fn new() -> ServerBuilder {
|
|
|
|
let (tx, rx) = unbounded();
|
|
|
|
let server = Server::new(tx);
|
|
|
|
|
2018-12-10 05:30:04 +01:00
|
|
|
ServerBuilder {
|
2018-08-19 19:47:04 +02:00
|
|
|
threads: num_cpus::get(),
|
2018-11-03 17:09:14 +01:00
|
|
|
token: Token(0),
|
2018-08-19 19:47:04 +02:00
|
|
|
workers: Vec::new(),
|
|
|
|
services: Vec::new(),
|
|
|
|
sockets: Vec::new(),
|
2018-12-10 06:51:35 +01:00
|
|
|
accept: AcceptLoop::new(server.clone()),
|
2019-03-11 20:01:55 +01:00
|
|
|
backlog: 2048,
|
2018-08-19 19:47:04 +02:00
|
|
|
exit: false,
|
2018-09-27 05:40:45 +02:00
|
|
|
shutdown_timeout: Duration::from_secs(30),
|
2018-08-19 19:47:04 +02:00
|
|
|
no_signals: false,
|
2018-12-10 06:51:35 +01:00
|
|
|
cmd: rx,
|
|
|
|
server,
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set number of workers to start.
|
|
|
|
///
|
2018-12-12 23:16:16 +01:00
|
|
|
/// By default server uses number of available logical cpu as workers
|
2018-08-19 19:47:04 +02:00
|
|
|
/// count.
|
|
|
|
pub fn workers(mut self, num: usize) -> Self {
|
|
|
|
self.threads = num;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-11 20:01:55 +01:00
|
|
|
/// Set the maximum number of pending connections.
|
|
|
|
///
|
|
|
|
/// This refers to the number of clients that can be waiting to be served.
|
|
|
|
/// Exceeding this number results in the client getting an error when
|
|
|
|
/// attempting to connect. It should only affect servers under significant
|
|
|
|
/// load.
|
|
|
|
///
|
|
|
|
/// Generally set in the 64-2048 range. Default value is 2048.
|
|
|
|
///
|
|
|
|
/// This method should be called before `bind()` method call.
|
|
|
|
pub fn backlog(mut self, num: i32) -> Self {
|
|
|
|
self.backlog = num;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-08-19 19:47:04 +02:00
|
|
|
/// Sets the maximum per-worker number of concurrent connections.
|
|
|
|
///
|
|
|
|
/// All socket listeners will stop accepting connections when this limit is
|
|
|
|
/// reached for each worker.
|
|
|
|
///
|
2018-09-07 20:35:25 +02:00
|
|
|
/// By default max connections is set to a 25k per worker.
|
|
|
|
pub fn maxconn(self, num: usize) -> Self {
|
2018-09-08 18:36:38 +02:00
|
|
|
worker::max_concurrent_connections(num);
|
2018-08-19 19:47:04 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-05 00:41:16 +01:00
|
|
|
/// Sets the maximum per-worker concurrent connection establish process.
|
|
|
|
///
|
|
|
|
/// All listeners will stop accepting connections when this limit is reached. It
|
|
|
|
/// can be used to limit the global SSL CPU usage.
|
2018-08-19 19:47:04 +02:00
|
|
|
///
|
2019-03-05 00:41:16 +01:00
|
|
|
/// By default max connections is set to a 256.
|
2019-03-05 01:16:39 +01:00
|
|
|
pub fn maxconnrate(self, num: usize) -> Self {
|
2019-03-05 00:41:16 +01:00
|
|
|
ssl::max_concurrent_ssl_connect(num);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stop actix system.
|
2018-08-19 19:47:04 +02:00
|
|
|
pub fn system_exit(mut self) -> Self {
|
|
|
|
self.exit = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Disable signal handling
|
|
|
|
pub fn disable_signals(mut self) -> Self {
|
|
|
|
self.no_signals = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-09-27 05:40:45 +02:00
|
|
|
/// Timeout for graceful workers shutdown in seconds.
|
2018-08-19 19:47:04 +02:00
|
|
|
///
|
|
|
|
/// After receiving a stop signal, workers have this much time to finish
|
|
|
|
/// serving requests. Workers still alive after the timeout are force
|
|
|
|
/// dropped.
|
|
|
|
///
|
|
|
|
/// By default shutdown timeout sets to 30 seconds.
|
2019-05-18 19:56:41 +02:00
|
|
|
pub fn shutdown_timeout(mut self, sec: u64) -> Self {
|
|
|
|
self.shutdown_timeout = Duration::from_secs(sec);
|
2018-08-19 19:47:04 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-12-12 23:16:16 +01:00
|
|
|
/// Execute external configuration as part of the server building
|
|
|
|
/// process.
|
2018-08-22 20:36:56 +02:00
|
|
|
///
|
|
|
|
/// This function is useful for moving parts of configuration to a
|
2018-11-03 17:09:14 +01:00
|
|
|
/// different module or even library.
|
2018-12-10 06:51:35 +01:00
|
|
|
pub fn configure<F>(mut self, f: F) -> io::Result<ServerBuilder>
|
2018-08-22 20:36:56 +02:00
|
|
|
where
|
2018-11-03 17:09:14 +01:00
|
|
|
F: Fn(&mut ServiceConfig) -> io::Result<()>,
|
2018-08-22 20:36:56 +02:00
|
|
|
{
|
2019-03-11 20:01:55 +01:00
|
|
|
let mut cfg = ServiceConfig::new(self.threads, self.backlog);
|
2018-11-03 17:09:14 +01:00
|
|
|
|
|
|
|
f(&mut cfg)?;
|
|
|
|
|
2018-12-12 23:16:16 +01:00
|
|
|
if let Some(apply) = cfg.apply {
|
|
|
|
let mut srv = ConfiguredService::new(apply);
|
|
|
|
for (name, lst) in cfg.services {
|
|
|
|
let token = self.token.next();
|
2019-03-12 22:14:21 +01:00
|
|
|
srv.stream(token, name, lst.local_addr()?);
|
2019-07-18 13:05:40 +02:00
|
|
|
self.sockets.push((token, StdListener::Tcp(lst)));
|
2018-12-12 23:16:16 +01:00
|
|
|
}
|
|
|
|
self.services.push(Box::new(srv));
|
2018-11-03 17:09:14 +01:00
|
|
|
}
|
2018-12-12 23:16:16 +01:00
|
|
|
self.threads = cfg.threads;
|
2018-11-03 17:09:14 +01:00
|
|
|
|
|
|
|
Ok(self)
|
2018-08-22 20:36:56 +02:00
|
|
|
}
|
|
|
|
|
2018-12-10 06:51:35 +01:00
|
|
|
/// Add new service to the server.
|
2018-09-18 05:19:48 +02:00
|
|
|
pub fn bind<F, U, N: AsRef<str>>(mut self, name: N, addr: U, factory: F) -> io::Result<Self>
|
2018-08-19 19:47:04 +02:00
|
|
|
where
|
2019-07-18 13:05:40 +02:00
|
|
|
F: ServiceFactory<TcpStream>,
|
2018-08-19 19:47:04 +02:00
|
|
|
U: net::ToSocketAddrs,
|
|
|
|
{
|
2019-03-11 20:01:55 +01:00
|
|
|
let sockets = bind_addr(addr, self.backlog)?;
|
2018-08-19 19:47:04 +02:00
|
|
|
|
|
|
|
for lst in sockets {
|
2019-03-09 16:27:56 +01:00
|
|
|
let token = self.token.next();
|
|
|
|
self.services.push(StreamNewService::create(
|
|
|
|
name.as_ref().to_string(),
|
|
|
|
token,
|
|
|
|
factory.clone(),
|
|
|
|
lst.local_addr()?,
|
|
|
|
));
|
2019-07-18 13:05:40 +02:00
|
|
|
self.sockets.push((token, StdListener::Tcp(lst)));
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:05:40 +02:00
|
|
|
#[cfg(all(unix, feature = "uds"))]
|
|
|
|
/// Add new unix domain service to the server.
|
2019-09-16 07:07:46 +02:00
|
|
|
pub fn bind_uds<F, U, N>(self, name: N, addr: U, factory: F) -> io::Result<Self>
|
2019-07-18 13:05:40 +02:00
|
|
|
where
|
|
|
|
F: ServiceFactory<tokio_uds::UnixStream>,
|
|
|
|
N: AsRef<str>,
|
|
|
|
U: AsRef<std::path::Path>,
|
|
|
|
{
|
|
|
|
use std::os::unix::net::UnixListener;
|
|
|
|
|
2019-09-16 07:07:46 +02:00
|
|
|
// The path must not exist when we try to bind.
|
|
|
|
// Try to remove it to avoid bind error.
|
|
|
|
if let Err(e) = std::fs::remove_file(addr.as_ref()) {
|
|
|
|
// NotFound is expected and not an issue. Anything else is.
|
|
|
|
if e.kind() != std::io::ErrorKind::NotFound {
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
}
|
2019-07-18 13:05:40 +02:00
|
|
|
|
|
|
|
let lst = UnixListener::bind(addr)?;
|
2019-09-16 07:07:46 +02:00
|
|
|
self.listen_uds(name, lst, factory)
|
|
|
|
}
|
2019-07-18 13:05:40 +02:00
|
|
|
|
2019-09-16 07:07:46 +02:00
|
|
|
#[cfg(all(unix, feature = "uds"))]
|
|
|
|
/// Add new unix domain service to the server.
|
|
|
|
/// Useful when running as a systemd service and
|
|
|
|
/// a socket FD can be acquired using the systemd crate.
|
|
|
|
pub fn listen_uds<F, N: AsRef<str>>(
|
|
|
|
mut self,
|
|
|
|
name: N,
|
|
|
|
lst: std::os::unix::net::UnixListener,
|
|
|
|
factory: F,
|
|
|
|
) -> io::Result<Self>
|
|
|
|
where
|
|
|
|
F: ServiceFactory<tokio_uds::UnixStream>,
|
|
|
|
{
|
|
|
|
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
2019-07-18 13:05:40 +02:00
|
|
|
let token = self.token.next();
|
|
|
|
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
|
|
|
|
self.services.push(StreamNewService::create(
|
|
|
|
name.as_ref().to_string(),
|
|
|
|
token,
|
|
|
|
factory.clone(),
|
|
|
|
addr,
|
|
|
|
));
|
|
|
|
self.sockets.push((token, StdListener::Uds(lst)));
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2018-12-10 06:51:35 +01:00
|
|
|
/// Add new service to the server.
|
2018-09-18 05:19:48 +02:00
|
|
|
pub fn listen<F, N: AsRef<str>>(
|
2018-10-30 04:29:47 +01:00
|
|
|
mut self,
|
|
|
|
name: N,
|
|
|
|
lst: net::TcpListener,
|
|
|
|
factory: F,
|
2019-03-09 04:43:13 +01:00
|
|
|
) -> io::Result<Self>
|
2018-08-19 19:47:04 +02:00
|
|
|
where
|
2019-07-18 13:05:40 +02:00
|
|
|
F: ServiceFactory<TcpStream>,
|
2018-08-19 19:47:04 +02:00
|
|
|
{
|
2018-11-03 17:09:14 +01:00
|
|
|
let token = self.token.next();
|
|
|
|
self.services.push(StreamNewService::create(
|
|
|
|
name.as_ref().to_string(),
|
|
|
|
token,
|
|
|
|
factory,
|
2019-03-09 16:27:56 +01:00
|
|
|
lst.local_addr()?,
|
2018-11-03 17:09:14 +01:00
|
|
|
));
|
2019-07-18 13:05:40 +02:00
|
|
|
self.sockets.push((token, StdListener::Tcp(lst)));
|
2019-03-09 04:43:13 +01:00
|
|
|
Ok(self)
|
2018-09-27 05:40:45 +02:00
|
|
|
}
|
|
|
|
|
2018-08-19 19:47:04 +02:00
|
|
|
/// Spawn new thread and start listening for incoming connections.
|
|
|
|
///
|
|
|
|
/// This method spawns new thread and starts new actix system. Other than
|
|
|
|
/// that it is similar to `start()` method. This method blocks.
|
|
|
|
///
|
|
|
|
/// This methods panics if no socket addresses get bound.
|
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// use actix_web::*;
|
|
|
|
///
|
2019-03-09 01:26:30 +01:00
|
|
|
/// fn main() -> std::io::Result<()> {
|
2018-08-19 19:47:04 +02:00
|
|
|
/// Server::new().
|
|
|
|
/// .service(
|
2019-03-09 01:26:30 +01:00
|
|
|
/// HttpServer::new(|| App::new().service(web::service("/").to(|| HttpResponse::Ok())))
|
2018-08-19 19:47:04 +02:00
|
|
|
/// .bind("127.0.0.1:0")
|
2019-03-09 01:26:30 +01:00
|
|
|
/// .run()
|
2018-08-19 19:47:04 +02:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-03-09 01:26:30 +01:00
|
|
|
pub fn run(self) -> io::Result<()> {
|
2018-08-19 19:47:04 +02:00
|
|
|
let sys = System::new("http-server");
|
|
|
|
self.start();
|
2019-03-09 01:26:30 +01:00
|
|
|
sys.run()
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
2018-12-10 06:51:35 +01:00
|
|
|
/// Starts processing incoming connections and return server controller.
|
2018-12-10 05:30:04 +01:00
|
|
|
pub fn start(mut self) -> Server {
|
2018-08-19 19:47:04 +02:00
|
|
|
if self.sockets.is_empty() {
|
2018-12-10 06:51:35 +01:00
|
|
|
panic!("Server should have at least one bound socket");
|
2018-08-19 19:47:04 +02:00
|
|
|
} else {
|
2018-09-18 05:19:48 +02:00
|
|
|
info!("Starting {} workers", self.threads);
|
2018-08-19 19:47:04 +02:00
|
|
|
|
|
|
|
// start workers
|
|
|
|
let mut workers = Vec::new();
|
|
|
|
for idx in 0..self.threads {
|
2018-09-14 08:46:01 +02:00
|
|
|
let worker = self.start_worker(idx, self.accept.get_notify());
|
|
|
|
workers.push(worker.clone());
|
|
|
|
self.workers.push((idx, worker));
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// start accept thread
|
|
|
|
for sock in &self.sockets {
|
2019-07-18 13:05:40 +02:00
|
|
|
info!("Starting server on {}", sock.1);
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
2018-12-10 06:51:35 +01:00
|
|
|
self.accept
|
2018-08-19 19:47:04 +02:00
|
|
|
.start(mem::replace(&mut self.sockets, Vec::new()), workers);
|
|
|
|
|
2018-12-11 06:06:54 +01:00
|
|
|
// handle signals
|
|
|
|
if !self.no_signals {
|
2019-11-14 13:38:24 +01:00
|
|
|
// Signals::start(self.server.clone());
|
2018-12-11 06:06:54 +01:00
|
|
|
}
|
|
|
|
|
2018-08-19 19:47:04 +02:00
|
|
|
// start http server actor
|
2018-12-10 06:51:35 +01:00
|
|
|
let server = self.server.clone();
|
|
|
|
spawn(self);
|
|
|
|
server
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-14 08:46:01 +02:00
|
|
|
fn start_worker(&self, idx: usize, notify: AcceptNotify) -> WorkerClient {
|
2018-11-01 23:33:35 +01:00
|
|
|
let (tx1, rx1) = unbounded();
|
|
|
|
let (tx2, rx2) = unbounded();
|
2018-09-27 05:40:45 +02:00
|
|
|
let timeout = self.shutdown_timeout;
|
2018-09-07 22:06:51 +02:00
|
|
|
let avail = WorkerAvailability::new(notify);
|
2018-11-01 23:33:35 +01:00
|
|
|
let worker = WorkerClient::new(idx, tx1, tx2, avail.clone());
|
2019-07-18 13:05:40 +02:00
|
|
|
let services: Vec<Box<dyn InternalServiceFactory>> =
|
2018-08-19 19:47:04 +02:00
|
|
|
self.services.iter().map(|v| v.clone_factory()).collect();
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
Arbiter::new().send(
|
|
|
|
async move {
|
|
|
|
Worker::start(rx1, rx2, services, avail, timeout);
|
|
|
|
}
|
2019-11-18 09:30:04 +01:00
|
|
|
.boxed(),
|
2019-11-14 13:38:24 +01:00
|
|
|
);
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2018-09-14 08:46:01 +02:00
|
|
|
worker
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
2018-12-10 06:51:35 +01:00
|
|
|
|
2018-12-11 06:06:54 +01:00
|
|
|
fn handle_cmd(&mut self, item: ServerCommand) {
|
|
|
|
match item {
|
|
|
|
ServerCommand::Pause(tx) => {
|
|
|
|
self.accept.send(Command::Pause);
|
|
|
|
let _ = tx.send(());
|
|
|
|
}
|
|
|
|
ServerCommand::Resume(tx) => {
|
|
|
|
self.accept.send(Command::Resume);
|
|
|
|
let _ = tx.send(());
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
// ServerCommand::Signal(sig) => {
|
|
|
|
// Signals support
|
|
|
|
// Handle `SIGINT`, `SIGTERM`, `SIGQUIT` signals and stop actix system
|
|
|
|
// match sig {
|
|
|
|
// Signal::Int => {
|
|
|
|
// info!("SIGINT received, exiting");
|
|
|
|
// self.exit = true;
|
|
|
|
// self.handle_cmd(ServerCommand::Stop {
|
|
|
|
// graceful: false,
|
|
|
|
// completion: None,
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
// Signal::Term => {
|
|
|
|
// info!("SIGTERM received, stopping");
|
|
|
|
// self.exit = true;
|
|
|
|
// self.handle_cmd(ServerCommand::Stop {
|
|
|
|
// graceful: true,
|
|
|
|
// completion: None,
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
// Signal::Quit => {
|
|
|
|
// info!("SIGQUIT received, exiting");
|
|
|
|
// self.exit = true;
|
|
|
|
// self.handle_cmd(ServerCommand::Stop {
|
|
|
|
// graceful: false,
|
|
|
|
// completion: None,
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
// _ => (),
|
|
|
|
// }
|
|
|
|
// }
|
2018-12-11 06:06:54 +01:00
|
|
|
ServerCommand::Stop {
|
|
|
|
graceful,
|
|
|
|
completion,
|
|
|
|
} => {
|
|
|
|
let exit = self.exit;
|
|
|
|
|
|
|
|
// stop accept thread
|
|
|
|
self.accept.send(Command::Stop);
|
|
|
|
|
|
|
|
// stop workers
|
2019-03-30 20:09:02 +01:00
|
|
|
if !self.workers.is_empty() && graceful {
|
2018-12-11 06:06:54 +01:00
|
|
|
spawn(
|
2019-11-14 13:38:24 +01:00
|
|
|
self.workers
|
|
|
|
.iter()
|
|
|
|
.map(move |worker| worker.1.stop(graceful))
|
|
|
|
.collect::<FuturesUnordered<_>>()
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.then(move |_| {
|
|
|
|
if let Some(tx) = completion {
|
|
|
|
let _ = tx.send(());
|
|
|
|
}
|
|
|
|
if exit {
|
|
|
|
spawn(
|
|
|
|
async {
|
|
|
|
delay(Instant::now() + Duration::from_millis(300))
|
|
|
|
.await;
|
|
|
|
System::current().stop();
|
|
|
|
}
|
2019-11-18 09:30:04 +01:00
|
|
|
.boxed(),
|
2019-11-14 13:38:24 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
ready(())
|
|
|
|
}),
|
2018-12-11 06:06:54 +01:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// we need to stop system if server was spawned
|
|
|
|
if self.exit {
|
2019-11-14 13:38:24 +01:00
|
|
|
spawn(
|
|
|
|
delay(Instant::now() + Duration::from_millis(300)).then(|_| {
|
|
|
|
System::current().stop();
|
|
|
|
ready(())
|
|
|
|
}),
|
|
|
|
);
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
2018-12-11 06:06:54 +01:00
|
|
|
if let Some(tx) = completion {
|
|
|
|
let _ = tx.send(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
ServerCommand::WorkerFaulted(idx) => {
|
2018-12-11 06:06:54 +01:00
|
|
|
let mut found = false;
|
|
|
|
for i in 0..self.workers.len() {
|
|
|
|
if self.workers[i].0 == idx {
|
|
|
|
self.workers.swap_remove(i);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
|
|
|
error!("Worker has died {:?}, restarting", idx);
|
|
|
|
|
|
|
|
let mut new_idx = self.workers.len();
|
|
|
|
'found: loop {
|
2018-08-19 19:47:04 +02:00
|
|
|
for i in 0..self.workers.len() {
|
2018-12-11 06:06:54 +01:00
|
|
|
if self.workers[i].0 == new_idx {
|
|
|
|
new_idx += 1;
|
|
|
|
continue 'found;
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-11 06:06:54 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2018-12-11 06:06:54 +01:00
|
|
|
let worker = self.start_worker(new_idx, self.accept.get_notify());
|
|
|
|
self.workers.push((new_idx, worker.clone()));
|
|
|
|
self.accept.send(Command::Worker(worker));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 06:51:35 +01:00
|
|
|
|
2018-12-11 06:06:54 +01:00
|
|
|
impl Future for ServerBuilder {
|
2019-11-14 13:38:24 +01:00
|
|
|
type Output = ();
|
2018-12-11 06:06:54 +01:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2018-12-11 06:06:54 +01:00
|
|
|
loop {
|
2019-11-14 13:38:24 +01:00
|
|
|
match ready!(Pin::new(&mut self.cmd).poll_next(cx)) {
|
|
|
|
Some(it) => self.as_mut().get_mut().handle_cmd(it),
|
|
|
|
None => {
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 20:01:55 +01:00
|
|
|
pub(super) fn bind_addr<S: net::ToSocketAddrs>(
|
|
|
|
addr: S,
|
|
|
|
backlog: i32,
|
|
|
|
) -> io::Result<Vec<net::TcpListener>> {
|
2018-08-19 19:47:04 +02:00
|
|
|
let mut err = None;
|
|
|
|
let mut succ = false;
|
|
|
|
let mut sockets = Vec::new();
|
|
|
|
for addr in addr.to_socket_addrs()? {
|
2019-03-11 20:01:55 +01:00
|
|
|
match create_tcp_listener(addr, backlog) {
|
2018-08-19 19:47:04 +02:00
|
|
|
Ok(lst) => {
|
|
|
|
succ = true;
|
|
|
|
sockets.push(lst);
|
|
|
|
}
|
|
|
|
Err(e) => err = Some(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !succ {
|
|
|
|
if let Some(e) = err.take() {
|
|
|
|
Err(e)
|
|
|
|
} else {
|
|
|
|
Err(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
"Can not bind to address.",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(sockets)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 20:01:55 +01:00
|
|
|
fn create_tcp_listener(addr: net::SocketAddr, backlog: i32) -> io::Result<net::TcpListener> {
|
2018-08-19 19:47:04 +02:00
|
|
|
let builder = match addr {
|
|
|
|
net::SocketAddr::V4(_) => TcpBuilder::new_v4()?,
|
|
|
|
net::SocketAddr::V6(_) => TcpBuilder::new_v6()?,
|
|
|
|
};
|
|
|
|
builder.reuse_address(true)?;
|
|
|
|
builder.bind(addr)?;
|
2019-03-11 20:01:55 +01:00
|
|
|
Ok(builder.listen(backlog)?)
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|