Upgrade to Tokio 1.0.0 ecosystem (#530)

* Upgrade Tonic to Tokio 1.0

Work in progress for updating Tonic to Tokio 1.0. Since tower has not
been released to crates.io, a git dependency is taken instead.

* Upgrade Tonic to Tokio 1.0 phase 2

* tonic: remove tower-* deps

* Apply suggestions from code review

Co-authored-by: Ed Marshall <[email protected]>
Co-authored-by: Lucio Franco <[email protected]>
This commit is contained in:
Quentin Perez
2021-01-11 12:18:34 -05:00
committed by GitHub
co-authored by Ed Marshall Lucio Franco
parent fe4d5b9d9a
commit fdda5ae26a
42 changed files with 185 additions and 185 deletions
+4 -2
View File
@@ -36,9 +36,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
match listenfd::ListenFd::from_env().take_tcp_listener(0)? {
Some(listener) => {
let mut listener = tokio::net::TcpListener::from_std(listener)?;
let listener = tokio_stream::wrappers::TcpListenerStream::new(
tokio::net::TcpListener::from_std(listener)?,
);
server.serve_with_incoming(listener.incoming()).await?;
server.serve_with_incoming(listener).await?;
}
None => {
server.serve(addr).await?;
+1 -5
View File
@@ -24,11 +24,7 @@ impl BlockingClient {
D: std::convert::TryInto<tonic::transport::Endpoint>,
D::Error: Into<StdError>,
{
let mut rt = Builder::new()
.basic_scheduler()
.enable_all()
.build()
.unwrap();
let rt = Builder::new_multi_thread().enable_all().build().unwrap();
let client = rt.block_on(GreeterClient::connect(dst))?;
Ok(Self { rt, client })
+8 -8
View File
@@ -18,42 +18,42 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let e1 = Endpoint::from_static("http://[::1]:50051");
let e2 = Endpoint::from_static("http://[::1]:50052");
let (channel, mut rx) = Channel::balance_channel(10);
let (channel, rx) = Channel::balance_channel(10);
let mut client = EchoClient::new(channel);
let done = Arc::new(AtomicBool::new(false));
let demo_done = done.clone();
tokio::spawn(async move {
tokio::time::delay_for(tokio::time::Duration::from_secs(5)).await;
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
println!("Added first endpoint");
let change = Change::Insert("1", e1);
let res = rx.send(change).await;
println!("{:?}", res);
tokio::time::delay_for(tokio::time::Duration::from_secs(5)).await;
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
println!("Added second endpoint");
let change = Change::Insert("2", e2);
let res = rx.send(change).await;
println!("{:?}", res);
tokio::time::delay_for(tokio::time::Duration::from_secs(5)).await;
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
println!("Removed first endpoint");
let change = Change::Remove("1");
let res = rx.send(change).await;
println!("{:?}", res);
tokio::time::delay_for(tokio::time::Duration::from_secs(5)).await;
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
println!("Removed second endpoint");
let change = Change::Remove("2");
let res = rx.send(change).await;
println!("{:?}", res);
tokio::time::delay_for(tokio::time::Duration::from_secs(5)).await;
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
println!("Added third endpoint");
let e3 = Endpoint::from_static("http://[::1]:50051");
let change = Change::Insert("3", e3);
let res = rx.send(change).await;
println!("{:?}", res);
tokio::time::delay_for(tokio::time::Duration::from_secs(5)).await;
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
println!("Removed third endpoint");
let change = Change::Remove("3");
let res = rx.send(change).await;
@@ -62,7 +62,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
});
while !done.load(SeqCst) {
tokio::time::delay_for(tokio::time::Duration::from_millis(500)).await;
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
let request = tonic::Request::new(EchoRequest {
message: "hello".into(),
});
+1 -2
View File
@@ -3,7 +3,6 @@ use tonic::{transport::Server, Request, Response, Status};
use hello_world::greeter_server::{Greeter, GreeterServer};
use hello_world::{HelloReply, HelloRequest};
use std::time::Duration;
use tokio::time::delay_for;
use tonic_health::server::HealthReporter;
pub mod hello_world {
@@ -34,7 +33,7 @@ async fn twiddle_service_status(mut reporter: HealthReporter) {
let mut iter = 0u64;
loop {
iter += 1;
delay_for(Duration::from_secs(1)).await;
tokio::time::sleep(Duration::from_secs(1)).await;
if iter % 2 == 0 {
reporter.set_serving::<GreeterServer<MyGreeter>>().await;
+3 -3
View File
@@ -41,7 +41,7 @@ async fn print_features(client: &mut RouteGuideClient<Channel>) -> Result<(), Bo
async fn run_record_route(client: &mut RouteGuideClient<Channel>) -> Result<(), Box<dyn Error>> {
let mut rng = rand::thread_rng();
let point_count: i32 = rng.gen_range(2, 100);
let point_count: i32 = rng.gen_range(2..100);
let mut points = vec![];
for _ in 0..=point_count {
@@ -115,8 +115,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
fn random_point(rng: &mut ThreadRng) -> Point {
let latitude = (rng.gen_range(0, 180) - 90) * 10_000_000;
let longitude = (rng.gen_range(0, 360) - 180) * 10_000_000;
let latitude = (rng.gen_range(0..180) - 90) * 10_000_000;
let longitude = (rng.gen_range(0..360) - 180) * 10_000_000;
Point {
latitude,
longitude,
+6 -3
View File
@@ -36,7 +36,8 @@ impl RouteGuide for RouteGuideService {
Ok(Response::new(Feature::default()))
}
type ListFeaturesStream = mpsc::Receiver<Result<Feature, Status>>;
type ListFeaturesStream =
Pin<Box<dyn Stream<Item = Result<Feature, Status>> + Send + Sync + 'static>>;
async fn list_features(
&self,
@@ -44,7 +45,7 @@ impl RouteGuide for RouteGuideService {
) -> Result<Response<Self::ListFeaturesStream>, Status> {
println!("ListFeatures = {:?}", request);
let (mut tx, rx) = mpsc::channel(4);
let (tx, rx) = mpsc::channel(4);
let features = self.features.clone();
tokio::spawn(async move {
@@ -58,7 +59,9 @@ impl RouteGuide for RouteGuideService {
println!(" /// done sending");
});
Ok(Response::new(rx))
Ok(Response::new(Box::pin(
tokio_stream::wrappers::ReceiverStream::new(rx),
)))
}
async fn record_route(
+2 -2
View File
@@ -1,5 +1,5 @@
use std::time::Duration;
use tokio::time::delay_for;
use tokio::time::sleep;
use tonic::{transport::Server, Request, Response, Status};
use hello_world::greeter_server::{Greeter, GreeterServer};
@@ -20,7 +20,7 @@ impl Greeter for MyGreeter {
) -> Result<Response<HelloReply>, Status> {
println!("Got a request from {:?}", request.remote_addr());
delay_for(Duration::from_millis(5000)).await;
sleep(Duration::from_millis(5000)).await;
let reply = hello_world::HelloReply {
message: format!("Hello {}!", request.into_inner().name),
+15 -7
View File
@@ -1,6 +1,6 @@
#![cfg_attr(not(unix), allow(unused_imports))]
use futures::stream::TryStreamExt;
use futures::TryFutureExt;
use std::path::Path;
#[cfg(unix)]
use tokio::net::UnixListener;
@@ -40,13 +40,21 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
tokio::fs::create_dir_all(Path::new(path).parent().unwrap()).await?;
let mut uds = UnixListener::bind(path)?;
let greeter = MyGreeter::default();
let incoming = {
let uds = UnixListener::bind(path)?;
async_stream::stream! {
while let item = uds.accept().map_ok(|(st, _)| unix::UnixStream(st)).await {
yield item;
}
}
};
Server::builder()
.add_service(GreeterServer::new(greeter))
.serve_with_incoming(uds.incoming().map_ok(unix::UnixStream))
.serve_with_incoming(incoming)
.await?;
Ok(())
@@ -59,7 +67,7 @@ mod unix {
task::{Context, Poll},
};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tonic::transport::server::Connected;
#[derive(Debug)]
@@ -71,8 +79,8 @@ mod unix {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<std::io::Result<usize>> {
buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.0).poll_read(cx, buf)
}
}