feat(lib): update Tokio, bytes, http, h2, and http-body

This commit is contained in:
Sean McArthur
2019-12-03 14:36:20 -08:00
parent 131962c86a
commit cb3f39c2dc
51 changed files with 985 additions and 1305 deletions

View File

@@ -1,34 +0,0 @@
use bytes::Buf;
use iovec::IoVec;
/// A `Buf` wrapping a static byte slice.
#[derive(Debug)]
pub(crate) struct StaticBuf(pub(crate) &'static [u8]);
impl Buf for StaticBuf {
#[inline]
fn remaining(&self) -> usize {
self.0.len()
}
#[inline]
fn bytes(&self) -> &[u8] {
self.0
}
#[inline]
fn advance(&mut self, cnt: usize) {
self.0 = &self.0[cnt..];
}
#[inline]
fn bytes_vec<'t>(&'t self, dst: &mut [&'t IoVec]) -> usize {
if dst.is_empty() || self.0.is_empty() {
0
} else {
dst[0] = self.0.into();
1
}
}
}

View File

@@ -1,12 +1,12 @@
use std::mem;
use futures_util::FutureExt as _;
use tokio_sync::{mpsc, watch};
use tokio::sync::{mpsc, watch};
use pin_project::pin_project;
use super::{Future, Never, Poll, Pin, task};
// Sentinel value signaling that the watch is still open
#[derive(Clone, Copy)]
enum Action {
Open,
// Closed isn't sent via the `Action` type, but rather once
@@ -103,10 +103,7 @@ where
loop {
match mem::replace(me.state, State::Draining) {
State::Watch(on_drain) => {
let recv = me.watch.rx.recv_ref();
futures_util::pin_mut!(recv);
match recv.poll_unpin(cx) {
match me.watch.rx.poll_recv_ref(cx) {
Poll::Ready(None) => {
// Drain has been triggered!
on_drain(me.future.as_mut());
@@ -151,7 +148,8 @@ mod tests {
#[test]
fn watch() {
tokio_test::task::mock(|cx| {
let mut mock = tokio_test::task::spawn(());
mock.enter(|cx, _| {
let (tx, rx) = channel();
let fut = TestMe {
draining: false,
@@ -198,7 +196,8 @@ mod tests {
#[test]
fn watch_clones() {
tokio_test::task::mock(|cx| {
let mut mock = tokio_test::task::spawn(());
mock.enter(|cx, _| {
let (tx, rx) = channel();
let fut1 = TestMe {

View File

@@ -3,48 +3,39 @@ use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use tokio_executor::{SpawnError, TypedExecutor};
use crate::body::{Payload, Body};
use crate::proto::h2::server::H2Stream;
use crate::server::conn::spawn_all::{NewSvcTask, Watcher};
use crate::service::HttpService;
/// An executor of futures.
pub trait Executor<Fut> {
/// Place the future into the executor to be run.
fn execute(&self, fut: Fut);
}
pub trait H2Exec<F, B: Payload>: Clone {
fn execute_h2stream(&mut self, fut: H2Stream<F, B>) -> crate::Result<()>;
fn execute_h2stream(&mut self, fut: H2Stream<F, B>);
}
pub trait NewSvcExec<I, N, S: HttpService<Body>, E, W: Watcher<I, S, E>>: Clone {
fn execute_new_svc(&mut self, fut: NewSvcTask<I, N, S, E, W>) -> crate::Result<()>;
fn execute_new_svc(&mut self, fut: NewSvcTask<I, N, S, E, W>);
}
type BoxFuture = Pin<Box<dyn Future<Output=()> + Send>>;
pub trait SharedExecutor {
fn shared_spawn(&self, future: BoxFuture) -> Result<(), SpawnError>;
}
impl<E> SharedExecutor for E
where
for<'a> &'a E: tokio_executor::Executor,
{
fn shared_spawn(mut self: &Self, future: BoxFuture) -> Result<(), SpawnError> {
tokio_executor::Executor::spawn(&mut self, future)
}
}
pub type BoxSendFuture = Pin<Box<dyn Future<Output=()> + Send>>;
// Either the user provides an executor for background tasks, or we use
// `tokio::spawn`.
#[derive(Clone)]
pub enum Exec {
Default,
Executor(Arc<dyn SharedExecutor + Send + Sync>),
Executor(Arc<dyn Executor<BoxSendFuture> + Send + Sync>),
}
// ===== impl Exec =====
impl Exec {
pub(crate) fn execute<F>(&self, fut: F) -> crate::Result<()>
pub(crate) fn execute<F>(&self, fut: F)
where
F: Future<Output=()> + Send + 'static,
{
@@ -52,34 +43,7 @@ impl Exec {
Exec::Default => {
#[cfg(feature = "tcp")]
{
use std::error::Error as StdError;
struct TokioSpawnError;
impl fmt::Debug for TokioSpawnError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt("tokio::spawn failed (is a tokio runtime running this future?)", f)
}
}
impl fmt::Display for TokioSpawnError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt("tokio::spawn failed (is a tokio runtime running this future?)", f)
}
}
impl StdError for TokioSpawnError {
fn description(&self) -> &str {
"tokio::spawn failed"
}
}
::tokio_executor::DefaultExecutor::current()
.spawn(Box::pin(fut))
.map_err(|err| {
warn!("executor error: {:?}", err);
crate::Error::new_execute(TokioSpawnError)
})
tokio::task::spawn(fut);
}
#[cfg(not(feature = "tcp"))]
{
@@ -88,11 +52,7 @@ impl Exec {
}
},
Exec::Executor(ref e) => {
e.shared_spawn(Box::pin(fut))
.map_err(|err| {
warn!("executor error: {:?}", err);
crate::Error::new_execute("custom executor failed")
})
e.execute(Box::pin(fut));
},
}
}
@@ -111,7 +71,7 @@ where
H2Stream<F, B>: Future<Output = ()> + Send + 'static,
B: Payload,
{
fn execute_h2stream(&mut self, fut: H2Stream<F, B>) -> crate::Result<()> {
fn execute_h2stream(&mut self, fut: H2Stream<F, B>) {
self.execute(fut)
}
}
@@ -122,7 +82,7 @@ where
S: HttpService<Body>,
W: Watcher<I, S, E>,
{
fn execute_new_svc(&mut self, fut: NewSvcTask<I, N, S, E, W>) -> crate::Result<()> {
fn execute_new_svc(&mut self, fut: NewSvcTask<I, N, S, E, W>) {
self.execute(fut)
}
}
@@ -131,34 +91,24 @@ where
impl<E, F, B> H2Exec<F, B> for E
where
E: TypedExecutor<H2Stream<F, B>> + Clone,
E: Executor<H2Stream<F, B>> + Clone,
H2Stream<F, B>: Future<Output=()>,
B: Payload,
{
fn execute_h2stream(&mut self, fut: H2Stream<F, B>) -> crate::Result<()> {
self.spawn(fut)
.map_err(|err| {
warn!("executor error: {:?}", err);
crate::Error::new_execute("custom executor failed")
})
fn execute_h2stream(&mut self, fut: H2Stream<F, B>) {
self.execute(fut)
}
}
impl<I, N, S, E, W> NewSvcExec<I, N, S, E, W> for E
where
E: TypedExecutor<NewSvcTask<I, N, S, E, W>> + Clone,
E: Executor<NewSvcTask<I, N, S, E, W>> + Clone,
NewSvcTask<I, N, S, E, W>: Future<Output=()>,
S: HttpService<Body>,
W: Watcher<I, S, E>,
{
fn execute_new_svc(&mut self, fut: NewSvcTask<I, N, S, E, W>) -> crate::Result<()> {
self.spawn(fut)
.map_err(|err| {
warn!("executor error: {:?}", err);
crate::Error::new_execute("custom executor failed")
})
fn execute_new_svc(&mut self, fut: NewSvcTask<I, N, S, E, W>) {
self.execute(fut)
}
}
// ===== StdError impls =====

View File

@@ -1,8 +1,8 @@
use std::io::{self, Read};
use std::{cmp, io};
use std::marker::Unpin;
use bytes::{Buf, Bytes, IntoBuf};
use tokio_io::{AsyncRead, AsyncWrite};
use bytes::{Buf, Bytes};
use tokio::io::{AsyncRead, AsyncWrite};
use crate::common::{Pin, Poll, task};
@@ -43,26 +43,22 @@ where
T: AsyncRead + Unpin,
{
#[inline]
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [std::mem::MaybeUninit<u8>]) -> bool {
self.inner.prepare_uninitialized_buffer(buf)
}
fn poll_read(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
if let Some(pre_bs) = self.pre.take() {
if let Some(mut prefix) = self.pre.take() {
// If there are no remaining bytes, let the bytes get dropped.
if pre_bs.len() > 0 {
let mut pre_reader = pre_bs.into_buf().reader();
let read_cnt = pre_reader.read(buf)?;
let mut new_pre = pre_reader.into_inner().into_inner();
new_pre.advance(read_cnt);
if prefix.len() > 0 {
let copy_len = cmp::min(prefix.len(), buf.len());
prefix.copy_to_slice(&mut buf[..copy_len]);
// Put back whats left
if new_pre.len() > 0 {
self.pre = Some(new_pre);
if prefix.len() > 0 {
self.pre = Some(prefix);
}
return Poll::Ready(Ok(read_cnt));
return Poll::Ready(Ok(copy_len));
}
}
Pin::new(&mut self.inner).poll_read(cx, buf)
@@ -118,7 +114,7 @@ mod tests {
// Rewind the stream so that it is as if we never read in the first place.
stream.rewind(Bytes::from(&buf[..]));
stream.rewind(Bytes::copy_from_slice(&buf[..]));
let mut buf = [0; 5];
stream
@@ -148,7 +144,7 @@ mod tests {
// Rewind the stream so that it is as if we never read in the first place.
stream.rewind(Bytes::from(&buf[..]));
stream.rewind(Bytes::copy_from_slice(&buf[..]));
let mut buf = [0; 5];
stream

View File

@@ -7,7 +7,6 @@ macro_rules! ready {
)
}
mod buf;
pub(crate) mod drain;
pub(crate) mod exec;
pub(crate) mod io;
@@ -15,8 +14,8 @@ mod lazy;
mod never;
pub(crate) mod task;
pub(crate) use self::buf::StaticBuf;
pub(crate) use self::exec::Exec;
pub(crate) use self::exec::{BoxSendFuture, Exec};
pub use self::exec::Executor;
pub(crate) use self::lazy::{lazy, Started as Lazy};
pub use self::never::Never;
pub(crate) use self::task::Poll;