2020-12-29 00:44:53 +01:00
|
|
|
use std::future::Future;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2019-12-05 11:40:24 +01:00
|
|
|
use std::time::Duration;
|
2020-12-29 00:44:53 +01:00
|
|
|
use std::{io, mem};
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2019-12-02 06:30:27 +01:00
|
|
|
use actix_rt::net::TcpStream;
|
2020-12-29 00:44:53 +01:00
|
|
|
use actix_rt::time::{sleep_until, Instant};
|
2019-12-05 11:40:24 +01:00
|
|
|
use actix_rt::{spawn, System};
|
2018-12-06 23:04:42 +01:00
|
|
|
use log::{error, info};
|
2020-12-29 00:44:53 +01:00
|
|
|
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver};
|
|
|
|
use tokio::sync::oneshot;
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
use crate::accept::AcceptLoop;
|
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};
|
2019-11-26 12:03:52 +01:00
|
|
|
use crate::signals::{Signal, Signals};
|
2020-12-29 00:44:53 +01:00
|
|
|
use crate::socket::{MioListener, StdSocketAddr, StdTcpListener, ToSocketAddrs};
|
|
|
|
use crate::socket::{MioTcpListener, MioTcpSocket};
|
|
|
|
use crate::waker_queue::{WakerInterest, WakerQueue};
|
|
|
|
use crate::worker::{self, Worker, WorkerAvailability, WorkerHandle};
|
|
|
|
use crate::{join_all, 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,
|
2020-12-29 00:44:53 +01:00
|
|
|
backlog: u32,
|
|
|
|
handles: Vec<(usize, WorkerHandle)>,
|
2019-07-18 13:05:40 +02:00
|
|
|
services: Vec<Box<dyn InternalServiceFactory>>,
|
2020-12-29 00:44:53 +01:00
|
|
|
sockets: Vec<(Token, String, MioListener)>,
|
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,
|
2019-11-26 11:33:45 +01:00
|
|
|
notify: Vec<oneshot::Sender<()>>,
|
2018-12-10 06:51:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-12-29 00:44:53 +01:00
|
|
|
let (tx, rx) = unbounded_channel();
|
2018-12-10 06:51:35 +01:00
|
|
|
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(),
|
2020-12-29 00:44:53 +01:00
|
|
|
token: Token::default(),
|
|
|
|
handles: Vec::new(),
|
2018-08-19 19:47:04 +02:00
|
|
|
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,
|
2019-11-26 11:33:45 +01:00
|
|
|
notify: Vec::new(),
|
2018-12-10 06:51:35 +01:00
|
|
|
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
|
2020-08-18 17:44:22 +02:00
|
|
|
/// count. Workers must be greater than 0.
|
2018-08-19 19:47:04 +02:00
|
|
|
pub fn workers(mut self, num: usize) -> Self {
|
2020-08-18 17:44:22 +02:00
|
|
|
assert_ne!(num, 0, "workers must be greater than 0");
|
2018-08-19 19:47:04 +02:00
|
|
|
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.
|
2020-12-29 00:44:53 +01:00
|
|
|
pub fn backlog(mut self, num: u32) -> Self {
|
2019-03-11 20:01:55 +01:00
|
|
|
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
|
|
|
/// 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-12-29 05:07:46 +01:00
|
|
|
srv.stream(token, name.clone(), lst.local_addr()?);
|
2020-12-29 00:44:53 +01:00
|
|
|
self.sockets.push((token, name, MioListener::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>,
|
2020-12-29 00:44:53 +01:00
|
|
|
U: ToSocketAddrs,
|
2018-08-19 19:47:04 +02:00
|
|
|
{
|
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-12-29 05:07:46 +01:00
|
|
|
self.sockets
|
2020-12-29 00:44:53 +01:00
|
|
|
.push((token, name.as_ref().to_string(), MioListener::Tcp(lst)));
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
#[cfg(unix)]
|
2019-07-18 13:05:40 +02:00
|
|
|
/// 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
|
2019-12-02 06:30:27 +01:00
|
|
|
F: ServiceFactory<actix_rt::net::UnixStream>,
|
2019-07-18 13:05:40 +02:00
|
|
|
N: AsRef<str>,
|
|
|
|
U: AsRef<std::path::Path>,
|
|
|
|
{
|
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
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
let lst = crate::socket::StdUnixListener::bind(addr)?;
|
2019-09-16 07:07:46 +02:00
|
|
|
self.listen_uds(name, lst, factory)
|
|
|
|
}
|
2019-07-18 13:05:40 +02:00
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
#[cfg(unix)]
|
2019-09-16 07:07:46 +02:00
|
|
|
/// 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,
|
2020-12-29 00:44:53 +01:00
|
|
|
lst: crate::socket::StdUnixListener,
|
2019-09-16 07:07:46 +02:00
|
|
|
factory: F,
|
|
|
|
) -> io::Result<Self>
|
|
|
|
where
|
2019-12-02 06:30:27 +01:00
|
|
|
F: ServiceFactory<actix_rt::net::UnixStream>,
|
2019-09-16 07:07:46 +02:00
|
|
|
{
|
2020-12-29 00:44:53 +01:00
|
|
|
use std::net::{IpAddr, Ipv4Addr};
|
|
|
|
lst.set_nonblocking(true)?;
|
2019-07-18 13:05:40 +02:00
|
|
|
let token = self.token.next();
|
2020-12-29 00:44:53 +01:00
|
|
|
let addr = StdSocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
|
2019-07-18 13:05:40 +02:00
|
|
|
self.services.push(StreamNewService::create(
|
|
|
|
name.as_ref().to_string(),
|
|
|
|
token,
|
2020-01-28 12:27:33 +01:00
|
|
|
factory,
|
2019-07-18 13:05:40 +02:00
|
|
|
addr,
|
|
|
|
));
|
2019-12-29 05:07:46 +01:00
|
|
|
self.sockets
|
2020-12-29 00:44:53 +01:00
|
|
|
.push((token, name.as_ref().to_string(), MioListener::from(lst)));
|
2019-07-18 13:05:40 +02:00
|
|
|
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,
|
2020-12-29 00:44:53 +01:00
|
|
|
lst: StdTcpListener,
|
2018-10-30 04:29:47 +01:00
|
|
|
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
|
|
|
{
|
2020-12-29 00:44:53 +01:00
|
|
|
lst.set_nonblocking(true)?;
|
|
|
|
let addr = lst.local_addr()?;
|
|
|
|
|
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,
|
2020-12-29 00:44:53 +01:00
|
|
|
addr,
|
2018-11-03 17:09:14 +01:00
|
|
|
));
|
2020-12-29 00:44:53 +01:00
|
|
|
|
2019-12-29 05:07:46 +01:00
|
|
|
self.sockets
|
2020-12-29 00:44:53 +01:00
|
|
|
.push((token, name.as_ref().to_string(), MioListener::from(lst)));
|
2019-03-09 04:43:13 +01:00
|
|
|
Ok(self)
|
2018-09-27 05:40:45 +02:00
|
|
|
}
|
|
|
|
|
2018-12-10 06:51:35 +01:00
|
|
|
/// Starts processing incoming connections and return server controller.
|
2019-12-29 05:07:46 +01:00
|
|
|
pub fn run(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
|
2020-12-29 00:44:53 +01:00
|
|
|
let handles = (0..self.threads)
|
2020-01-21 22:35:22 +01:00
|
|
|
.map(|idx| {
|
2020-12-29 00:44:53 +01:00
|
|
|
let handle = self.start_worker(idx, self.accept.waker_owned());
|
|
|
|
self.handles.push((idx, handle.clone()));
|
2020-01-21 22:35:22 +01:00
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
handle
|
2020-01-21 22:35:22 +01:00
|
|
|
})
|
|
|
|
.collect();
|
2018-08-19 19:47:04 +02:00
|
|
|
|
|
|
|
// start accept thread
|
|
|
|
for sock in &self.sockets {
|
2019-12-29 05:07:46 +01:00
|
|
|
info!("Starting \"{}\" service on {}", sock.1, sock.2);
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
2019-12-29 05:07:46 +01:00
|
|
|
self.accept.start(
|
2020-08-17 16:37:57 +02:00
|
|
|
mem::take(&mut self.sockets)
|
2019-12-29 05:07:46 +01:00
|
|
|
.into_iter()
|
|
|
|
.map(|t| (t.0, t.2))
|
|
|
|
.collect(),
|
2020-12-29 00:44:53 +01:00
|
|
|
handles,
|
2019-12-29 05:07:46 +01:00
|
|
|
);
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2018-12-11 06:06:54 +01:00
|
|
|
// handle signals
|
|
|
|
if !self.no_signals {
|
2020-12-13 20:26:57 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
fn start_worker(&self, idx: usize, waker: WakerQueue) -> WorkerHandle {
|
|
|
|
let avail = WorkerAvailability::new(waker);
|
|
|
|
let services = self.services.iter().map(|v| v.clone_factory()).collect();
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2019-12-05 11:40:24 +01:00
|
|
|
Worker::start(idx, services, avail, self.shutdown_timeout)
|
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) => {
|
2020-12-29 00:44:53 +01:00
|
|
|
self.accept.wake(WakerInterest::Pause);
|
2018-12-11 06:06:54 +01:00
|
|
|
let _ = tx.send(());
|
|
|
|
}
|
|
|
|
ServerCommand::Resume(tx) => {
|
2020-12-29 00:44:53 +01:00
|
|
|
self.accept.wake(WakerInterest::Resume);
|
2018-12-11 06:06:54 +01:00
|
|
|
let _ = tx.send(());
|
|
|
|
}
|
2019-11-26 12:03:52 +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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 11:33:45 +01:00
|
|
|
ServerCommand::Notify(tx) => {
|
|
|
|
self.notify.push(tx);
|
|
|
|
}
|
2018-12-11 06:06:54 +01:00
|
|
|
ServerCommand::Stop {
|
|
|
|
graceful,
|
|
|
|
completion,
|
|
|
|
} => {
|
|
|
|
let exit = self.exit;
|
|
|
|
|
|
|
|
// stop accept thread
|
2020-12-29 00:44:53 +01:00
|
|
|
self.accept.wake(WakerInterest::Stop);
|
2020-08-17 16:37:57 +02:00
|
|
|
let notify = std::mem::take(&mut self.notify);
|
2018-12-11 06:06:54 +01:00
|
|
|
|
|
|
|
// stop workers
|
2020-12-29 00:44:53 +01:00
|
|
|
if !self.handles.is_empty() && graceful {
|
|
|
|
let iter = self
|
|
|
|
.handles
|
|
|
|
.iter()
|
|
|
|
.map(move |worker| worker.1.stop(graceful))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let fut = join_all(iter);
|
|
|
|
|
|
|
|
spawn(async move {
|
|
|
|
let _ = fut.await;
|
|
|
|
if let Some(tx) = completion {
|
|
|
|
let _ = tx.send(());
|
|
|
|
}
|
|
|
|
for tx in notify {
|
|
|
|
let _ = tx.send(());
|
|
|
|
}
|
|
|
|
if exit {
|
|
|
|
spawn(async {
|
|
|
|
sleep_until(Instant::now() + Duration::from_millis(300)).await;
|
|
|
|
System::current().stop();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
2018-12-11 06:06:54 +01:00
|
|
|
} else {
|
|
|
|
// we need to stop system if server was spawned
|
|
|
|
if self.exit {
|
2020-12-29 00:44:53 +01:00
|
|
|
spawn(async {
|
|
|
|
sleep_until(Instant::now() + Duration::from_millis(300)).await;
|
|
|
|
System::current().stop();
|
|
|
|
});
|
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-26 11:33:45 +01:00
|
|
|
for tx in notify {
|
|
|
|
let _ = tx.send(());
|
|
|
|
}
|
2018-12-11 06:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
ServerCommand::WorkerFaulted(idx) => {
|
2018-12-11 06:06:54 +01:00
|
|
|
let mut found = false;
|
2020-12-29 00:44:53 +01:00
|
|
|
for i in 0..self.handles.len() {
|
|
|
|
if self.handles[i].0 == idx {
|
|
|
|
self.handles.swap_remove(i);
|
2018-12-11 06:06:54 +01:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
|
|
|
error!("Worker has died {:?}, restarting", idx);
|
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
let mut new_idx = self.handles.len();
|
2018-12-11 06:06:54 +01:00
|
|
|
'found: loop {
|
2020-12-29 00:44:53 +01:00
|
|
|
for i in 0..self.handles.len() {
|
|
|
|
if self.handles[i].0 == new_idx {
|
2018-12-11 06:06:54 +01:00
|
|
|
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
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
let handle = self.start_worker(new_idx, self.accept.waker_owned());
|
|
|
|
self.handles.push((new_idx, handle.clone()));
|
|
|
|
self.accept.wake(WakerInterest::Worker(handle));
|
2018-12-11 06:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2020-12-29 00:44:53 +01:00
|
|
|
match Pin::new(&mut self.cmd).poll_recv(cx) {
|
|
|
|
Poll::Ready(Some(it)) => self.as_mut().get_mut().handle_cmd(it),
|
|
|
|
_ => return Poll::Pending,
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
pub(super) fn bind_addr<S: ToSocketAddrs>(
|
2019-03-11 20:01:55 +01:00
|
|
|
addr: S,
|
2020-12-29 00:44:53 +01:00
|
|
|
backlog: u32,
|
|
|
|
) -> io::Result<Vec<MioTcpListener>> {
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
fn create_tcp_listener(addr: StdSocketAddr, backlog: u32) -> io::Result<MioTcpListener> {
|
|
|
|
let socket = match addr {
|
|
|
|
StdSocketAddr::V4(_) => MioTcpSocket::new_v4()?,
|
|
|
|
StdSocketAddr::V6(_) => MioTcpSocket::new_v6()?,
|
2018-08-19 19:47:04 +02:00
|
|
|
};
|
2020-12-29 00:44:53 +01:00
|
|
|
|
|
|
|
socket.set_reuseaddr(true)?;
|
|
|
|
socket.bind(addr)?;
|
|
|
|
socket.listen(backlog)
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|