Remove deprecated tokio-proto and replace with homegrown rpc framework (#199)

# New Crates

- crate rpc contains the core client/server request-response framework, as well as a transport trait.
- crate bincode-transport implements a transport that works almost exactly as tarpc works today (not to say it's wire-compatible).
- crate trace has some foundational types for tracing. This isn't really fleshed out yet, but it's useful for in-process log tracing, at least.

All crates are now at the top level. e.g. tarpc-plugins is now tarpc/plugins rather than tarpc/src/plugins. tarpc itself is now a *very* small code surface, as most functionality has been moved into the other more granular crates.

# New Features
- deadlines: all requests specify a deadline, and a server will stop processing a response when past its deadline.
- client cancellation propagation: when a client drops a request, the client sends a message to the server informing it to cancel its response. This means cancellations can propagate across multiple server hops.
- trace context stuff as mentioned above
- more server configuration for total connection limits, per-connection request limits, etc.

# Removals
- no more shutdown handle.  I left it out for now because of time and not being sure what the right solution is.
- all async now, no blocking stub or server interface. This helps with maintainability, and async/await makes async code much more usable. The service trait is thusly renamed Service, and the client is renamed Client.
- no built-in transport. Tarpc is now transport agnostic (see bincode-transport for transitioning existing uses).
- going along with the previous bullet, no preferred transport means no TLS support at this time. We could make a tls transport or make bincode-transport compatible with TLS.
- a lot of examples were removed because I couldn't keep up with maintaining all of them. Hopefully the ones I kept are still illustrative.
- no more plugins!

# Open Questions

1. Should client.send() return `Future<Response>` or `Future<Future<Response>>`? The former appears more ergonomic but it doesn’t allow concurrent requests with a single client handle. The latter is less ergonomic but yields back control of the client once it’s successfully sent out the request. Should we offer fns for both?
2. Should rpc service! Fns take &mut self or &self or self? The service needs to impl Clone anyway, technically we only need to clone it once per connection, and then leave it up to the user to decide if they want to clone it per RPC. In practice, everyone doing nontrivial stuff will need to clone it per RPC, I think.
3. Do the request/response structs look ok?
4. Is supporting server shutdown/lameduck important?

Fixes #178 #155 #124 #104 #83 #38
This commit is contained in:
Tim
2018-10-16 11:26:27 -07:00
committed by GitHub
parent 5e4b97e589
commit 905e5be8bb
73 changed files with 4690 additions and 5143 deletions

View File

@@ -0,0 +1,285 @@
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the MIT License, <LICENSE or http://opensource.org/licenses/MIT>.
// This file may not be copied, modified, or distributed except according to those terms.
//! A TCP [`Transport`] that serializes as bincode.
#![feature(
futures_api,
pin,
arbitrary_self_types,
underscore_imports,
await_macro,
async_await,
)]
#![deny(missing_docs, missing_debug_implementations)]
mod vendored;
use bytes::{Bytes, BytesMut};
use crate::vendored::tokio_serde_bincode::{IoErrorWrapper, ReadBincode, WriteBincode};
use futures::{
Poll,
compat::{Compat01As03, Future01CompatExt, Stream01CompatExt},
prelude::*,
ready, task,
};
use futures_legacy::{
executor::{
self as executor01, Notify as Notify01, NotifyHandle as NotifyHandle01,
UnsafeNotify as UnsafeNotify01,
},
sink::SinkMapErr as SinkMapErr01,
sink::With as With01,
stream::MapErr as MapErr01,
Async as Async01, AsyncSink as AsyncSink01, Sink as Sink01, Stream as Stream01,
};
use pin_utils::unsafe_pinned;
use serde::{Deserialize, Serialize};
use std::{fmt, io, marker::PhantomData, net::SocketAddr, pin::Pin, task::LocalWaker};
use tokio::codec::{Framed, LengthDelimitedCodec, length_delimited};
use tokio_tcp::{self, TcpListener, TcpStream};
/// Returns a new bincode transport that reads from and writes to `io`.
pub fn new<Item, SinkItem>(io: TcpStream) -> Transport<Item, SinkItem>
where
Item: for<'de> Deserialize<'de>,
SinkItem: Serialize,
{
let peer_addr = io.peer_addr();
let local_addr = io.local_addr();
let inner = length_delimited::Builder::new()
.max_frame_length(8_000_000)
.new_framed(io)
.map_err(IoErrorWrapper as _)
.sink_map_err(IoErrorWrapper as _)
.with(freeze as _);
let inner = WriteBincode::new(inner);
let inner = ReadBincode::new(inner);
Transport {
inner,
staged_item: None,
peer_addr,
local_addr,
}
}
fn freeze(bytes: BytesMut) -> Result<Bytes, IoErrorWrapper> {
Ok(bytes.freeze())
}
/// Connects to `addr`, wrapping the connection in a bincode transport.
pub async fn connect<Item, SinkItem>(addr: &SocketAddr) -> io::Result<Transport<Item, SinkItem>>
where
Item: for<'de> Deserialize<'de>,
SinkItem: Serialize,
{
let stream = await!(TcpStream::connect(addr).compat())?;
Ok(new(stream))
}
/// Listens on `addr`, wrapping accepted connections in bincode transports.
pub fn listen<Item, SinkItem>(addr: &SocketAddr) -> io::Result<Incoming<Item, SinkItem>>
where
Item: for<'de> Deserialize<'de>,
SinkItem: Serialize,
{
let listener = TcpListener::bind(addr)?;
let local_addr = listener.local_addr()?;
let incoming = listener.incoming().compat();
Ok(Incoming {
incoming,
local_addr,
ghost: PhantomData,
})
}
/// A [`TcpListener`] that wraps connections in bincode transports.
#[derive(Debug)]
pub struct Incoming<Item, SinkItem> {
incoming: Compat01As03<tokio_tcp::Incoming>,
local_addr: SocketAddr,
ghost: PhantomData<(Item, SinkItem)>,
}
impl<Item, SinkItem> Incoming<Item, SinkItem> {
unsafe_pinned!(incoming: Compat01As03<tokio_tcp::Incoming>);
/// Returns the address being listened on.
pub fn local_addr(&self) -> SocketAddr {
self.local_addr
}
}
impl<Item, SinkItem> Stream for Incoming<Item, SinkItem>
where
Item: for<'a> Deserialize<'a>,
SinkItem: Serialize,
{
type Item = io::Result<Transport<Item, SinkItem>>;
fn poll_next(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<Self::Item>> {
let next = ready!(self.incoming().poll_next(waker)?);
Poll::Ready(next.map(|conn| Ok(new(conn))))
}
}
/// A transport that serializes to, and deserializes from, a [`TcpStream`].
pub struct Transport<Item, SinkItem> {
inner: ReadBincode<
WriteBincode<
With01<
SinkMapErr01<
MapErr01<
Framed<tokio_tcp::TcpStream, LengthDelimitedCodec>,
fn(std::io::Error) -> IoErrorWrapper,
>,
fn(std::io::Error) -> IoErrorWrapper,
>,
BytesMut,
fn(BytesMut) -> Result<Bytes, IoErrorWrapper>,
Result<Bytes, IoErrorWrapper>
>,
SinkItem,
>,
Item,
>,
staged_item: Option<SinkItem>,
peer_addr: io::Result<SocketAddr>,
local_addr: io::Result<SocketAddr>,
}
impl<Item, SinkItem> fmt::Debug for Transport<Item, SinkItem> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Transport")
}
}
impl<Item, SinkItem> Stream for Transport<Item, SinkItem>
where
Item: for<'a> Deserialize<'a>,
{
type Item = io::Result<Item>;
fn poll_next(self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<io::Result<Item>>> {
unsafe {
let inner = &mut Pin::get_mut_unchecked(self).inner;
let mut compat = inner.compat();
let compat = Pin::new_unchecked(&mut compat);
match ready!(compat.poll_next(waker)) {
None => Poll::Ready(None),
Some(Ok(next)) => Poll::Ready(Some(Ok(next))),
Some(Err(e)) => Poll::Ready(Some(Err(e.0))),
}
}
}
}
impl<Item, SinkItem> Sink for Transport<Item, SinkItem>
where
SinkItem: Serialize,
{
type SinkItem = SinkItem;
type SinkError = io::Error;
fn start_send(self: Pin<&mut Self>, item: SinkItem) -> io::Result<()> {
let me = unsafe { Pin::get_mut_unchecked(self) };
assert!(me.staged_item.is_none());
me.staged_item = Some(item);
Ok(())
}
fn poll_ready(self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<io::Result<()>> {
let notify = &WakerToHandle(waker);
executor01::with_notify(notify, 0, move || {
let me = unsafe { Pin::get_mut_unchecked(self) };
match me.staged_item.take() {
Some(staged_item) => match me.inner.start_send(staged_item)? {
AsyncSink01::Ready => Poll::Ready(Ok(())),
AsyncSink01::NotReady(item) => {
me.staged_item = Some(item);
Poll::Pending
}
},
None => Poll::Ready(Ok(())),
}
})
}
fn poll_flush(self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<io::Result<()>> {
let notify = &WakerToHandle(waker);
executor01::with_notify(notify, 0, move || {
let me = unsafe { Pin::get_mut_unchecked(self) };
match me.inner.poll_complete()? {
Async01::Ready(()) => Poll::Ready(Ok(())),
Async01::NotReady => Poll::Pending,
}
})
}
fn poll_close(self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<io::Result<()>> {
let notify = &WakerToHandle(waker);
executor01::with_notify(notify, 0, move || {
let me = unsafe { Pin::get_mut_unchecked(self) };
match me.inner.get_mut().close()? {
Async01::Ready(()) => Poll::Ready(Ok(())),
Async01::NotReady => Poll::Pending,
}
})
}
}
impl<Item, SinkItem> rpc::Transport for Transport<Item, SinkItem>
where
Item: for<'de> Deserialize<'de>,
SinkItem: Serialize,
{
type Item = Item;
type SinkItem = SinkItem;
fn peer_addr(&self) -> io::Result<SocketAddr> {
// TODO: should just access from the inner transport.
// https://github.com/alexcrichton/tokio-serde-bincode/issues/4
Ok(*self.peer_addr.as_ref().unwrap())
}
fn local_addr(&self) -> io::Result<SocketAddr> {
Ok(*self.local_addr.as_ref().unwrap())
}
}
#[derive(Clone, Debug)]
struct WakerToHandle<'a>(&'a LocalWaker);
#[derive(Debug)]
struct NotifyWaker(task::Waker);
impl Notify01 for NotifyWaker {
fn notify(&self, _: usize) {
self.0.wake();
}
}
unsafe impl UnsafeNotify01 for NotifyWaker {
unsafe fn clone_raw(&self) -> NotifyHandle01 {
let ptr = Box::new(NotifyWaker(self.0.clone()));
NotifyHandle01::new(Box::into_raw(ptr))
}
unsafe fn drop_raw(&self) {
let ptr: *const dyn UnsafeNotify01 = self;
drop(Box::from_raw(ptr as *mut dyn UnsafeNotify01));
}
}
impl<'a> From<WakerToHandle<'a>> for NotifyHandle01 {
fn from(handle: WakerToHandle<'a>) -> NotifyHandle01 {
unsafe { NotifyWaker(handle.0.clone().into_waker()).clone_raw() }
}
}

View File

@@ -0,0 +1 @@
pub(crate) mod tokio_serde_bincode;

View File

@@ -0,0 +1,224 @@
//! `Stream` and `Sink` adaptors for serializing and deserializing values using
//! Bincode.
//!
//! This crate provides adaptors for going from a stream or sink of buffers
//! ([`Bytes`]) to a stream or sink of values by performing Bincode encoding or
//! decoding. It is expected that each yielded buffer contains a single
//! serialized Bincode value. The specific strategy by which this is done is left
//! up to the user. One option is to use using [`length_delimited`] from
//! [tokio-io].
//!
//! [`Bytes`]: https://docs.rs/bytes/0.4/bytes/struct.Bytes.html
//! [`length_delimited`]: http://alexcrichton.com/tokio-io/tokio_io/codec/length_delimited/index.html
//! [tokio-io]: http://github.com/alexcrichton/tokio-io
//! [examples]: https://github.com/carllerche/tokio-serde-json/tree/master/examples
#![allow(missing_debug_implementations)]
use bincode::Error;
use bytes::{Bytes, BytesMut};
use futures_legacy::{Poll, Sink, StartSend, Stream};
use serde::{Deserialize, Serialize};
use std::io;
use tokio_serde::{Deserializer, FramedRead, FramedWrite, Serializer};
use std::marker::PhantomData;
/// Adapts a stream of Bincode encoded buffers to a stream of values by
/// deserializing them.
///
/// `ReadBincode` implements `Stream` by polling the inner buffer stream and
/// deserializing the buffer as Bincode. It expects that each yielded buffer
/// represents a single Bincode value and does not contain any extra trailing
/// bytes.
pub(crate) struct ReadBincode<T, U> {
inner: FramedRead<T, U, Bincode<U>>,
}
/// Adapts a buffer sink to a value sink by serializing the values as Bincode.
///
/// `WriteBincode` implements `Sink` by serializing the submitted values to a
/// buffer. The buffer is then sent to the inner stream, which is responsible
/// for handling framing on the wire.
pub(crate) struct WriteBincode<T: Sink, U> {
inner: FramedWrite<T, U, Bincode<U>>,
}
struct Bincode<T> {
ghost: PhantomData<T>,
}
impl<T, U> ReadBincode<T, U>
where
T: Stream<Error = IoErrorWrapper>,
U: for<'de> Deserialize<'de>,
Bytes: From<T::Item>,
{
/// Creates a new `ReadBincode` with the given buffer stream.
pub fn new(inner: T) -> ReadBincode<T, U> {
let json = Bincode { ghost: PhantomData };
ReadBincode {
inner: FramedRead::new(inner, json),
}
}
}
impl<T, U> ReadBincode<T, U> {
/// Returns a mutable reference to the underlying stream wrapped by
/// `ReadBincode`.
///
/// Note that care should be taken to not tamper with the underlying stream
/// of data coming in as it may corrupt the stream of frames otherwise
/// being worked with.
pub fn get_mut(&mut self) -> &mut T {
self.inner.get_mut()
}
}
impl<T, U> Stream for ReadBincode<T, U>
where
T: Stream<Error = IoErrorWrapper>,
U: for<'de> Deserialize<'de>,
Bytes: From<T::Item>,
{
type Item = U;
type Error = <T as Stream>::Error;
fn poll(&mut self) -> Poll<Option<U>, Self::Error> {
self.inner.poll()
}
}
impl<T, U> Sink for ReadBincode<T, U>
where
T: Sink,
{
type SinkItem = T::SinkItem;
type SinkError = T::SinkError;
fn start_send(&mut self, item: T::SinkItem) -> StartSend<T::SinkItem, T::SinkError> {
self.get_mut().start_send(item)
}
fn poll_complete(&mut self) -> Poll<(), T::SinkError> {
self.get_mut().poll_complete()
}
fn close(&mut self) -> Poll<(), T::SinkError> {
self.get_mut().close()
}
}
pub(crate) struct IoErrorWrapper(pub io::Error);
impl From<Box<bincode::ErrorKind>> for IoErrorWrapper {
fn from(e: Box<bincode::ErrorKind>) -> Self {
IoErrorWrapper(match *e {
bincode::ErrorKind::Io(e) => e,
bincode::ErrorKind::InvalidUtf8Encoding(e) => {
io::Error::new(io::ErrorKind::InvalidInput, e)
}
bincode::ErrorKind::InvalidBoolEncoding(e) => {
io::Error::new(io::ErrorKind::InvalidInput, e.to_string())
}
bincode::ErrorKind::InvalidTagEncoding(e) => {
io::Error::new(io::ErrorKind::InvalidInput, e.to_string())
}
bincode::ErrorKind::InvalidCharEncoding => {
io::Error::new(io::ErrorKind::InvalidInput, "Invalid char encoding")
}
bincode::ErrorKind::DeserializeAnyNotSupported => {
io::Error::new(io::ErrorKind::InvalidInput, "Deserialize Any not supported")
}
bincode::ErrorKind::SizeLimit => {
io::Error::new(io::ErrorKind::InvalidInput, "Size limit exceeded")
}
bincode::ErrorKind::SequenceMustHaveLength => {
io::Error::new(io::ErrorKind::InvalidInput, "Sequence must have length")
}
bincode::ErrorKind::Custom(s) => io::Error::new(io::ErrorKind::Other, s),
})
}
}
impl From<IoErrorWrapper> for io::Error {
fn from(wrapper: IoErrorWrapper) -> io::Error {
wrapper.0
}
}
impl<T, U> WriteBincode<T, U>
where
T: Sink<SinkItem = BytesMut, SinkError = IoErrorWrapper>,
U: Serialize,
{
/// Creates a new `WriteBincode` with the given buffer sink.
pub fn new(inner: T) -> WriteBincode<T, U> {
let json = Bincode { ghost: PhantomData };
WriteBincode {
inner: FramedWrite::new(inner, json),
}
}
}
impl<T: Sink, U> WriteBincode<T, U> {
/// Returns a mutable reference to the underlying sink wrapped by
/// `WriteBincode`.
///
/// Note that care should be taken to not tamper with the underlying sink as
/// it may corrupt the sequence of frames otherwise being worked with.
pub fn get_mut(&mut self) -> &mut T {
self.inner.get_mut()
}
}
impl<T, U> Sink for WriteBincode<T, U>
where
T: Sink<SinkItem = BytesMut, SinkError = IoErrorWrapper>,
U: Serialize,
{
type SinkItem = U;
type SinkError = <T as Sink>::SinkError;
fn start_send(&mut self, item: U) -> StartSend<U, Self::SinkError> {
self.inner.start_send(item)
}
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
self.inner.poll_complete()
}
fn close(&mut self) -> Poll<(), Self::SinkError> {
self.inner.poll_complete()
}
}
impl<T, U> Stream for WriteBincode<T, U>
where
T: Stream + Sink,
{
type Item = T::Item;
type Error = T::Error;
fn poll(&mut self) -> Poll<Option<T::Item>, T::Error> {
self.get_mut().poll()
}
}
impl<T> Deserializer<T> for Bincode<T>
where
T: for<'de> Deserialize<'de>,
{
type Error = Error;
fn deserialize(&mut self, src: &Bytes) -> Result<T, Error> {
bincode::deserialize(src)
}
}
impl<T: Serialize> Serializer<T> for Bincode<T> {
type Error = Error;
fn serialize(&mut self, item: &T) -> Result<BytesMut, Self::Error> {
bincode::serialize(item).map(Into::into)
}
}