2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-01-14 01:30:38 -05:00
|
|
|
use crate::resources::Resource;
|
2018-09-18 14:53:16 -04:00
|
|
|
use futures;
|
|
|
|
use futures::Future;
|
2018-10-03 23:58:29 -04:00
|
|
|
use futures::Poll;
|
|
|
|
use std::io;
|
|
|
|
use std::mem;
|
|
|
|
use std::net::SocketAddr;
|
2018-09-18 14:53:16 -04:00
|
|
|
use tokio;
|
2018-10-03 23:58:29 -04:00
|
|
|
use tokio::net::TcpStream;
|
2019-04-23 16:27:44 -04:00
|
|
|
use tokio::runtime;
|
|
|
|
|
|
|
|
pub fn create_threadpool_runtime() -> tokio::runtime::Runtime {
|
|
|
|
runtime::Builder::new()
|
2019-05-28 09:32:43 -04:00
|
|
|
.panic_handler(|err| std::panic::resume_unwind(err))
|
2019-04-23 16:27:44 -04:00
|
|
|
.build()
|
|
|
|
.unwrap()
|
|
|
|
}
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
pub fn run<F>(future: F)
|
|
|
|
where
|
|
|
|
F: Future<Item = (), Error = ()> + Send + 'static,
|
|
|
|
{
|
|
|
|
// tokio::runtime::current_thread::run(future)
|
2019-04-23 16:27:44 -04:00
|
|
|
let rt = create_threadpool_runtime();
|
|
|
|
rt.block_on_all(future).unwrap();
|
2019-04-14 16:07:24 -04:00
|
|
|
}
|
|
|
|
|
2019-05-28 09:32:43 -04:00
|
|
|
/// THIS IS A HACK AND SHOULD BE AVOIDED.
|
|
|
|
///
|
|
|
|
/// This creates a new tokio runtime, with many new threads, to execute the
|
|
|
|
/// given future. This is useful when we want to block the main runtime to
|
|
|
|
/// resolve a future without worrying that we'll us up all the threads in the
|
|
|
|
/// main runtime.
|
2018-09-18 14:53:16 -04:00
|
|
|
pub fn block_on<F, R, E>(future: F) -> Result<R, E>
|
|
|
|
where
|
|
|
|
F: Send + 'static + Future<Item = R, Error = E>,
|
|
|
|
R: Send + 'static,
|
|
|
|
E: Send + 'static,
|
|
|
|
{
|
2019-05-28 09:32:43 -04:00
|
|
|
use std::sync::mpsc::channel;
|
|
|
|
use std::thread;
|
|
|
|
let (sender, receiver) = channel();
|
|
|
|
// Create a new runtime to evaluate the future asynchronously.
|
|
|
|
thread::spawn(move || {
|
|
|
|
let mut rt = create_threadpool_runtime();
|
|
|
|
let r = rt.block_on(future);
|
|
|
|
sender.send(r).unwrap();
|
|
|
|
});
|
|
|
|
receiver.recv().unwrap()
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the default executor so we can use tokio::spawn(). It's difficult to
|
|
|
|
// pass around mut references to the runtime, so using with_default is
|
|
|
|
// preferable. Ideally Tokio would provide this function.
|
2019-03-14 19:17:52 -04:00
|
|
|
#[cfg(test)]
|
2018-09-18 14:53:16 -04:00
|
|
|
pub fn init<F>(f: F)
|
|
|
|
where
|
|
|
|
F: FnOnce(),
|
|
|
|
{
|
2019-04-23 16:27:44 -04:00
|
|
|
let rt = create_threadpool_runtime();
|
2018-09-18 14:53:16 -04:00
|
|
|
let mut executor = rt.executor();
|
|
|
|
let mut enter = tokio_executor::enter().expect("Multiple executors at once");
|
2019-04-23 16:27:44 -04:00
|
|
|
tokio_executor::with_default(&mut executor, &mut enter, move |_enter| f());
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
2018-10-03 23:58:29 -04:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum AcceptState {
|
|
|
|
Pending(Resource),
|
|
|
|
Empty,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Simply accepts a connection.
|
|
|
|
pub fn accept(r: Resource) -> Accept {
|
|
|
|
Accept {
|
|
|
|
state: AcceptState::Pending(r),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A future which can be used to easily read available number of bytes to fill
|
|
|
|
/// a buffer.
|
|
|
|
///
|
|
|
|
/// Created by the [`read`] function.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Accept {
|
|
|
|
state: AcceptState,
|
|
|
|
}
|
|
|
|
impl Future for Accept {
|
|
|
|
type Item = (TcpStream, SocketAddr);
|
|
|
|
type Error = io::Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
let (stream, addr) = match self.state {
|
2019-05-23 14:22:52 -04:00
|
|
|
// Similar to try_ready!, but also track/untrack accept task
|
|
|
|
// in TcpListener resource.
|
|
|
|
// In this way, when the listener is closed, the task can be
|
|
|
|
// notified to error out (instead of stuck forever).
|
|
|
|
AcceptState::Pending(ref mut r) => match r.poll_accept() {
|
|
|
|
Ok(futures::prelude::Async::Ready(t)) => {
|
|
|
|
r.untrack_task();
|
|
|
|
t
|
|
|
|
}
|
|
|
|
Ok(futures::prelude::Async::NotReady) => {
|
|
|
|
// Would error out if another accept task is being tracked.
|
|
|
|
r.track_task()?;
|
|
|
|
return Ok(futures::prelude::Async::NotReady);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
r.untrack_task();
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
},
|
2018-10-03 23:58:29 -04:00
|
|
|
AcceptState::Empty => panic!("poll Accept after it's done"),
|
|
|
|
};
|
|
|
|
|
|
|
|
match mem::replace(&mut self.state, AcceptState::Empty) {
|
|
|
|
AcceptState::Pending(_) => Ok((stream, addr).into()),
|
|
|
|
AcceptState::Empty => panic!("invalid internal state"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-11 08:36:34 -05:00
|
|
|
|
|
|
|
/// `futures::future::poll_fn` only support `F: FnMut()->Poll<T, E>`
|
|
|
|
/// However, we require that `F: FnOnce()->Poll<T, E>`.
|
|
|
|
/// Therefore, we created our version of `poll_fn`.
|
|
|
|
pub fn poll_fn<T, E, F>(f: F) -> PollFn<F>
|
|
|
|
where
|
|
|
|
F: FnOnce() -> Poll<T, E>,
|
|
|
|
{
|
|
|
|
PollFn { inner: Some(f) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PollFn<F> {
|
|
|
|
inner: Option<F>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E, F> Future for PollFn<F>
|
|
|
|
where
|
|
|
|
F: FnOnce() -> Poll<T, E>,
|
|
|
|
{
|
|
|
|
type Item = T;
|
|
|
|
type Error = E;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<T, E> {
|
|
|
|
let f = self.inner.take().expect("Inner fn has been taken.");
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
}
|
2019-03-10 15:37:05 -04:00
|
|
|
|
|
|
|
pub fn panic_on_error<I, E, F>(f: F) -> impl Future<Item = I, Error = ()>
|
|
|
|
where
|
|
|
|
F: Future<Item = I, Error = E>,
|
|
|
|
E: std::fmt::Debug,
|
|
|
|
{
|
|
|
|
f.map_err(|err| panic!("Future got unexpected error: {:?}", err))
|
|
|
|
}
|