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 Nov 29, 2022
1 parent 777886e commit ef87308
Show file tree
Hide file tree
Showing 13 changed files with 440 additions and 212 deletions.
91 changes: 37 additions & 54 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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 Expand Up @@ -98,4 +99,4 @@ debug-assertions = false
codegen-units = 1
panic = 'unwind'
incremental = false
overflow-checks = false
overflow-checks = false
4 changes: 2 additions & 2 deletions examples/src/hello/grpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() {
};
let resp = CLIENT.clone().hello(req).await;
match resp {
Ok(info) => println!("{:?}", info),
Err(e) => eprintln!("{:?}", e),
Ok(info) => println!("{info:?}"),
Err(e) => eprintln!("{e:?}"),
}
}
7 changes: 6 additions & 1 deletion examples/src/hello/grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use std::net::SocketAddr;

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

pub struct S;

#[volo::async_trait]
Expand All @@ -23,7 +25,10 @@ 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::HelloServiceServer::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:?}"),
}
}
20 changes: 12 additions & 8 deletions volo-build/src/grpc_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ impl CodegenBackend for VoloGrpcBackend {
let file = self.cx.file(file_id).unwrap();

let package = file.package.iter().join(".");
let name = format!("{package}.{}", s.name);

let req_enum_name_send = format_ident!("{}RequestSend", service_name);
let resp_enum_name_send = format_ident!("{}ResponseSend", service_name);
Expand All @@ -195,7 +196,7 @@ impl CodegenBackend for VoloGrpcBackend {
let paths = s
.methods
.iter()
.map(|method| format!("/{}.{}/{}", package, s.name, method.name))
.map(|method| format!("/{package}.{}/{}", s.name, method.name))
.collect::<Vec<_>>();

let req_matches = s.methods.iter().map(|method| {
Expand All @@ -204,7 +205,7 @@ impl CodegenBackend for VoloGrpcBackend {
.rust_name(method.def_id)
.upper_camel_ident()
.as_syn_ident();
let path = format!("/{}.{}/{}", package, s.name, method.name);
let path = format!("/{package}.{}/{}", s.name, method.name);
let client_streaming = self.cx.node_contains_tag::<ClientStreaming>(method.def_id);
let input_ty = &method.args[0].ty;

Expand Down Expand Up @@ -261,7 +262,7 @@ impl CodegenBackend for VoloGrpcBackend {
let client_methods = s.methods.iter().map(|method| {
let method_name = self.cx.rust_name(method.def_id).as_syn_ident();

let path = format!("/{}.{}/{}", package, s.name, method.name);
let path = format!("/{package}.{}/{}", s.name, method.name);
let input_ty = &method.args[0].ty;
let client_streaming = self.cx.node_contains_tag::<ClientStreaming>(method.def_id);
let req_ty = self.client_input_ty(input_ty.clone(), client_streaming);
Expand Down Expand Up @@ -368,7 +369,7 @@ impl CodegenBackend for VoloGrpcBackend {
#req_enum_name_send,
#resp_enum_name_recv,
> {
::volo_grpc::client::ClientBuilder::new(MkHelloServiceGenericClient, service_name)
::volo_grpc::client::ClientBuilder::new(#mk_client_name, service_name)
}
}

Expand Down Expand Up @@ -409,11 +410,10 @@ impl CodegenBackend for VoloGrpcBackend {
}

impl<S> #server_name<S> {
pub fn new(inner: S) -> ::volo_grpc::server::Server<Self, ::volo::layer::Identity> {
let service = Self {
pub fn new(inner: S) -> Self {
Self {
inner: ::std::sync::Arc::new(inner),
};
::volo_grpc::server::Server::new(service)
}
}
}

Expand Down Expand Up @@ -441,6 +441,10 @@ impl CodegenBackend for VoloGrpcBackend {
}
}
}

impl<S: #service_name> ::volo_grpc::server::NamedService for #server_name<S> {
const NAME: &'static str = #name;
}
});
}

Expand Down
4 changes: 3 additions & 1 deletion volo-grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ maintenance = { status = "actively-developed" }

[dependencies]
volo = { version = "0.2", path = "../volo" }
motore.workspace = true
motore = { workspace = true, features = ["tower"] }
metainfo.workspace = true

anyhow.workspace = true
async-stream.workspace = true
async-trait.workspace = true
base64.workspace = true
bytes.workspace = true
fxhash.workspace = true
futures-util.workspace = true
futures.workspace = true
h2.workspace = true
Expand All @@ -35,6 +36,7 @@ http-body.workspace = true
http.workspace = true
hyper = { workspace = true, features = ["full"] }
hyper-timeout.workspace = true
matchit.workspace = true
percent-encoding.workspace = true
pin-project.workspace = true
prost.workspace = true
Expand Down
Loading

0 comments on commit ef87308

Please sign in to comment.