transport: impl Service for Channel instead of GrpcService (#482)

* transport: impl Service for Channel instead of GrpcService

This adds tower::Service impl for tonic::transport::Channel
and removes GrpcService impl declaration.

Channel still implements GrpcService, thanks to the general
impl declaration of GrpcService for types who implement Service.

Fixes #481.

* examples: stop using GrpcService to avoid ambiguity

* examples: add timeout example
This commit is contained in:
Koki Kato
2021-01-05 13:36:18 -05:00
committed by GitHub
parent 2b9cdb9779
commit e7be352132
5 changed files with 88 additions and 7 deletions
+8
View File
@@ -74,6 +74,14 @@ path = "src/tower/server.rs"
name = "tower-client"
path = "src/tower/client.rs"
[[bin]]
name = "timeout-server"
path = "src/timeout/server.rs"
[[bin]]
name = "timeout-client"
path = "src/timeout/client.rs"
[[bin]]
name = "multiplex-server"
path = "src/multiplex/server.rs"
+28
View File
@@ -0,0 +1,28 @@
use hello_world::greeter_client::GreeterClient;
use hello_world::HelloRequest;
use std::time::Duration;
use tower::timeout::Timeout;
use tonic::transport::Channel;
pub mod hello_world {
tonic::include_proto!("helloworld");
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let channel = Channel::from_static("http://[::1]:50051").connect().await?;
let timeout_channel = Timeout::new(channel, Duration::from_millis(1000));
let mut client = GreeterClient::new(timeout_channel);
let request = tonic::Request::new(HelloRequest {
name: "Tonic".into(),
});
let response = client.say_hello(request).await?;
println!("RESPONSE={:?}", response);
Ok(())
}
+45
View File
@@ -0,0 +1,45 @@
use std::time::Duration;
use tokio::time::delay_for;
use tonic::{transport::Server, Request, Response, Status};
use hello_world::greeter_server::{Greeter, GreeterServer};
use hello_world::{HelloReply, HelloRequest};
pub mod hello_world {
tonic::include_proto!("helloworld");
}
#[derive(Default)]
pub struct MyGreeter {}
#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
request: Request<HelloRequest>,
) -> Result<Response<HelloReply>, Status> {
println!("Got a request from {:?}", request.remote_addr());
delay_for(Duration::from_millis(5000)).await;
let reply = hello_world::HelloReply {
message: format!("Hello {}!", request.into_inner().name),
};
Ok(Response::new(reply))
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse().unwrap();
let greeter = MyGreeter::default();
println!("GreeterServer listening on {}", addr);
Server::builder()
.add_service(GreeterServer::new(greeter))
.serve(addr)
.await?;
Ok(())
}
-1
View File
@@ -32,7 +32,6 @@ mod service {
use std::pin::Pin;
use std::task::{Context, Poll};
use tonic::body::BoxBody;
use tonic::client::GrpcService;
use tonic::transport::Body;
use tonic::transport::Channel;
use tower::Service;
+7 -6
View File
@@ -10,7 +10,7 @@ pub use endpoint::Endpoint;
pub use tls::ClientTlsConfig;
use super::service::{Connection, DynamicServiceStream};
use crate::{body::BoxBody, client::GrpcService};
use crate::body::BoxBody;
use bytes::Bytes;
use http::{
uri::{InvalidUri, Uri},
@@ -177,17 +177,18 @@ impl Channel {
}
}
impl GrpcService<BoxBody> for Channel {
type ResponseBody = hyper::Body;
impl Service<http::Request<BoxBody>> for Channel {
type Response = http::Response<super::Body>;
type Error = super::Error;
type Future = ResponseFuture;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
GrpcService::poll_ready(&mut self.svc, cx).map_err(super::Error::from_source)
Service::poll_ready(&mut self.svc, cx).map_err(super::Error::from_source)
}
fn call(&mut self, request: Request<BoxBody>) -> Self::Future {
let inner = GrpcService::call(&mut self.svc, request);
fn call(&mut self, request: http::Request<BoxBody>) -> Self::Future {
let inner = Service::call(&mut self.svc, request);
ResponseFuture { inner }
}
}