mirror of
https://github.com/OMGeeky/tarpc.git
synced 2026-01-05 19:16:29 +01:00
# Changes
## Client is now a trait
And `Channel<Req, Resp>` implements `Client<Req, Resp>`. Previously, `Client<Req, Resp>` was a thin wrapper around `Channel<Req, Resp>`.
This was changed to allow for mapping the request and response types. For example, you can take a `channel: Channel<Req, Resp>` and do:
```rust
channel
.with_request(|req: Req2| -> Req { ... })
.map_response(|resp: Resp| -> Resp2 { ... })
```
...which returns a type that implements `Client<Req2, Resp2>`.
### Why would you want to map request and response types?
The main benefit of this is that it enables creating different client types backed by the same channel. For example, you could run multiple clients multiplexing requests over a single `TcpStream`. I have a demo in `tarpc/examples/service_registry.rs` showing how you might do this with a bincode transport. I am considering factoring out the service registry portion of that to an actual library, because it's doing pretty cool stuff. For this PR, though, it'll just be part of the example.
## Client::new is now client::new
This is pretty minor, but necessary because async fns can't currently exist on traits. I changed `Server::new` to match this as well.
## Macro-generated Clients are generic over the backing Client.
This is a natural consequence of the above change. However, it is transparent to the user by keeping `Channel<Req, Resp>` as the default type for the `<C: Client>` type parameter. `new_stub` returns `Client<Channel<Req, Resp>>`, and other clients can be created via the `From` trait.
## example-service/ now has two binaries, one for client and one for server.
This serves as a "realistic" example of how one might set up a service. The other examples all run the client and server in the same binary, which isn't realistic in distributed systems use cases.
## `service!` trait fns take self by value.
Services are already cloned per request, so this just passes on that flexibility to the trait implementers.
# Open Questions
In the service registry example, multiple services are running on a single port, and thus multiple clients are sending requests over a single `TcpStream`. This has implications for throttling: [`max_in_flight_requests_per_connection`](https://github.com/google/tarpc/blob/master/rpc/src/server/mod.rs#L57-L60) will set a maximum for the sum of requests for all clients sharing a single connection. I think this is reasonable behavior, but users may expect this setting to act like `max_in_flight_requests_per_client`.
Fixes #103 #153 #205
203 lines
5.7 KiB
Rust
203 lines
5.7 KiB
Rust
// Copyright 2018 Google LLC
|
|
//
|
|
// Use of this source code is governed by an MIT-style
|
|
// license that can be found in the LICENSE file or at
|
|
// https://opensource.org/licenses/MIT.
|
|
|
|
//! A TCP [`Transport`] that serializes as bincode.
|
|
|
|
#![feature(
|
|
futures_api,
|
|
pin,
|
|
arbitrary_self_types,
|
|
await_macro,
|
|
async_await
|
|
)]
|
|
#![deny(missing_docs, missing_debug_implementations)]
|
|
|
|
use self::compat::Compat;
|
|
use async_bincode::{AsyncBincodeStream, AsyncDestination};
|
|
use futures::{
|
|
compat::{Compat01As03, Future01CompatExt, Stream01CompatExt},
|
|
prelude::*,
|
|
ready,
|
|
};
|
|
use pin_utils::unsafe_pinned;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::{
|
|
error::Error,
|
|
io,
|
|
marker::PhantomData,
|
|
net::SocketAddr,
|
|
pin::Pin,
|
|
task::{LocalWaker, Poll},
|
|
};
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
|
use tokio_tcp::{TcpListener, TcpStream};
|
|
|
|
mod compat;
|
|
|
|
/// A transport that serializes to, and deserializes from, a [`TcpStream`].
|
|
#[derive(Debug)]
|
|
pub struct Transport<S, Item, SinkItem> {
|
|
inner: Compat<AsyncBincodeStream<S, Item, SinkItem, AsyncDestination>, SinkItem>,
|
|
}
|
|
|
|
impl<S, Item, SinkItem> Transport<S, Item, SinkItem> {
|
|
/// Returns the transport underlying the bincode transport.
|
|
pub fn into_inner(self) -> S {
|
|
self.inner.into_inner().into_inner()
|
|
}
|
|
}
|
|
|
|
impl<S, Item, SinkItem> Transport<S, Item, SinkItem> {
|
|
unsafe_pinned!(
|
|
inner: Compat<AsyncBincodeStream<S, Item, SinkItem, AsyncDestination>, SinkItem>
|
|
);
|
|
}
|
|
|
|
impl<S, Item, SinkItem> Stream for Transport<S, Item, SinkItem>
|
|
where
|
|
S: AsyncRead,
|
|
Item: for<'a> Deserialize<'a>,
|
|
{
|
|
type Item = io::Result<Item>;
|
|
|
|
fn poll_next(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<io::Result<Item>>> {
|
|
match self.inner().poll_next(waker) {
|
|
Poll::Pending => Poll::Pending,
|
|
Poll::Ready(None) => Poll::Ready(None),
|
|
Poll::Ready(Some(Ok(next))) => Poll::Ready(Some(Ok(next))),
|
|
Poll::Ready(Some(Err(e))) => {
|
|
Poll::Ready(Some(Err(io::Error::new(io::ErrorKind::Other, e))))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<S, Item, SinkItem> Sink for Transport<S, Item, SinkItem>
|
|
where
|
|
S: AsyncWrite,
|
|
SinkItem: Serialize,
|
|
{
|
|
type SinkItem = SinkItem;
|
|
type SinkError = io::Error;
|
|
|
|
fn start_send(mut self: Pin<&mut Self>, item: SinkItem) -> io::Result<()> {
|
|
self.inner()
|
|
.start_send(item)
|
|
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
|
|
}
|
|
|
|
fn poll_ready(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<io::Result<()>> {
|
|
convert(self.inner().poll_ready(waker))
|
|
}
|
|
|
|
fn poll_flush(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<io::Result<()>> {
|
|
convert(self.inner().poll_flush(waker))
|
|
}
|
|
|
|
fn poll_close(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<io::Result<()>> {
|
|
convert(self.inner().poll_close(waker))
|
|
}
|
|
}
|
|
|
|
fn convert<E: Into<Box<Error + Send + Sync>>>(poll: Poll<Result<(), E>>) -> Poll<io::Result<()>> {
|
|
match poll {
|
|
Poll::Pending => Poll::Pending,
|
|
Poll::Ready(Ok(())) => Poll::Ready(Ok(())),
|
|
Poll::Ready(Err(e)) => Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, e))),
|
|
}
|
|
}
|
|
|
|
impl<Item, SinkItem> rpc::Transport for Transport<TcpStream, Item, SinkItem>
|
|
where
|
|
Item: for<'de> Deserialize<'de>,
|
|
SinkItem: Serialize,
|
|
{
|
|
type Item = Item;
|
|
type SinkItem = SinkItem;
|
|
|
|
fn peer_addr(&self) -> io::Result<SocketAddr> {
|
|
self.inner.get_ref().get_ref().peer_addr()
|
|
}
|
|
|
|
fn local_addr(&self) -> io::Result<SocketAddr> {
|
|
self.inner.get_ref().get_ref().local_addr()
|
|
}
|
|
}
|
|
|
|
/// Returns a new bincode transport that reads from and writes to `io`.
|
|
pub fn new<Item, SinkItem>(io: TcpStream) -> Transport<TcpStream, Item, SinkItem>
|
|
where
|
|
Item: for<'de> Deserialize<'de>,
|
|
SinkItem: Serialize,
|
|
{
|
|
Transport::from(io)
|
|
}
|
|
|
|
impl<S, Item, SinkItem> From<S> for Transport<S, Item, SinkItem> {
|
|
fn from(inner: S) -> Self {
|
|
Transport {
|
|
inner: Compat::new(AsyncBincodeStream::from(inner).for_async()),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Connects to `addr`, wrapping the connection in a bincode transport.
|
|
pub async fn connect<Item, SinkItem>(
|
|
addr: &SocketAddr,
|
|
) -> io::Result<Transport<TcpStream, Item, SinkItem>>
|
|
where
|
|
Item: for<'de> Deserialize<'de>,
|
|
SinkItem: Serialize,
|
|
{
|
|
Ok(new(await!(TcpStream::connect(addr).compat())?))
|
|
}
|
|
|
|
/// 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<TcpStream, 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))))
|
|
}
|
|
}
|