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:
@@ -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"
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
@@ -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(())
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user