2019-11-14 13:38:24 +01:00
|
|
|
use std::pin::Pin;
|
2018-09-08 18:36:38 +02:00
|
|
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
2018-09-07 22:06:51 +02:00
|
|
|
use std::sync::Arc;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::task::{Context, Poll};
|
2019-12-04 10:12:02 +01:00
|
|
|
use std::time;
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2019-12-05 11:40:24 +01:00
|
|
|
use actix_rt::time::{delay_until, Delay, Instant};
|
2018-12-10 05:30:04 +01:00
|
|
|
use actix_rt::{spawn, Arbiter};
|
2019-12-02 17:30:09 +01:00
|
|
|
use actix_utils::counter::Counter;
|
2020-03-11 20:27:26 +01:00
|
|
|
use futures_channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
|
|
|
|
use futures_channel::oneshot;
|
|
|
|
use futures_util::future::{join_all, LocalBoxFuture, MapOk};
|
2020-05-19 03:20:32 +02:00
|
|
|
use futures_util::{future::Future, stream::Stream, FutureExt, TryFutureExt};
|
2018-12-06 23:04:42 +01:00
|
|
|
use log::{error, info, trace};
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2018-12-10 07:14:29 +01:00
|
|
|
use crate::accept::AcceptNotify;
|
2020-12-13 01:46:32 +01:00
|
|
|
use crate::service::{BoxedServerService, InternalServiceFactory};
|
2019-07-18 13:05:40 +02:00
|
|
|
use crate::socket::{SocketAddr, StdStream};
|
2018-12-10 07:14:29 +01:00
|
|
|
use crate::Token;
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2018-11-01 23:33:35 +01:00
|
|
|
pub(crate) struct WorkerCommand(Conn);
|
|
|
|
|
|
|
|
/// Stop worker message. Returns `true` on successful shutdown
|
|
|
|
/// and `false` if some connections still alive.
|
|
|
|
pub(crate) struct StopCommand {
|
|
|
|
graceful: bool,
|
|
|
|
result: oneshot::Sender<bool>,
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
|
|
|
|
2018-12-10 05:30:04 +01:00
|
|
|
#[derive(Debug)]
|
2018-08-19 19:47:04 +02:00
|
|
|
pub(crate) struct Conn {
|
2019-07-18 13:05:40 +02:00
|
|
|
pub io: StdStream,
|
2018-08-19 19:47:04 +02:00
|
|
|
pub token: Token,
|
2019-07-18 13:05:40 +02:00
|
|
|
pub peer: Option<SocketAddr>,
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
2018-12-10 07:14:29 +01:00
|
|
|
static MAX_CONNS: AtomicUsize = AtomicUsize::new(25600);
|
2018-09-08 18:36:38 +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.
|
|
|
|
///
|
|
|
|
/// By default max connections is set to a 25k per worker.
|
|
|
|
pub fn max_concurrent_connections(num: usize) {
|
|
|
|
MAX_CONNS.store(num, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn num_connections() -> usize {
|
|
|
|
MAX_CONNS_COUNTER.with(|conns| conns.total())
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_local! {
|
2018-09-14 22:07:38 +02:00
|
|
|
static MAX_CONNS_COUNTER: Counter =
|
|
|
|
Counter::new(MAX_CONNS.load(Ordering::Relaxed));
|
2018-09-08 18:36:38 +02:00
|
|
|
}
|
|
|
|
|
2018-08-19 19:47:04 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub(crate) struct WorkerClient {
|
|
|
|
pub idx: usize,
|
2018-11-01 23:33:35 +01:00
|
|
|
tx1: UnboundedSender<WorkerCommand>,
|
|
|
|
tx2: UnboundedSender<StopCommand>,
|
2018-09-07 22:06:51 +02:00
|
|
|
avail: WorkerAvailability,
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WorkerClient {
|
2018-09-14 08:46:01 +02:00
|
|
|
pub fn new(
|
2018-10-30 04:29:47 +01:00
|
|
|
idx: usize,
|
2018-11-01 23:33:35 +01:00
|
|
|
tx1: UnboundedSender<WorkerCommand>,
|
|
|
|
tx2: UnboundedSender<StopCommand>,
|
2018-10-30 04:29:47 +01:00
|
|
|
avail: WorkerAvailability,
|
2018-09-14 08:46:01 +02:00
|
|
|
) -> Self {
|
2018-11-01 23:33:35 +01:00
|
|
|
WorkerClient {
|
|
|
|
idx,
|
|
|
|
tx1,
|
|
|
|
tx2,
|
|
|
|
avail,
|
|
|
|
}
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
2018-09-14 08:46:01 +02:00
|
|
|
pub fn send(&self, msg: Conn) -> Result<(), Conn> {
|
2018-11-01 23:33:35 +01:00
|
|
|
self.tx1
|
|
|
|
.unbounded_send(WorkerCommand(msg))
|
|
|
|
.map_err(|msg| msg.into_inner().0)
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn available(&self) -> bool {
|
2018-09-07 22:06:51 +02:00
|
|
|
self.avail.available()
|
|
|
|
}
|
2018-09-14 08:46:01 +02:00
|
|
|
|
2018-09-27 05:40:45 +02:00
|
|
|
pub fn stop(&self, graceful: bool) -> oneshot::Receiver<bool> {
|
2018-11-01 23:33:35 +01:00
|
|
|
let (result, rx) = oneshot::channel();
|
|
|
|
let _ = self.tx2.unbounded_send(StopCommand { graceful, result });
|
2018-09-14 08:46:01 +02:00
|
|
|
rx
|
|
|
|
}
|
2018-09-07 22:06:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub(crate) struct WorkerAvailability {
|
|
|
|
notify: AcceptNotify,
|
|
|
|
available: Arc<AtomicBool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WorkerAvailability {
|
|
|
|
pub fn new(notify: AcceptNotify) -> Self {
|
|
|
|
WorkerAvailability {
|
|
|
|
notify,
|
|
|
|
available: Arc::new(AtomicBool::new(false)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn available(&self) -> bool {
|
|
|
|
self.available.load(Ordering::Acquire)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set(&self, val: bool) {
|
|
|
|
let old = self.available.swap(val, Ordering::Release);
|
|
|
|
if !old && val {
|
|
|
|
self.notify.notify()
|
|
|
|
}
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:19:48 +02:00
|
|
|
/// Service worker
|
2018-08-19 19:47:04 +02:00
|
|
|
///
|
2018-09-18 05:19:48 +02:00
|
|
|
/// Worker accepts Socket objects via unbounded channel and starts stream
|
2018-08-19 19:47:04 +02:00
|
|
|
/// processing.
|
|
|
|
pub(crate) struct Worker {
|
2018-09-14 08:46:01 +02:00
|
|
|
rx: UnboundedReceiver<WorkerCommand>,
|
2018-11-01 23:33:35 +01:00
|
|
|
rx2: UnboundedReceiver<StopCommand>,
|
2019-12-04 10:12:02 +01:00
|
|
|
services: Vec<WorkerService>,
|
2018-09-07 22:06:51 +02:00
|
|
|
availability: WorkerAvailability,
|
2018-09-14 22:07:38 +02:00
|
|
|
conns: Counter,
|
2019-07-18 13:05:40 +02:00
|
|
|
factories: Vec<Box<dyn InternalServiceFactory>>,
|
2018-09-14 08:46:01 +02:00
|
|
|
state: WorkerState,
|
2018-09-27 05:40:45 +02:00
|
|
|
shutdown_timeout: time::Duration,
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
2019-12-04 10:12:02 +01:00
|
|
|
struct WorkerService {
|
|
|
|
factory: usize,
|
|
|
|
status: WorkerServiceStatus,
|
|
|
|
service: BoxedServerService,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WorkerService {
|
|
|
|
fn created(&mut self, service: BoxedServerService) {
|
|
|
|
self.service = service;
|
|
|
|
self.status = WorkerServiceStatus::Unavailable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
enum WorkerServiceStatus {
|
|
|
|
Available,
|
|
|
|
Unavailable,
|
|
|
|
Failed,
|
|
|
|
Restarting,
|
|
|
|
Stopping,
|
|
|
|
Stopped,
|
|
|
|
}
|
|
|
|
|
2018-08-19 19:47:04 +02:00
|
|
|
impl Worker {
|
2018-09-14 08:46:01 +02:00
|
|
|
pub(crate) fn start(
|
2019-12-05 11:40:24 +01:00
|
|
|
idx: usize,
|
2019-07-18 13:05:40 +02:00
|
|
|
factories: Vec<Box<dyn InternalServiceFactory>>,
|
2018-10-30 04:29:47 +01:00
|
|
|
availability: WorkerAvailability,
|
|
|
|
shutdown_timeout: time::Duration,
|
2019-12-05 11:40:24 +01:00
|
|
|
) -> WorkerClient {
|
|
|
|
let (tx1, rx) = unbounded();
|
|
|
|
let (tx2, rx2) = unbounded();
|
|
|
|
let avail = availability.clone();
|
|
|
|
|
|
|
|
Arbiter::new().send(
|
|
|
|
async move {
|
|
|
|
availability.set(false);
|
|
|
|
let mut wrk = MAX_CONNS_COUNTER.with(move |conns| Worker {
|
|
|
|
rx,
|
|
|
|
rx2,
|
|
|
|
availability,
|
|
|
|
factories,
|
|
|
|
shutdown_timeout,
|
|
|
|
services: Vec::new(),
|
|
|
|
conns: conns.clone(),
|
|
|
|
state: WorkerState::Unavailable(Vec::new()),
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut fut: Vec<MapOk<LocalBoxFuture<'static, _>, _>> = Vec::new();
|
|
|
|
for (idx, factory) in wrk.factories.iter().enumerate() {
|
|
|
|
fut.push(factory.create().map_ok(move |r| {
|
|
|
|
r.into_iter()
|
|
|
|
.map(|(t, s): (Token, _)| (idx, t, s))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
}));
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
|
2019-12-05 11:40:24 +01:00
|
|
|
spawn(async move {
|
|
|
|
let res = join_all(fut).await;
|
|
|
|
let res: Result<Vec<_>, _> = res.into_iter().collect();
|
|
|
|
match res {
|
|
|
|
Ok(services) => {
|
|
|
|
for item in services {
|
|
|
|
for (factory, token, service) in item {
|
|
|
|
assert_eq!(token.0, wrk.services.len());
|
|
|
|
wrk.services.push(WorkerService {
|
|
|
|
factory,
|
|
|
|
service,
|
|
|
|
status: WorkerServiceStatus::Unavailable,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("Can not start worker: {:?}", e);
|
|
|
|
Arbiter::current().stop();
|
2018-11-03 17:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-05 11:40:24 +01:00
|
|
|
wrk.await
|
|
|
|
});
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
2019-12-25 12:10:13 +01:00
|
|
|
.boxed(),
|
2019-12-05 11:40:24 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
WorkerClient::new(idx, tx1, tx2, avail)
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
2018-09-07 22:06:51 +02:00
|
|
|
fn shutdown(&mut self, force: bool) {
|
|
|
|
if force {
|
2019-12-04 10:12:02 +01:00
|
|
|
self.services.iter_mut().for_each(|srv| {
|
|
|
|
if srv.status == WorkerServiceStatus::Available {
|
|
|
|
srv.status = WorkerServiceStatus::Stopped;
|
2018-11-03 17:09:14 +01:00
|
|
|
}
|
2018-09-07 22:06:51 +02:00
|
|
|
});
|
|
|
|
} else {
|
2019-12-04 10:12:02 +01:00
|
|
|
self.services.iter_mut().for_each(move |srv| {
|
|
|
|
if srv.status == WorkerServiceStatus::Available {
|
|
|
|
srv.status = WorkerServiceStatus::Stopping;
|
2018-11-03 17:09:14 +01:00
|
|
|
}
|
2018-09-07 22:06:51 +02:00
|
|
|
});
|
|
|
|
}
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
2019-12-04 10:12:02 +01:00
|
|
|
fn check_readiness(&mut self, cx: &mut Context<'_>) -> Result<bool, (Token, usize)> {
|
2019-11-14 13:38:24 +01:00
|
|
|
let mut ready = self.conns.available(cx);
|
2018-09-14 08:46:01 +02:00
|
|
|
let mut failed = None;
|
2019-12-04 10:12:02 +01:00
|
|
|
for (idx, srv) in &mut self.services.iter_mut().enumerate() {
|
|
|
|
if srv.status == WorkerServiceStatus::Available
|
|
|
|
|| srv.status == WorkerServiceStatus::Unavailable
|
|
|
|
{
|
|
|
|
match srv.service.poll_ready(cx) {
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(Ok(_)) => {
|
2019-12-04 10:12:02 +01:00
|
|
|
if srv.status == WorkerServiceStatus::Unavailable {
|
2018-11-03 17:09:14 +01:00
|
|
|
trace!(
|
|
|
|
"Service {:?} is available",
|
2019-12-04 10:12:02 +01:00
|
|
|
self.factories[srv.factory].name(Token(idx))
|
2018-11-03 17:09:14 +01:00
|
|
|
);
|
2019-12-04 10:12:02 +01:00
|
|
|
srv.status = WorkerServiceStatus::Available;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Poll::Pending => {
|
|
|
|
ready = false;
|
|
|
|
|
|
|
|
if srv.status == WorkerServiceStatus::Available {
|
|
|
|
trace!(
|
|
|
|
"Service {:?} is unavailable",
|
|
|
|
self.factories[srv.factory].name(Token(idx))
|
|
|
|
);
|
|
|
|
srv.status = WorkerServiceStatus::Unavailable;
|
2018-11-03 17:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(Err(_)) => {
|
2018-11-03 17:09:14 +01:00
|
|
|
error!(
|
|
|
|
"Service {:?} readiness check returned error, restarting",
|
2019-12-04 10:12:02 +01:00
|
|
|
self.factories[srv.factory].name(Token(idx))
|
2018-11-03 17:09:14 +01:00
|
|
|
);
|
2019-12-04 10:12:02 +01:00
|
|
|
failed = Some((Token(idx), srv.factory));
|
|
|
|
srv.status = WorkerServiceStatus::Failed;
|
2018-09-18 05:19:48 +02:00
|
|
|
}
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
2018-08-21 07:21:23 +02:00
|
|
|
}
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
|
|
|
if let Some(idx) = failed {
|
|
|
|
Err(idx)
|
2018-08-21 07:21:23 +02:00
|
|
|
} else {
|
2018-09-14 08:46:01 +02:00
|
|
|
Ok(ready)
|
2018-08-21 07:21:23 +02:00
|
|
|
}
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-07 22:06:51 +02:00
|
|
|
|
2018-09-14 08:46:01 +02:00
|
|
|
enum WorkerState {
|
|
|
|
Available,
|
|
|
|
Unavailable(Vec<Conn>),
|
2018-11-03 17:09:14 +01:00
|
|
|
Restarting(
|
|
|
|
usize,
|
|
|
|
Token,
|
2020-09-13 11:12:07 +02:00
|
|
|
#[allow(clippy::type_complexity)]
|
2019-11-14 13:38:24 +01:00
|
|
|
Pin<Box<dyn Future<Output = Result<Vec<(Token, BoxedServerService)>, ()>>>>,
|
2018-11-03 17:09:14 +01:00
|
|
|
),
|
2019-12-04 10:12:02 +01:00
|
|
|
Shutdown(
|
|
|
|
Pin<Box<Delay>>,
|
|
|
|
Pin<Box<Delay>>,
|
|
|
|
Option<oneshot::Sender<bool>>,
|
|
|
|
),
|
2018-09-12 17:25:14 +02:00
|
|
|
}
|
2018-09-07 22:06:51 +02:00
|
|
|
|
2018-09-14 08:46:01 +02:00
|
|
|
impl Future for Worker {
|
2019-11-14 13:38:24 +01:00
|
|
|
type Output = ();
|
2018-09-14 08:46:01 +02:00
|
|
|
|
2020-01-28 12:27:33 +01:00
|
|
|
// FIXME: remove this attribute
|
|
|
|
#[allow(clippy::never_loop)]
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2018-11-01 23:33:35 +01:00
|
|
|
// `StopWorker` message handler
|
2019-11-14 13:38:24 +01:00
|
|
|
if let Poll::Ready(Some(StopCommand { graceful, result })) =
|
|
|
|
Pin::new(&mut self.rx2).poll_next(cx)
|
|
|
|
{
|
2018-12-06 23:04:42 +01:00
|
|
|
self.availability.set(false);
|
|
|
|
let num = num_connections();
|
|
|
|
if num == 0 {
|
|
|
|
info!("Shutting down worker, 0 connections");
|
|
|
|
let _ = result.send(true);
|
2019-11-14 13:38:24 +01:00
|
|
|
return Poll::Ready(());
|
2018-12-06 23:04:42 +01:00
|
|
|
} else if graceful {
|
|
|
|
self.shutdown(false);
|
2018-11-01 23:33:35 +01:00
|
|
|
let num = num_connections();
|
2018-12-06 23:04:42 +01:00
|
|
|
if num != 0 {
|
|
|
|
info!("Graceful worker shutdown, {} connections", num);
|
|
|
|
self.state = WorkerState::Shutdown(
|
2019-12-05 11:40:24 +01:00
|
|
|
Box::pin(delay_until(Instant::now() + time::Duration::from_secs(1))),
|
|
|
|
Box::pin(delay_until(Instant::now() + self.shutdown_timeout)),
|
2019-12-04 10:12:02 +01:00
|
|
|
Some(result),
|
2018-12-06 23:04:42 +01:00
|
|
|
);
|
2018-11-01 23:33:35 +01:00
|
|
|
} else {
|
2018-12-06 23:04:42 +01:00
|
|
|
let _ = result.send(true);
|
2019-11-14 13:38:24 +01:00
|
|
|
return Poll::Ready(());
|
2018-11-01 23:33:35 +01:00
|
|
|
}
|
2018-12-06 23:04:42 +01:00
|
|
|
} else {
|
|
|
|
info!("Force shutdown worker, {} connections", num);
|
|
|
|
self.shutdown(true);
|
|
|
|
let _ = result.send(false);
|
2019-11-14 13:38:24 +01:00
|
|
|
return Poll::Ready(());
|
2018-11-01 23:33:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 10:12:02 +01:00
|
|
|
match self.state {
|
|
|
|
WorkerState::Unavailable(ref mut conns) => {
|
|
|
|
let conn = conns.pop();
|
|
|
|
match self.check_readiness(cx) {
|
2018-09-14 08:46:01 +02:00
|
|
|
Ok(true) => {
|
|
|
|
// process requests from wait queue
|
2019-12-04 10:12:02 +01:00
|
|
|
if let Some(conn) = conn {
|
|
|
|
let guard = self.conns.get();
|
|
|
|
let _ = self.services[conn.token.0]
|
|
|
|
.service
|
2020-12-13 01:46:32 +01:00
|
|
|
.call((Some(guard), conn.io));
|
2019-12-04 10:12:02 +01:00
|
|
|
} else {
|
|
|
|
self.state = WorkerState::Available;
|
|
|
|
self.availability.set(true);
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
2019-12-02 17:30:09 +01:00
|
|
|
self.poll(cx)
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
|
|
|
Ok(false) => {
|
2019-12-04 10:12:02 +01:00
|
|
|
// push connection back to queue
|
|
|
|
if let Some(conn) = conn {
|
2020-01-28 12:27:33 +01:00
|
|
|
if let WorkerState::Unavailable(ref mut conns) = self.state {
|
|
|
|
conns.push(conn);
|
2019-12-04 10:12:02 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-02 17:30:09 +01:00
|
|
|
Poll::Pending
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
2018-11-03 17:09:14 +01:00
|
|
|
Err((token, idx)) => {
|
2018-09-18 05:19:48 +02:00
|
|
|
trace!(
|
|
|
|
"Service {:?} failed, restarting",
|
2018-11-03 17:09:14 +01:00
|
|
|
self.factories[idx].name(token)
|
2018-09-18 05:19:48 +02:00
|
|
|
);
|
2019-12-04 10:12:02 +01:00
|
|
|
self.services[token.0].status = WorkerServiceStatus::Restarting;
|
2018-11-03 17:09:14 +01:00
|
|
|
self.state =
|
|
|
|
WorkerState::Restarting(idx, token, self.factories[idx].create());
|
2019-12-02 17:30:09 +01:00
|
|
|
self.poll(cx)
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
2018-09-12 17:25:14 +02:00
|
|
|
}
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
2019-12-04 10:12:02 +01:00
|
|
|
WorkerState::Restarting(idx, token, ref mut fut) => {
|
|
|
|
match Pin::new(fut).poll(cx) {
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(Ok(item)) => {
|
2018-11-03 17:09:14 +01:00
|
|
|
for (token, service) in item {
|
|
|
|
trace!(
|
|
|
|
"Service {:?} has been restarted",
|
|
|
|
self.factories[idx].name(token)
|
|
|
|
);
|
2019-12-04 10:12:02 +01:00
|
|
|
self.services[token.0].created(service);
|
2018-11-03 17:09:14 +01:00
|
|
|
self.state = WorkerState::Unavailable(Vec::new());
|
2019-12-04 10:12:02 +01:00
|
|
|
return self.poll(cx);
|
2018-11-03 17:09:14 +01:00
|
|
|
}
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(Err(_)) => {
|
2018-11-03 17:09:14 +01:00
|
|
|
panic!(
|
|
|
|
"Can not restart {:?} service",
|
|
|
|
self.factories[idx].name(token)
|
|
|
|
);
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Pending => {
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
2018-09-12 17:25:14 +02:00
|
|
|
}
|
2019-12-02 17:30:09 +01:00
|
|
|
self.poll(cx)
|
2018-09-12 17:25:14 +02:00
|
|
|
}
|
2019-12-04 10:12:02 +01:00
|
|
|
WorkerState::Shutdown(ref mut t1, ref mut t2, ref mut tx) => {
|
2018-09-14 08:46:01 +02:00
|
|
|
let num = num_connections();
|
|
|
|
if num == 0 {
|
2019-12-04 10:12:02 +01:00
|
|
|
let _ = tx.take().unwrap().send(true);
|
2018-12-10 05:30:04 +01:00
|
|
|
Arbiter::current().stop();
|
2019-11-14 13:38:24 +01:00
|
|
|
return Poll::Ready(());
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
2018-09-12 17:25:14 +02:00
|
|
|
|
2018-09-14 08:46:01 +02:00
|
|
|
// check graceful timeout
|
2019-12-04 10:12:02 +01:00
|
|
|
match t2.as_mut().poll(cx) {
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Pending => (),
|
|
|
|
Poll::Ready(_) => {
|
2019-12-04 10:12:02 +01:00
|
|
|
let _ = tx.take().unwrap().send(false);
|
2018-09-14 08:46:01 +02:00
|
|
|
self.shutdown(true);
|
2018-12-10 05:30:04 +01:00
|
|
|
Arbiter::current().stop();
|
2019-11-14 13:38:24 +01:00
|
|
|
return Poll::Ready(());
|
2018-09-12 17:25:14 +02:00
|
|
|
}
|
2018-09-08 18:36:38 +02:00
|
|
|
}
|
2018-09-14 08:46:01 +02:00
|
|
|
|
|
|
|
// sleep for 1 second and then check again
|
2019-12-04 10:12:02 +01:00
|
|
|
match t1.as_mut().poll(cx) {
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Pending => (),
|
|
|
|
Poll::Ready(_) => {
|
2019-12-05 11:40:24 +01:00
|
|
|
*t1 = Box::pin(delay_until(
|
|
|
|
Instant::now() + time::Duration::from_secs(1),
|
2019-12-04 10:12:02 +01:00
|
|
|
));
|
|
|
|
let _ = t1.as_mut().poll(cx);
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-02 17:30:09 +01:00
|
|
|
Poll::Pending
|
2018-09-07 22:06:51 +02:00
|
|
|
}
|
2018-09-14 08:46:01 +02:00
|
|
|
WorkerState::Available => {
|
|
|
|
loop {
|
2019-11-14 13:38:24 +01:00
|
|
|
match Pin::new(&mut self.rx).poll_next(cx) {
|
2019-12-05 11:40:24 +01:00
|
|
|
// handle incoming io stream
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(Some(WorkerCommand(msg))) => {
|
2019-12-04 10:12:02 +01:00
|
|
|
match self.check_readiness(cx) {
|
2018-09-14 09:07:50 +02:00
|
|
|
Ok(true) => {
|
|
|
|
let guard = self.conns.get();
|
2018-11-03 18:22:04 +01:00
|
|
|
let _ = self.services[msg.token.0]
|
2019-12-04 10:12:02 +01:00
|
|
|
.service
|
2020-12-13 01:46:32 +01:00
|
|
|
.call((Some(guard), msg.io));
|
2018-09-14 22:07:38 +02:00
|
|
|
continue;
|
2018-09-14 09:07:50 +02:00
|
|
|
}
|
|
|
|
Ok(false) => {
|
2018-09-18 05:19:48 +02:00
|
|
|
trace!("Worker is unavailable");
|
2018-09-14 09:07:50 +02:00
|
|
|
self.availability.set(false);
|
|
|
|
self.state = WorkerState::Unavailable(vec![msg]);
|
|
|
|
}
|
2018-11-03 17:09:14 +01:00
|
|
|
Err((token, idx)) => {
|
2018-09-18 05:19:48 +02:00
|
|
|
trace!(
|
|
|
|
"Service {:?} failed, restarting",
|
2018-11-03 17:09:14 +01:00
|
|
|
self.factories[idx].name(token)
|
2018-09-18 05:19:48 +02:00
|
|
|
);
|
2018-09-14 09:07:50 +02:00
|
|
|
self.availability.set(false);
|
2019-12-04 10:12:02 +01:00
|
|
|
self.services[token.0].status =
|
|
|
|
WorkerServiceStatus::Restarting;
|
2018-09-14 22:07:38 +02:00
|
|
|
self.state = WorkerState::Restarting(
|
|
|
|
idx,
|
2018-11-03 17:09:14 +01:00
|
|
|
token,
|
2018-09-14 22:07:38 +02:00
|
|
|
self.factories[idx].create(),
|
|
|
|
);
|
2018-09-14 09:07:50 +02:00
|
|
|
}
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
return self.poll(cx);
|
2018-09-14 22:07:38 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Pending => {
|
2018-09-14 08:46:01 +02:00
|
|
|
self.state = WorkerState::Available;
|
2019-11-14 13:38:24 +01:00
|
|
|
return Poll::Pending;
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(None) => return Poll::Ready(()),
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 17:25:14 +02:00
|
|
|
}
|
2019-12-02 17:30:09 +01:00
|
|
|
}
|
2018-09-07 22:06:51 +02:00
|
|
|
}
|
|
|
|
}
|