2021-04-04 21:34:52 +02:00
|
|
|
use std::{
|
|
|
|
future::Future,
|
|
|
|
mem,
|
|
|
|
pin::Pin,
|
|
|
|
sync::{
|
2021-04-05 00:00:12 +02:00
|
|
|
atomic::{AtomicBool, Ordering},
|
2021-04-04 21:34:52 +02:00
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
task::{Context, Poll},
|
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
|
|
|
|
use actix_rt::{
|
|
|
|
spawn,
|
|
|
|
time::{sleep, Instant, Sleep},
|
|
|
|
Arbiter,
|
|
|
|
};
|
2019-12-02 17:30:09 +01:00
|
|
|
use actix_utils::counter::Counter;
|
2021-04-02 13:22:05 +02:00
|
|
|
use futures_core::{future::LocalBoxFuture, ready};
|
2018-12-06 23:04:42 +01:00
|
|
|
use log::{error, info, trace};
|
2021-04-04 21:34:52 +02:00
|
|
|
use tokio::sync::{
|
|
|
|
mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender},
|
|
|
|
oneshot,
|
|
|
|
};
|
2018-08-19 19:47:04 +02:00
|
|
|
|
2020-12-13 01:46:32 +01:00
|
|
|
use crate::service::{BoxedServerService, InternalServiceFactory};
|
2021-04-01 07:55:33 +02:00
|
|
|
use crate::socket::MioStream;
|
2020-12-29 00:44:53 +01:00
|
|
|
use crate::waker_queue::{WakerInterest, WakerQueue};
|
|
|
|
use crate::{join_all, 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,
|
2021-04-04 22:53:19 +02:00
|
|
|
tx: 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 {
|
2020-12-29 00:44:53 +01:00
|
|
|
pub io: MioStream,
|
2018-08-19 19:47:04 +02:00
|
|
|
pub token: Token,
|
|
|
|
}
|
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
// a handle to worker that can send message to worker and share the availability of worker to other
|
|
|
|
// thread.
|
2018-08-19 19:47:04 +02:00
|
|
|
#[derive(Clone)]
|
2020-12-29 00:44:53 +01:00
|
|
|
pub(crate) struct WorkerHandle {
|
2018-08-19 19:47:04 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
impl WorkerHandle {
|
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 {
|
2020-12-29 00:44:53 +01:00
|
|
|
WorkerHandle {
|
2018-11-01 23:33:35 +01:00
|
|
|
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> {
|
2020-12-29 00:44:53 +01:00
|
|
|
self.tx1.send(WorkerCommand(msg)).map_err(|msg| msg.0 .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> {
|
2021-04-04 22:53:19 +02:00
|
|
|
let (tx, rx) = oneshot::channel();
|
|
|
|
let _ = self.tx2.send(StopCommand { graceful, tx });
|
2018-09-14 08:46:01 +02:00
|
|
|
rx
|
|
|
|
}
|
2018-09-07 22:06:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub(crate) struct WorkerAvailability {
|
2020-12-29 00:44:53 +01:00
|
|
|
waker: WakerQueue,
|
2018-09-07 22:06:51 +02:00
|
|
|
available: Arc<AtomicBool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WorkerAvailability {
|
2020-12-29 00:44:53 +01:00
|
|
|
pub fn new(waker: WakerQueue) -> Self {
|
2018-09-07 22:06:51 +02:00
|
|
|
WorkerAvailability {
|
2020-12-29 00:44:53 +01:00
|
|
|
waker,
|
2018-09-07 22:06:51 +02:00
|
|
|
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);
|
2020-12-29 00:44:53 +01:00
|
|
|
// notify the accept on switched to available.
|
2018-09-07 22:06:51 +02:00
|
|
|
if !old && val {
|
2020-12-29 00:44:53 +01:00
|
|
|
self.waker.wake(WakerInterest::WorkerAvailable);
|
2018-09-07 22:06:51 +02:00
|
|
|
}
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 05:08:14 +01:00
|
|
|
/// Service worker.
|
2018-08-19 19:47:04 +02:00
|
|
|
///
|
2021-01-29 05:08:14 +01:00
|
|
|
/// Worker accepts Socket objects via unbounded channel and starts stream processing.
|
|
|
|
pub(crate) struct ServerWorker {
|
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,
|
2021-04-04 21:34:52 +02:00
|
|
|
shutdown_timeout: 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,
|
|
|
|
}
|
|
|
|
|
2021-02-04 16:01:51 +01:00
|
|
|
/// Config for worker behavior passed down from server builder.
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub(crate) struct ServerWorkerConfig {
|
|
|
|
shutdown_timeout: Duration,
|
|
|
|
max_blocking_threads: usize,
|
2021-04-05 00:00:12 +02:00
|
|
|
max_concurrent_connections: usize,
|
2021-02-04 16:01:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ServerWorkerConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
// 512 is the default max blocking thread count of tokio runtime.
|
|
|
|
let max_blocking_threads = std::cmp::max(512 / num_cpus::get(), 1);
|
|
|
|
Self {
|
|
|
|
shutdown_timeout: Duration::from_secs(30),
|
|
|
|
max_blocking_threads,
|
2021-04-05 00:00:12 +02:00
|
|
|
max_concurrent_connections: 25600,
|
2021-02-04 16:01:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ServerWorkerConfig {
|
|
|
|
pub(crate) fn max_blocking_threads(&mut self, num: usize) {
|
|
|
|
self.max_blocking_threads = num;
|
|
|
|
}
|
|
|
|
|
2021-04-05 00:00:12 +02:00
|
|
|
pub(crate) fn max_concurrent_connections(&mut self, num: usize) {
|
|
|
|
self.max_concurrent_connections = num;
|
|
|
|
}
|
|
|
|
|
2021-02-04 16:01:51 +01:00
|
|
|
pub(crate) fn shutdown_timeout(&mut self, dur: Duration) {
|
|
|
|
self.shutdown_timeout = dur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 05:08:14 +01:00
|
|
|
impl ServerWorker {
|
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,
|
2021-02-04 16:01:51 +01:00
|
|
|
config: ServerWorkerConfig,
|
2020-12-29 00:44:53 +01:00
|
|
|
) -> WorkerHandle {
|
|
|
|
let (tx1, rx) = unbounded_channel();
|
|
|
|
let (tx2, rx2) = unbounded_channel();
|
2019-12-05 11:40:24 +01:00
|
|
|
let avail = availability.clone();
|
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
// every worker runs in it's own arbiter.
|
2021-02-04 16:01:51 +01:00
|
|
|
// use a custom tokio runtime builder to change the settings of runtime.
|
|
|
|
Arbiter::with_tokio_rt(move || {
|
|
|
|
tokio::runtime::Builder::new_current_thread()
|
|
|
|
.enable_all()
|
|
|
|
.max_blocking_threads(config.max_blocking_threads)
|
|
|
|
.build()
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
.spawn(async move {
|
2020-12-29 00:44:53 +01:00
|
|
|
availability.set(false);
|
2021-04-05 00:00:12 +02:00
|
|
|
let mut wrk = ServerWorker {
|
2020-12-29 00:44:53 +01:00
|
|
|
rx,
|
|
|
|
rx2,
|
2021-04-04 22:53:19 +02:00
|
|
|
services: Vec::new(),
|
2020-12-29 00:44:53 +01:00
|
|
|
availability,
|
2021-04-05 00:00:12 +02:00
|
|
|
conns: Counter::new(config.max_concurrent_connections),
|
2020-12-29 00:44:53 +01:00
|
|
|
factories,
|
2021-04-04 21:34:52 +02:00
|
|
|
state: Default::default(),
|
|
|
|
shutdown_timeout: config.shutdown_timeout,
|
2021-04-05 00:00:12 +02:00
|
|
|
};
|
2019-11-14 13:38:24 +01:00
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
let fut = wrk
|
|
|
|
.factories
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(idx, factory)| {
|
|
|
|
let fut = factory.create();
|
|
|
|
async move {
|
|
|
|
fut.await.map(|r| {
|
|
|
|
r.into_iter().map(|(t, s)| (idx, t, s)).collect::<Vec<_>>()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2021-02-04 16:01:51 +01:00
|
|
|
// a second spawn to make sure worker future runs as non boxed future.
|
|
|
|
// As Arbiter::spawn would box the future before send it to arbiter.
|
2020-12-29 00:44:53 +01:00
|
|
|
spawn(async move {
|
|
|
|
let res: Result<Vec<_>, _> = join_all(fut).await.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,
|
|
|
|
});
|
2019-12-05 11:40:24 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-03 17:09:14 +01:00
|
|
|
}
|
2020-12-29 00:44:53 +01:00
|
|
|
Err(e) => {
|
|
|
|
error!("Can not start worker: {:?}", e);
|
2021-01-31 05:41:28 +01:00
|
|
|
Arbiter::current().stop();
|
2020-12-29 00:44:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
wrk.await
|
|
|
|
});
|
2021-02-04 16:01:51 +01:00
|
|
|
});
|
2019-12-05 11:40:24 +01:00
|
|
|
|
2020-12-29 00:44:53 +01:00
|
|
|
WorkerHandle::new(idx, tx1, tx2, avail)
|
2018-08-19 19:47:04 +02:00
|
|
|
}
|
|
|
|
|
2021-04-04 22:53:19 +02:00
|
|
|
fn restart_service(&mut self, token: Token, factory_id: usize) {
|
|
|
|
let factory = &self.factories[factory_id];
|
2021-04-04 14:22:34 +02:00
|
|
|
trace!("Service {:?} failed, restarting", factory.name(token));
|
|
|
|
self.services[token.0].status = WorkerServiceStatus::Restarting;
|
2021-04-04 22:53:19 +02:00
|
|
|
self.state = WorkerState::Restarting(Restart {
|
|
|
|
factory_id,
|
|
|
|
token,
|
|
|
|
fut: factory.create(),
|
|
|
|
});
|
2021-04-04 14:22:34 +02:00
|
|
|
}
|
|
|
|
|
2018-09-07 22:06:51 +02:00
|
|
|
fn shutdown(&mut self, force: bool) {
|
2021-04-02 13:22:05 +02:00
|
|
|
self.services
|
|
|
|
.iter_mut()
|
|
|
|
.filter(|srv| srv.status == WorkerServiceStatus::Available)
|
|
|
|
.for_each(|srv| {
|
|
|
|
srv.status = if force {
|
|
|
|
WorkerServiceStatus::Stopped
|
|
|
|
} else {
|
|
|
|
WorkerServiceStatus::Stopping
|
|
|
|
};
|
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;
|
2020-12-29 00:44:53 +01:00
|
|
|
for (idx, srv) in self.services.iter_mut().enumerate() {
|
2019-12-04 10:12:02 +01:00
|
|
|
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,
|
2020-12-29 00:44:53 +01:00
|
|
|
Unavailable,
|
2021-04-04 22:53:19 +02:00
|
|
|
Restarting(Restart),
|
|
|
|
Shutdown(Shutdown),
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Restart {
|
|
|
|
factory_id: usize,
|
|
|
|
token: Token,
|
|
|
|
fut: LocalBoxFuture<'static, Result<Vec<(Token, BoxedServerService)>, ()>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown keep states necessary for server shutdown:
|
|
|
|
// Sleep for interval check the shutdown progress.
|
|
|
|
// Instant for the start time of shutdown.
|
|
|
|
// Sender for send back the shutdown outcome(force/grace) to StopCommand caller.
|
|
|
|
struct Shutdown {
|
|
|
|
timer: Pin<Box<Sleep>>,
|
|
|
|
start_from: Instant,
|
|
|
|
tx: oneshot::Sender<bool>,
|
2021-04-04 21:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for WorkerState {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Unavailable
|
|
|
|
}
|
2018-09-12 17:25:14 +02:00
|
|
|
}
|
2018-09-07 22:06:51 +02:00
|
|
|
|
2021-01-29 05:08:14 +01:00
|
|
|
impl Future for ServerWorker {
|
2019-11-14 13:38:24 +01:00
|
|
|
type Output = ();
|
2018-09-14 08:46:01 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2021-04-04 21:34:52 +02:00
|
|
|
let this = self.as_mut().get_mut();
|
|
|
|
|
2018-11-01 23:33:35 +01:00
|
|
|
// `StopWorker` message handler
|
2021-04-04 22:53:19 +02:00
|
|
|
if let Poll::Ready(Some(StopCommand { graceful, tx })) =
|
2021-04-04 21:34:52 +02:00
|
|
|
Pin::new(&mut this.rx2).poll_recv(cx)
|
2019-11-14 13:38:24 +01:00
|
|
|
{
|
2021-04-04 21:34:52 +02:00
|
|
|
this.availability.set(false);
|
2021-04-05 00:00:12 +02:00
|
|
|
let num = this.conns.total();
|
2018-12-06 23:04:42 +01:00
|
|
|
if num == 0 {
|
|
|
|
info!("Shutting down worker, 0 connections");
|
2021-04-04 22:53:19 +02:00
|
|
|
let _ = tx.send(true);
|
2019-11-14 13:38:24 +01:00
|
|
|
return Poll::Ready(());
|
2018-12-06 23:04:42 +01:00
|
|
|
} else if graceful {
|
2021-04-02 13:49:12 +02:00
|
|
|
info!("Graceful worker shutdown, {} connections", num);
|
2021-04-04 21:34:52 +02:00
|
|
|
this.shutdown(false);
|
|
|
|
|
2021-04-04 22:53:19 +02:00
|
|
|
this.state = WorkerState::Shutdown(Shutdown {
|
|
|
|
timer: Box::pin(sleep(Duration::from_secs(1))),
|
|
|
|
start_from: Instant::now(),
|
|
|
|
tx,
|
|
|
|
});
|
2018-12-06 23:04:42 +01:00
|
|
|
} else {
|
|
|
|
info!("Force shutdown worker, {} connections", num);
|
2021-04-04 21:34:52 +02:00
|
|
|
this.shutdown(true);
|
|
|
|
|
2021-04-04 22:53:19 +02:00
|
|
|
let _ = tx.send(false);
|
2019-11-14 13:38:24 +01:00
|
|
|
return Poll::Ready(());
|
2018-11-01 23:33:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 21:34:52 +02:00
|
|
|
match this.state {
|
|
|
|
WorkerState::Unavailable => match this.check_readiness(cx) {
|
2020-12-29 00:44:53 +01:00
|
|
|
Ok(true) => {
|
2021-04-04 21:34:52 +02:00
|
|
|
this.state = WorkerState::Available;
|
|
|
|
this.availability.set(true);
|
2020-12-29 00:44:53 +01:00
|
|
|
self.poll(cx)
|
2018-09-12 17:25:14 +02:00
|
|
|
}
|
2020-12-29 00:44:53 +01:00
|
|
|
Ok(false) => Poll::Pending,
|
|
|
|
Err((token, idx)) => {
|
2021-04-04 21:34:52 +02:00
|
|
|
this.restart_service(token, idx);
|
2020-12-29 00:44:53 +01:00
|
|
|
self.poll(cx)
|
|
|
|
}
|
|
|
|
},
|
2021-04-04 22:53:19 +02:00
|
|
|
WorkerState::Restarting(ref mut restart) => {
|
|
|
|
let factory_id = restart.factory_id;
|
|
|
|
let token = restart.token;
|
|
|
|
|
|
|
|
let item = ready!(restart.fut.as_mut().poll(cx)).unwrap_or_else(|_| {
|
2021-04-03 20:40:12 +02:00
|
|
|
panic!(
|
|
|
|
"Can not restart {:?} service",
|
2021-04-04 22:53:19 +02:00
|
|
|
this.factories[factory_id].name(token)
|
2021-04-03 20:40:12 +02:00
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
// Only interest in the first item?
|
|
|
|
let (token, service) = item
|
|
|
|
.into_iter()
|
|
|
|
.next()
|
|
|
|
.expect("No BoxedServerService. Restarting can not progress");
|
|
|
|
|
|
|
|
trace!(
|
|
|
|
"Service {:?} has been restarted",
|
2021-04-04 22:53:19 +02:00
|
|
|
this.factories[factory_id].name(token)
|
2021-04-03 20:40:12 +02:00
|
|
|
);
|
|
|
|
|
2021-04-04 21:34:52 +02:00
|
|
|
this.services[token.0].created(service);
|
|
|
|
this.state = WorkerState::Unavailable;
|
2021-04-03 20:40:12 +02:00
|
|
|
|
2019-12-02 17:30:09 +01:00
|
|
|
self.poll(cx)
|
2018-09-12 17:25:14 +02:00
|
|
|
}
|
2021-04-04 22:53:19 +02:00
|
|
|
WorkerState::Shutdown(ref mut shutdown) => {
|
2021-04-04 21:34:52 +02:00
|
|
|
// Wait for 1 second.
|
2021-04-04 22:53:19 +02:00
|
|
|
ready!(shutdown.timer.as_mut().poll(cx));
|
2021-04-04 21:34:52 +02:00
|
|
|
|
2021-04-05 00:00:12 +02:00
|
|
|
if this.conns.total() == 0 {
|
2021-04-04 21:34:52 +02:00
|
|
|
// Graceful shutdown.
|
2021-04-04 22:53:19 +02:00
|
|
|
if let WorkerState::Shutdown(shutdown) = mem::take(&mut this.state) {
|
|
|
|
let _ = shutdown.tx.send(true);
|
2021-04-04 21:34:52 +02:00
|
|
|
}
|
2021-01-31 05:41:28 +01:00
|
|
|
Arbiter::current().stop();
|
2021-04-04 21:34:52 +02:00
|
|
|
Poll::Ready(())
|
2021-04-04 22:53:19 +02:00
|
|
|
} else if shutdown.start_from.elapsed() >= this.shutdown_timeout {
|
2021-04-04 21:34:52 +02:00
|
|
|
// Timeout forceful shutdown.
|
2021-04-04 22:53:19 +02:00
|
|
|
if let WorkerState::Shutdown(shutdown) = mem::take(&mut this.state) {
|
|
|
|
let _ = shutdown.tx.send(false);
|
2021-04-04 21:34:52 +02:00
|
|
|
}
|
2021-01-31 05:41:28 +01:00
|
|
|
Arbiter::current().stop();
|
2021-04-04 21:34:52 +02:00
|
|
|
Poll::Ready(())
|
|
|
|
} else {
|
|
|
|
// Reset timer and wait for 1 second.
|
|
|
|
let time = Instant::now() + Duration::from_secs(1);
|
2021-04-04 22:53:19 +02:00
|
|
|
shutdown.timer.as_mut().reset(time);
|
|
|
|
shutdown.timer.as_mut().poll(cx)
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
2018-09-07 22:06:51 +02:00
|
|
|
}
|
2020-12-29 00:44:53 +01:00
|
|
|
// actively poll stream and handle worker command
|
|
|
|
WorkerState::Available => loop {
|
2021-04-04 21:34:52 +02:00
|
|
|
match this.check_readiness(cx) {
|
2021-04-04 14:22:34 +02:00
|
|
|
Ok(true) => {}
|
2020-12-29 00:44:53 +01:00
|
|
|
Ok(false) => {
|
|
|
|
trace!("Worker is unavailable");
|
2021-04-04 21:34:52 +02:00
|
|
|
this.availability.set(false);
|
|
|
|
this.state = WorkerState::Unavailable;
|
2020-12-29 00:44:53 +01:00
|
|
|
return self.poll(cx);
|
|
|
|
}
|
|
|
|
Err((token, idx)) => {
|
2021-04-04 21:34:52 +02:00
|
|
|
this.restart_service(token, idx);
|
|
|
|
this.availability.set(false);
|
2020-12-29 00:44:53 +01:00
|
|
|
return self.poll(cx);
|
2018-09-14 08:46:01 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-29 00:44:53 +01:00
|
|
|
|
2021-04-04 21:34:52 +02:00
|
|
|
match ready!(Pin::new(&mut this.rx).poll_recv(cx)) {
|
2020-12-29 00:44:53 +01:00
|
|
|
// handle incoming io stream
|
2021-04-02 13:22:05 +02:00
|
|
|
Some(WorkerCommand(msg)) => {
|
2021-04-04 21:34:52 +02:00
|
|
|
let guard = this.conns.get();
|
|
|
|
let _ = this.services[msg.token.0].service.call((guard, msg.io));
|
2020-12-29 00:44:53 +01:00
|
|
|
}
|
2021-04-02 13:22:05 +02:00
|
|
|
None => return Poll::Ready(()),
|
2020-12-29 00:44:53 +01:00
|
|
|
};
|
|
|
|
},
|
2019-12-02 17:30:09 +01:00
|
|
|
}
|
2018-09-07 22:06:51 +02:00
|
|
|
}
|
|
|
|
}
|