This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
forked from hyperium/tonic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transport): Add service multiplexing/routing (hyperium#99)
* feat(transport): Add service multiplexing/routing This change introduces a new "router" built on top of `transport::Server` that allows one to run multiple gRPC services on the same socket. ```rust Server::builder() .add_service(greeter) .add_service(echo) .serve(addr) .await?; ``` There is also a new `multiplex` example showcasing server side service multiplexing and client side service multiplexing. BREAKING CHANGES: `Server::serve` is now crate private and all services must be added via `Server::add_service`. Codegen also returns just a `Service` now instead of a `MakeService` pair. Closes hyperium#29 Signed-off-by: Lucio Franco luciofranco14@gmail.com
- Loading branch information
1 parent
1b31f81
commit 18c6aa3
Showing
20 changed files
with
473 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
pub mod hello_world { | ||
tonic::include_proto!("helloworld"); | ||
} | ||
|
||
pub mod echo { | ||
tonic::include_proto!("grpc.examples.echo"); | ||
} | ||
|
||
use echo::{client::EchoClient, EchoRequest}; | ||
use hello_world::{client::GreeterClient, HelloRequest}; | ||
use tonic::transport::Endpoint; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let channel = Endpoint::from_static("http://[::1]:50051").channel(); | ||
|
||
let mut greeter_client = GreeterClient::new(channel.clone()); | ||
let mut echo_client = EchoClient::new(channel); | ||
|
||
let request = tonic::Request::new(HelloRequest { | ||
name: "Tonic".into(), | ||
}); | ||
|
||
let response = greeter_client.say_hello(request).await?; | ||
|
||
println!("GREETER RESPONSE={:?}", response); | ||
|
||
let request = tonic::Request::new(EchoRequest { | ||
message: "hello".into(), | ||
}); | ||
|
||
let response = echo_client.unary_echo(request).await?; | ||
|
||
println!("ECHO RESPONSE={:?}", response); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::collections::VecDeque; | ||
use tonic::{transport::Server, Request, Response, Status}; | ||
|
||
pub mod hello_world { | ||
tonic::include_proto!("helloworld"); | ||
} | ||
|
||
pub mod echo { | ||
tonic::include_proto!("grpc.examples.echo"); | ||
} | ||
|
||
use hello_world::{ | ||
server::{Greeter, GreeterServer}, | ||
HelloReply, HelloRequest, | ||
}; | ||
|
||
use echo::{ | ||
server::{Echo, EchoServer}, | ||
EchoRequest, EchoResponse, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let addr = "[::1]:50051".parse().unwrap(); | ||
|
||
let greeter = GreeterServer::new(MyGreeter::default()); | ||
let echo = EchoServer::new(MyEcho::default()); | ||
|
||
Server::builder() | ||
.add_service(greeter) | ||
.add_service(echo) | ||
.serve(addr) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct MyGreeter {} | ||
|
||
#[tonic::async_trait] | ||
impl Greeter for MyGreeter { | ||
async fn say_hello( | ||
&self, | ||
request: Request<HelloRequest>, | ||
) -> Result<Response<HelloReply>, Status> { | ||
let reply = hello_world::HelloReply { | ||
message: format!("Hello {}!", request.into_inner().name).into(), | ||
}; | ||
Ok(Response::new(reply)) | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct MyEcho; | ||
|
||
#[tonic::async_trait] | ||
impl Echo for MyEcho { | ||
async fn unary_echo( | ||
&self, | ||
request: Request<EchoRequest>, | ||
) -> Result<Response<EchoResponse>, Status> { | ||
let message = request.into_inner().message; | ||
Ok(Response::new(EchoResponse { message })) | ||
} | ||
|
||
type ServerStreamingEchoStream = VecDeque<Result<EchoResponse, Status>>; | ||
type BidirectionalStreamingEchoStream = VecDeque<Result<EchoResponse, Status>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.