update a chunk of stuff to the hyper release

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller
2019-12-12 15:27:07 +01:00
parent 3f1020b732
commit db0cb9ce0b
23 changed files with 253 additions and 164 deletions

View File

@ -1,10 +1,15 @@
//! Generic AsyncRead/AsyncWrite utilities.
use std::io;
use std::mem::MaybeUninit;
use std::os::unix::io::{AsRawFd, RawFd};
use std::pin::Pin;
use std::task::{Context, Poll};
use futures::stream::{Stream, TryStream};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::TcpListener;
use hyper::client::connect::Connection;
pub enum EitherStream<L, R> {
Left(L),
@ -27,7 +32,7 @@ impl<L: AsyncRead, R: AsyncRead> AsyncRead for EitherStream<L, R> {
}
}
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [MaybeUninit<u8>]) -> bool {
match *self {
EitherStream::Left(ref s) => s.prepare_uninitialized_buffer(buf),
EitherStream::Right(ref s) => s.prepare_uninitialized_buffer(buf),
@ -109,3 +114,83 @@ impl<L: AsyncWrite, R: AsyncWrite> AsyncWrite for EitherStream<L, R> {
}
}
}
// we need this for crate::client::http_client:
impl Connection for EitherStream<
tokio::net::TcpStream,
tokio_openssl::SslStream<tokio::net::TcpStream>,
> {
fn connected(&self) -> hyper::client::connect::Connected {
match self {
EitherStream::Left(s) => s.connected(),
EitherStream::Right(s) => s.get_ref().connected(),
}
}
}
/// Tokio's `Incoming` now is a reference type and hyper's `AddrIncoming` misses some standard
/// stuff like `AsRawFd`, so here's something implementing hyper's `Accept` from a `TcpListener`
pub struct StaticIncoming(TcpListener);
impl From<TcpListener> for StaticIncoming {
fn from(inner: TcpListener) -> Self {
Self(inner)
}
}
impl AsRawFd for StaticIncoming {
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}
impl hyper::server::accept::Accept for StaticIncoming {
type Conn = tokio::net::TcpStream;
type Error = std::io::Error;
fn poll_accept(
self: Pin<&mut Self>,
cx: &mut Context,
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
match self.get_mut().0.poll_accept(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(Ok((conn, _addr))) => Poll::Ready(Some(Ok(conn))),
Poll::Ready(Err(err)) => Poll::Ready(Some(Err(err))),
}
}
}
/// We also implement TryStream for this, as tokio doesn't do this anymore either and we want to be
/// able to map connections to then add eg. ssl to them. This support code makes the changes
/// required for hyper 0.13 a bit less annoying to read.
impl Stream for StaticIncoming {
type Item = std::io::Result<(tokio::net::TcpStream, std::net::SocketAddr)>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
match self.get_mut().0.poll_accept(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(result) => Poll::Ready(Some(result)),
}
}
}
/// Implement hyper's `Accept` for any `TryStream` of sockets:
pub struct HyperAccept<T>(pub T);
impl<T, I> hyper::server::accept::Accept for HyperAccept<T>
where
T: TryStream<Ok = I>,
I: AsyncRead + AsyncWrite,
{
type Conn = I;
type Error = T::Error;
fn poll_accept(
self: Pin<&mut Self>,
cx: &mut Context,
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
let this = unsafe { self.map_unchecked_mut(|this| &mut this.0) };
this.try_poll_next(cx)
}
}

View File

@ -193,7 +193,6 @@ impl Reloadable for tokio::net::TcpListener {
fd_change_cloexec(fd, true)?;
Ok(Self::from_std(
unsafe { std::net::TcpListener::from_raw_fd(fd) },
&tokio_net::driver::Handle::default(),
)?)
}
}

View File

@ -7,8 +7,7 @@ use std::task::{Context, Poll};
use failure::Error;
use futures::future::FutureExt;
use crate::tools::async_mutex::{AsyncLockGuard, AsyncMutex, LockFuture};
use tokio::sync::oneshot;
/// Make a future cancellable.
///
@ -42,11 +41,11 @@ use crate::tools::async_mutex::{AsyncLockGuard, AsyncMutex, LockFuture};
pub struct Cancellable<T: Future + Unpin> {
/// Our core: we're waiting on a future, on on a lock. The cancel method just unlocks the
/// lock, so that our LockFuture finishes.
inner: futures::future::Select<T, LockFuture<()>>,
inner: futures::future::Select<T, oneshot::Receiver<()>>,
/// When this future is created, this holds a guard. When a `Canceller` wants to cancel the
/// future, it'll drop this guard, causing our inner future to resolve to `None`.
guard: Arc<Mutex<Option<AsyncLockGuard<()>>>>,
sender: Arc<Mutex<Option<oneshot::Sender<()>>>>,
}
/// Reference to a cancellable future. Multiple instances may exist simultaneously.
@ -55,14 +54,14 @@ pub struct Cancellable<T: Future + Unpin> {
///
/// This can be cloned to be used in multiple places.
#[derive(Clone)]
pub struct Canceller(Arc<Mutex<Option<AsyncLockGuard<()>>>>);
pub struct Canceller(Arc<Mutex<Option<oneshot::Sender<()>>>>);
impl Canceller {
/// Cancel the associated future.
///
/// This does nothing if the future already finished successfully.
pub fn cancel(&self) {
*self.0.lock().unwrap() = None;
let _ = self.0.lock().unwrap().take().unwrap().send(());
}
}
@ -71,19 +70,20 @@ impl<T: Future + Unpin> Cancellable<T> {
///
/// Returns a future and a `Canceller` which can be cloned and used later to cancel the future.
pub fn new(inner: T) -> Result<(Self, Canceller), Error> {
// we don't even need to sture the mutex...
let (mutex, guard) = AsyncMutex::new_locked(())?;
// we don't even need to store the mutex...
let (tx, rx) = oneshot::channel();
let this = Self {
inner: futures::future::select(inner, mutex.lock()),
guard: Arc::new(Mutex::new(Some(guard))),
inner: futures::future::select(inner, rx),
sender: Arc::new(Mutex::new(Some(tx))),
};
let canceller = this.canceller();
Ok((this, canceller))
}
/// Create another `Canceller` for this future.
pub fn canceller(&self) -> Canceller {
Canceller(self.guard.clone())
Canceller(Arc::clone(&self.sender))
}
}

View File

@ -7,7 +7,7 @@ where
F: Future<Output = T> + Send + 'static,
T: std::fmt::Debug + Send + 'static,
{
let rt = tokio::runtime::Runtime::new().unwrap();
let mut rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let (tx, rx) = tokio::sync::oneshot::channel();

View File

@ -2,7 +2,7 @@ use std::io::{self, Read};
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio_executor::threadpool::blocking;
use tokio::task::block_in_place;
use futures::stream::Stream;
pub struct WrappedReaderStream<R: Read + Unpin> {
@ -24,8 +24,8 @@ impl<R: Read + Unpin> Stream for WrappedReaderStream<R> {
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
match blocking(|| this.reader.read(&mut this.buffer)) {
Poll::Ready(Ok(Ok(n))) => {
match block_in_place(|| this.reader.read(&mut this.buffer)) {
Ok(n) => {
if n == 0 {
// EOF
Poll::Ready(None)
@ -33,12 +33,7 @@ impl<R: Read + Unpin> Stream for WrappedReaderStream<R> {
Poll::Ready(Some(Ok(this.buffer[..n].to_vec())))
}
}
Poll::Ready(Ok(Err(err))) => Poll::Ready(Some(Err(err))),
Poll::Ready(Err(err)) => Poll::Ready(Some(Err(io::Error::new(
io::ErrorKind::Other,
err.to_string(),
)))),
Poll::Pending => Poll::Pending,
Err(err) => Poll::Ready(Some(Err(err))),
}
}
}