2018-12-10 04:55:40 +01:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::io;
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
use futures::channel::mpsc::unbounded;
|
|
|
|
use futures::channel::oneshot::{channel, Receiver};
|
2018-12-10 04:55:40 +01:00
|
|
|
use futures::future::{lazy, Future};
|
2019-11-14 13:38:24 +01:00
|
|
|
use futures::{future, FutureExt};
|
2018-12-10 04:55:40 +01:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
use tokio::runtime::current_thread::Handle;
|
|
|
|
use tokio_net::driver::Reactor;
|
|
|
|
use tokio_timer::{clock::Clock, timer::Timer};
|
2018-12-10 04:55:40 +01:00
|
|
|
|
|
|
|
use crate::arbiter::{Arbiter, SystemArbiter};
|
|
|
|
use crate::runtime::Runtime;
|
|
|
|
use crate::system::System;
|
2019-11-14 13:38:24 +01:00
|
|
|
use tokio_executor::current_thread::CurrentThread;
|
2018-12-10 04:55:40 +01:00
|
|
|
|
|
|
|
/// Builder struct for a actix runtime.
|
|
|
|
///
|
|
|
|
/// Either use `Builder::build` to create a system and start actors.
|
|
|
|
/// Alternatively, use `Builder::run` to start the tokio runtime and
|
|
|
|
/// run a function in its context.
|
|
|
|
pub struct Builder {
|
|
|
|
/// Name of the System. Defaults to "actix" if unset.
|
|
|
|
name: Cow<'static, str>,
|
|
|
|
|
|
|
|
/// The clock to use
|
|
|
|
clock: Clock,
|
|
|
|
|
|
|
|
/// Whether the Arbiter will stop the whole System on uncaught panic. Defaults to false.
|
|
|
|
stop_on_panic: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Builder {
|
|
|
|
pub(crate) fn new() -> Self {
|
|
|
|
Builder {
|
|
|
|
name: Cow::Borrowed("actix"),
|
|
|
|
clock: Clock::new(),
|
|
|
|
stop_on_panic: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the name of the System.
|
|
|
|
pub fn name<T: Into<String>>(mut self, name: T) -> Self {
|
|
|
|
self.name = Cow::Owned(name.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the Clock instance that will be used by this System.
|
|
|
|
///
|
|
|
|
/// Defaults to the system clock.
|
|
|
|
pub fn clock(mut self, clock: Clock) -> Self {
|
|
|
|
self.clock = clock;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the option 'stop_on_panic' which controls whether the System is stopped when an
|
|
|
|
/// uncaught panic is thrown from a worker thread.
|
|
|
|
///
|
|
|
|
/// Defaults to false.
|
|
|
|
pub fn stop_on_panic(mut self, stop_on_panic: bool) -> Self {
|
|
|
|
self.stop_on_panic = stop_on_panic;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create new System.
|
|
|
|
///
|
|
|
|
/// This method panics if it can not create tokio runtime
|
|
|
|
pub fn build(self) -> SystemRunner {
|
|
|
|
self.create_runtime(|| {})
|
|
|
|
}
|
|
|
|
|
2019-05-23 20:34:47 +02:00
|
|
|
/// Create new System that can run asynchronously.
|
|
|
|
///
|
|
|
|
/// This method panics if it cannot start the system arbiter
|
2019-06-05 19:36:04 +02:00
|
|
|
pub(crate) fn build_async(self, executor: Handle) -> AsyncSystemRunner {
|
2019-05-23 20:34:47 +02:00
|
|
|
self.create_async_runtime(executor)
|
|
|
|
}
|
|
|
|
|
2018-12-10 04:55:40 +01:00
|
|
|
/// This function will start tokio runtime and will finish once the
|
|
|
|
/// `System::stop()` message get called.
|
|
|
|
/// Function `f` get called within tokio runtime context.
|
2019-03-06 19:24:58 +01:00
|
|
|
pub fn run<F>(self, f: F) -> io::Result<()>
|
2018-12-10 04:55:40 +01:00
|
|
|
where
|
|
|
|
F: FnOnce() + 'static,
|
|
|
|
{
|
|
|
|
self.create_runtime(f).run()
|
|
|
|
}
|
|
|
|
|
2019-06-05 19:36:04 +02:00
|
|
|
fn create_async_runtime(self, executor: Handle) -> AsyncSystemRunner {
|
2019-05-23 20:34:47 +02:00
|
|
|
let (stop_tx, stop) = channel();
|
|
|
|
let (sys_sender, sys_receiver) = unbounded();
|
|
|
|
|
|
|
|
let system = System::construct(sys_sender, Arbiter::new_system(), self.stop_on_panic);
|
|
|
|
|
|
|
|
// system arbiter
|
|
|
|
let arb = SystemArbiter::new(stop_tx, sys_receiver);
|
|
|
|
|
|
|
|
// start the system arbiter
|
|
|
|
executor.spawn(arb).expect("could not start system arbiter");
|
|
|
|
|
|
|
|
AsyncSystemRunner { stop, system }
|
|
|
|
}
|
|
|
|
|
2018-12-10 04:55:40 +01:00
|
|
|
fn create_runtime<F>(self, f: F) -> SystemRunner
|
|
|
|
where
|
|
|
|
F: FnOnce() + 'static,
|
|
|
|
{
|
|
|
|
let (stop_tx, stop) = channel();
|
|
|
|
let (sys_sender, sys_receiver) = unbounded();
|
|
|
|
|
2019-03-22 18:29:12 +01:00
|
|
|
let system = System::construct(sys_sender, Arbiter::new_system(), self.stop_on_panic);
|
2018-12-10 04:55:40 +01:00
|
|
|
|
|
|
|
// system arbiter
|
|
|
|
let arb = SystemArbiter::new(stop_tx, sys_receiver);
|
|
|
|
|
|
|
|
let mut rt = self.build_rt().unwrap();
|
|
|
|
rt.spawn(arb);
|
|
|
|
|
|
|
|
// init system arbiter and run configuration method
|
2019-11-14 13:38:24 +01:00
|
|
|
let _ = rt.block_on(lazy(move |_| {
|
2018-12-10 04:55:40 +01:00
|
|
|
f();
|
|
|
|
Ok::<_, ()>(())
|
|
|
|
}));
|
|
|
|
|
|
|
|
SystemRunner { rt, stop, system }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn build_rt(&self) -> io::Result<Runtime> {
|
|
|
|
// We need a reactor to receive events about IO objects from kernel
|
|
|
|
let reactor = Reactor::new()?;
|
|
|
|
let reactor_handle = reactor.handle();
|
|
|
|
|
|
|
|
// Place a timer wheel on top of the reactor. If there are no timeouts to fire, it'll let the
|
|
|
|
// reactor pick up some new external events.
|
|
|
|
let timer = Timer::new_with_now(reactor, self.clock.clone());
|
|
|
|
let timer_handle = timer.handle();
|
|
|
|
|
|
|
|
// And now put a single-threaded executor on top of the timer. When there are no futures ready
|
|
|
|
// to do something, it'll let the timer or the reactor to generate some new stimuli for the
|
|
|
|
// futures to continue in their life.
|
|
|
|
let executor = CurrentThread::new_with_park(timer);
|
|
|
|
|
|
|
|
Ok(Runtime::new2(
|
|
|
|
reactor_handle,
|
|
|
|
timer_handle,
|
|
|
|
self.clock.clone(),
|
|
|
|
executor,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 20:34:47 +02:00
|
|
|
#[derive(Debug)]
|
2019-06-05 19:36:04 +02:00
|
|
|
pub(crate) struct AsyncSystemRunner {
|
2019-05-23 20:34:47 +02:00
|
|
|
stop: Receiver<i32>,
|
|
|
|
system: System,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncSystemRunner {
|
|
|
|
/// This function will start event loop and returns a future that
|
|
|
|
/// resolves once the `System::stop()` function is called.
|
2019-11-14 13:38:24 +01:00
|
|
|
pub(crate) fn run_nonblocking(self) -> impl Future<Output = Result<(), io::Error>> + Send {
|
2019-05-23 20:34:47 +02:00
|
|
|
let AsyncSystemRunner { stop, .. } = self;
|
|
|
|
|
|
|
|
// run loop
|
2019-11-14 13:38:24 +01:00
|
|
|
future::lazy(|_| {
|
2019-06-05 19:36:04 +02:00
|
|
|
Arbiter::run_system();
|
2019-11-14 13:38:24 +01:00
|
|
|
async {
|
|
|
|
let res = match stop.await {
|
|
|
|
Ok(code) => {
|
|
|
|
if code != 0 {
|
|
|
|
Err(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
format!("Non-zero exit code: {}", code),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-05-23 20:34:47 +02:00
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
|
|
|
|
};
|
2019-05-23 20:34:47 +02:00
|
|
|
Arbiter::stop_system();
|
2019-11-14 13:38:24 +01:00
|
|
|
return res;
|
|
|
|
}
|
2019-06-22 05:02:17 +02:00
|
|
|
})
|
2019-11-14 13:38:24 +01:00
|
|
|
.flatten()
|
2019-05-23 20:34:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 04:55:40 +01:00
|
|
|
/// Helper object that runs System's event loop
|
|
|
|
#[must_use = "SystemRunner must be run"]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct SystemRunner {
|
|
|
|
rt: Runtime,
|
|
|
|
stop: Receiver<i32>,
|
|
|
|
system: System,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SystemRunner {
|
|
|
|
/// This function will start event loop and will finish once the
|
|
|
|
/// `System::stop()` function is called.
|
2019-03-06 19:24:58 +01:00
|
|
|
pub fn run(self) -> io::Result<()> {
|
2018-12-10 04:55:40 +01:00
|
|
|
let SystemRunner { mut rt, stop, .. } = self;
|
|
|
|
|
|
|
|
// run loop
|
2019-11-14 13:38:24 +01:00
|
|
|
let _ = rt.block_on(async {
|
2018-12-10 04:55:40 +01:00
|
|
|
Arbiter::run_system();
|
|
|
|
Ok::<_, ()>(())
|
2019-11-14 13:38:24 +01:00
|
|
|
});
|
2019-03-06 19:24:58 +01:00
|
|
|
let result = match rt.block_on(stop) {
|
|
|
|
Ok(code) => {
|
|
|
|
if code != 0 {
|
|
|
|
Err(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
format!("Non-zero exit code: {}", code),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
|
2018-12-10 04:55:40 +01:00
|
|
|
};
|
|
|
|
Arbiter::stop_system();
|
2019-03-06 19:24:58 +01:00
|
|
|
result
|
2018-12-10 04:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute a future and wait for result.
|
2019-11-14 13:38:24 +01:00
|
|
|
pub fn block_on<F, O>(&mut self, fut: F) -> O
|
2018-12-10 04:55:40 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
F: Future<Output = O>,
|
2018-12-10 04:55:40 +01:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
let _ = self.rt.block_on(async {
|
2018-12-10 04:55:40 +01:00
|
|
|
Arbiter::run_system();
|
2019-11-14 13:38:24 +01:00
|
|
|
});
|
|
|
|
|
2018-12-10 04:55:40 +01:00
|
|
|
let res = self.rt.block_on(fut);
|
2019-11-14 13:38:24 +01:00
|
|
|
let _ = self.rt.block_on(async {
|
2018-12-10 04:55:40 +01:00
|
|
|
Arbiter::stop_system();
|
2019-11-14 13:38:24 +01:00
|
|
|
});
|
|
|
|
|
2018-12-10 04:55:40 +01:00
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|