Skip to content

Commit

Permalink
feat(grpc): multi-service support
Browse files Browse the repository at this point in the history
  • Loading branch information
Millione committed Dec 12, 2022
1 parent 52873d8 commit 02a895b
Show file tree
Hide file tree
Showing 27 changed files with 656 additions and 267 deletions.
35 changes: 22 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ lazy_static = "1"
linkedbytes = "0.1"
linked-hash-map = "0.5"
log = "0.4"
matchit = "0.7"
nom = "7"
normpath = "0.3"
num_enum = "0.5"
Expand Down
8 changes: 8 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ path = "src/compression/grpc_server.rs"
name = "compression-grpc-client"
path = "src/compression/grpc_client.rs"

# multiplex
[[bin]]
name = "multiplex-grpc-server"
path = "src/multiplex/grpc_server.rs"
[[bin]]
name = "multiplex-grpc-client"
path = "src/multiplex/grpc_client.rs"

[dependencies]
anyhow.workspace = true
async-trait.workspace = true
Expand Down
15 changes: 15 additions & 0 deletions examples/proto/echo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

package echo;

message EchoRequest {
string message = 1;
}

message EchoResponse {
string message = 1;
}

service Echo {
rpc UnaryEcho(EchoRequest) returns (EchoResponse) {}
}
6 changes: 3 additions & 3 deletions examples/proto/hello.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ syntax = "proto3";

package hello;

service HelloService {
rpc Hello (HelloRequest) returns (HelloResponse);
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
string name = 1;
}

message HelloResponse {
message HelloReply {
string message = 1;
}
10 changes: 5 additions & 5 deletions examples/src/compression/grpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use volo_grpc::codec::compression::{
};

lazy_static! {
static ref CLIENT: volo_gen::proto_gen::hello::HelloServiceClient = {
static ref CLIENT: volo_gen::proto_gen::hello::GreeterClient = {
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
volo_gen::proto_gen::hello::HelloServiceClientBuilder::new("hello")
volo_gen::proto_gen::hello::GreeterClientBuilder::new("hello")
.send_compressions(vec![
Gzip(Some(GzipConfig::default())),
Zlib(Some(ZlibConfig {
Expand All @@ -29,10 +29,10 @@ async fn main() {
let req = volo_gen::proto_gen::hello::HelloRequest {
name: "Volo".to_string(),
};
let resp = CLIENT.clone().hello(req).await;
let resp = CLIENT.clone().say_hello(req).await;

match resp {
Ok(info) => println!("{:?}", info),
Err(e) => eprintln!("{:?}", e),
Ok(info) => println!("{info:?}"),
Err(e) => eprintln!("{e:?}"),
}
}
37 changes: 22 additions & 15 deletions examples/src/compression/grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

use std::net::SocketAddr;

use volo_grpc::codec::compression::{
CompressionEncoding::{Gzip, Identity, Zlib},
GzipConfig, Level, ZlibConfig,
use volo_grpc::{
codec::compression::{
CompressionEncoding::{Gzip, Identity, Zlib},
GzipConfig, Level, ZlibConfig,
},
server::{Server, ServiceBuilder},
};

pub struct S;

#[volo::async_trait]
impl volo_gen::proto_gen::hello::HelloService for S {
async fn hello(
impl volo_gen::proto_gen::hello::Greeter for S {
async fn say_hello(
&self,
req: volo_grpc::Request<volo_gen::proto_gen::hello::HelloRequest>,
) -> Result<volo_grpc::Response<volo_gen::proto_gen::hello::HelloResponse>, volo_grpc::Status>
) -> Result<volo_grpc::Response<volo_gen::proto_gen::hello::HelloReply>, volo_grpc::Status>
{
let resp = volo_gen::proto_gen::hello::HelloResponse {
let resp = volo_gen::proto_gen::hello::HelloReply {
message: format!("Hello, {}!", req.get_ref().name),
};
Ok(volo_grpc::Response::new(resp))
Expand All @@ -28,14 +31,18 @@ async fn main() {
let addr: SocketAddr = "[::]:8080".parse().unwrap();
let addr = volo::net::Address::from(addr);

volo_gen::proto_gen::hello::HelloServiceServer::new(S)
.send_compressions(vec![
Zlib(Some(ZlibConfig {
level: Level::fast(),
})),
Gzip(Some(GzipConfig::default())),
])
.accept_compressions(vec![Gzip(None), Zlib(None), Identity])
Server::new()
.add_service(
ServiceBuilder::new(volo_gen::proto_gen::hello::GreeterServer::new(S))
.send_compressions(vec![
Zlib(Some(ZlibConfig {
level: Level::fast(),
})),
Gzip(Some(GzipConfig::default())),
])
.accept_compressions(vec![Gzip(None), Zlib(None), Identity])
.build(),
)
.run(addr)
.await
.unwrap();
Expand Down
10 changes: 5 additions & 5 deletions examples/src/hello/grpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::net::SocketAddr;
use lazy_static::lazy_static;

lazy_static! {
static ref CLIENT: volo_gen::proto_gen::hello::HelloServiceClient = {
static ref CLIENT: volo_gen::proto_gen::hello::GreeterClient = {
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
volo_gen::proto_gen::hello::HelloServiceClientBuilder::new("hello")
volo_gen::proto_gen::hello::GreeterClientBuilder::new("hello")
.address(addr)
.build()
};
Expand All @@ -18,9 +18,9 @@ async fn main() {
let req = volo_gen::proto_gen::hello::HelloRequest {
name: "Volo".to_string(),
};
let resp = CLIENT.clone().hello(req).await;
let resp = CLIENT.clone().say_hello(req).await;
match resp {
Ok(info) => println!("{:?}", info),
Err(e) => eprintln!("{:?}", e),
Ok(info) => println!("{info:?}"),
Err(e) => eprintln!("{e:?}"),
}
}
13 changes: 8 additions & 5 deletions examples/src/hello/grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

use std::net::SocketAddr;

use volo_grpc::server::{Server, ServiceBuilder};

pub struct S;

#[volo::async_trait]
impl volo_gen::proto_gen::hello::HelloService for S {
async fn hello(
impl volo_gen::proto_gen::hello::Greeter for S {
async fn say_hello(
&self,
req: volo_grpc::Request<volo_gen::proto_gen::hello::HelloRequest>,
) -> Result<volo_grpc::Response<volo_gen::proto_gen::hello::HelloResponse>, volo_grpc::Status>
) -> Result<volo_grpc::Response<volo_gen::proto_gen::hello::HelloReply>, volo_grpc::Status>
{
let resp = volo_gen::proto_gen::hello::HelloResponse {
let resp = volo_gen::proto_gen::hello::HelloReply {
message: format!("Hello, {}!", req.get_ref().name),
};
Ok(volo_grpc::Response::new(resp))
Expand All @@ -23,7 +25,8 @@ async fn main() {
let addr: SocketAddr = "[::]:8080".parse().unwrap();
let addr = volo::net::Address::from(addr);

volo_gen::proto_gen::hello::HelloServiceServer::new(S)
Server::new()
.add_service(ServiceBuilder::new(volo_gen::proto_gen::hello::GreeterServer::new(S)).build())
.run(addr)
.await
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/src/hello/thrift_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async fn main() {
.hello(req)
.await;
match resp {
Ok(info) => println!("{:?}", info),
Err(e) => eprintln!("{:?}", e),
Ok(info) => println!("{info:?}"),
Err(e) => eprintln!("{e:?}"),
}
}
41 changes: 41 additions & 0 deletions examples/src/multiplex/grpc_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#![feature(type_alias_impl_trait)]

use std::net::SocketAddr;

use lazy_static::lazy_static;

lazy_static! {
static ref GREETER_CLIENT: volo_gen::proto_gen::hello::GreeterClient = {
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
volo_gen::proto_gen::hello::GreeterClientBuilder::new("hello")
.address(addr)
.build()
};
static ref ECHO_CLIENT: volo_gen::proto_gen::echo::EchoClient = {
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
volo_gen::proto_gen::echo::EchoClientBuilder::new("hello")
.address(addr)
.build()
};
}

#[volo::main]
async fn main() {
let req = volo_gen::proto_gen::hello::HelloRequest {
name: "Volo".to_string(),
};
let resp = GREETER_CLIENT.clone().say_hello(req).await;
match resp {
Ok(info) => println!("GREETER: {info:?}"),
Err(e) => eprintln!("GREETER: {e:?}"),
}

let req = volo_gen::proto_gen::echo::EchoRequest {
message: "Volo".to_string(),
};
let resp = ECHO_CLIENT.clone().unary_echo(req).await;
match resp {
Ok(info) => println!("ECHO: {info:?}"),
Err(e) => eprintln!("ECHO: {e:?}"),
}
}
Loading

0 comments on commit 02a895b

Please sign in to comment.