-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add use_generic_streaming_requests
option to make testing server impls easier
#2115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9a56fc6
f9ccc27
0585ada
8475ab9
517c941
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
authors = ["Yotam Ofek <[email protected]>"] | ||
edition = "2021" | ||
license = "MIT" | ||
name = "use_generic_streaming_requests" | ||
|
||
[dependencies] | ||
tokio-stream = "0.1" | ||
prost = "0.13" | ||
tonic = {path = "../../tonic"} | ||
tokio = {version = "1.0", features = ["macros"]} | ||
|
||
[build-dependencies] | ||
tonic-build = {path = "../../tonic-build" } | ||
|
||
[package.metadata.cargo-machete] | ||
ignored = ["prost"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2020 Lucio Franco | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
fn main() { | ||
tonic_build::configure() | ||
.use_generic_streaming_requests(true) | ||
.compile_protos(&["proto/test.proto"], &["proto"]) | ||
.unwrap(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
syntax = "proto3"; | ||
|
||
package test; | ||
|
||
service Test { | ||
rpc TestRequest(stream Message) returns (Message); | ||
} | ||
|
||
message Message {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use tokio_stream::StreamExt; | ||
use tonic::{Response, Status}; | ||
|
||
tonic::include_proto!("test"); | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Svc; | ||
|
||
#[tonic::async_trait] | ||
impl test_server::Test for Svc { | ||
async fn test_request( | ||
&self, | ||
req: tonic::Request< | ||
impl tokio_stream::Stream<Item = Result<Message, Status>> + Send + Unpin, | ||
>, | ||
) -> Result<Response<Message>, Status> { | ||
let mut req = req.into_inner(); | ||
while let Some(message) = req.try_next().await? { | ||
println!("Got message: {message:?}") | ||
} | ||
|
||
Ok(Response::new(Message {})) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use tonic::Request; | ||
|
||
use super::test_server::Test; | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_request_handler() { | ||
let incoming_messages = tokio_stream::iter([Message {}, Message {}].map(Ok)); | ||
let svc = Svc; | ||
svc.test_request(Request::new(incoming_messages)) | ||
.await | ||
.unwrap(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ pub(crate) fn generate_internal<T: Service>( | |
disable_comments: &HashSet<String>, | ||
use_arc_self: bool, | ||
generate_default_stubs: bool, | ||
use_generic_streaming_requests: bool, | ||
) -> TokenStream { | ||
let methods = generate_methods( | ||
service, | ||
|
@@ -41,6 +42,7 @@ pub(crate) fn generate_internal<T: Service>( | |
disable_comments, | ||
use_arc_self, | ||
generate_default_stubs, | ||
use_generic_streaming_requests, | ||
); | ||
let package = if emit_package { service.package() } else { "" }; | ||
// Transport based implementations | ||
|
@@ -203,6 +205,7 @@ fn generate_trait<T: Service>( | |
disable_comments: &HashSet<String>, | ||
use_arc_self: bool, | ||
generate_default_stubs: bool, | ||
use_generic_streaming_requests: bool, | ||
) -> TokenStream { | ||
let methods = generate_trait_methods( | ||
service, | ||
|
@@ -212,6 +215,7 @@ fn generate_trait<T: Service>( | |
disable_comments, | ||
use_arc_self, | ||
generate_default_stubs, | ||
use_generic_streaming_requests, | ||
); | ||
let trait_doc = generate_doc_comment(format!( | ||
" Generated trait containing gRPC methods that should be implemented for use with {}Server.", | ||
|
@@ -227,6 +231,7 @@ fn generate_trait<T: Service>( | |
} | ||
} | ||
|
||
#[allow(clippy::too_many_arguments)] | ||
fn generate_trait_methods<T: Service>( | ||
service: &T, | ||
emit_package: bool, | ||
|
@@ -235,6 +240,7 @@ fn generate_trait_methods<T: Service>( | |
disable_comments: &HashSet<String>, | ||
use_arc_self: bool, | ||
generate_default_stubs: bool, | ||
use_generic_streaming_requests: bool, | ||
) -> TokenStream { | ||
let mut stream = TokenStream::new(); | ||
|
||
|
@@ -257,92 +263,61 @@ fn generate_trait_methods<T: Service>( | |
quote!(&self) | ||
}; | ||
|
||
let method = match ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if the benefits of adding this feature are worth the increased complexity that comes with adding more features. However, the refactoring of this part that was done to add this feature seems to improve the readability and maintainability of the code, so if it be separated into a separate pull request, I think we can proceed with the discussion separately. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, the refactoring is in a separate commit so I'll put it in its own PR. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
method.client_streaming(), | ||
method.server_streaming(), | ||
generate_default_stubs, | ||
) { | ||
(false, false, true) => { | ||
quote! { | ||
#method_doc | ||
async fn #name(#self_param, request: tonic::Request<#req_message>) | ||
-> std::result::Result<tonic::Response<#res_message>, tonic::Status> { | ||
Err(tonic::Status::unimplemented("Not yet implemented")) | ||
} | ||
} | ||
} | ||
(false, false, false) => { | ||
quote! { | ||
#method_doc | ||
async fn #name(#self_param, request: tonic::Request<#req_message>) | ||
-> std::result::Result<tonic::Response<#res_message>, tonic::Status>; | ||
} | ||
} | ||
(true, false, true) => { | ||
quote! { | ||
#method_doc | ||
async fn #name(#self_param, request: tonic::Request<tonic::Streaming<#req_message>>) | ||
-> std::result::Result<tonic::Response<#res_message>, tonic::Status> { | ||
Err(tonic::Status::unimplemented("Not yet implemented")) | ||
} | ||
} | ||
} | ||
(true, false, false) => { | ||
quote! { | ||
#method_doc | ||
async fn #name(#self_param, request: tonic::Request<tonic::Streaming<#req_message>>) | ||
-> std::result::Result<tonic::Response<#res_message>, tonic::Status>; | ||
} | ||
} | ||
(false, true, true) => { | ||
quote! { | ||
#method_doc | ||
async fn #name(#self_param, request: tonic::Request<#req_message>) | ||
-> std::result::Result<tonic::Response<BoxStream<#res_message>>, tonic::Status> { | ||
Err(tonic::Status::unimplemented("Not yet implemented")) | ||
} | ||
let result = |ok| quote!(std::result::Result<#ok, tonic::Status>); | ||
let response_result = |message| result(quote!(tonic::Response<#message>)); | ||
|
||
let req_param_type = { | ||
let inner_ty = if !method.client_streaming() { | ||
req_message | ||
} else if !use_generic_streaming_requests { | ||
quote!(tonic::Streaming<#req_message>) | ||
} else { | ||
let message_ty = result(req_message); | ||
quote!(impl tokio_stream::Stream<Item = #message_ty> + std::marker::Send + std::marker::Unpin) | ||
}; | ||
|
||
quote!(tonic::Request<#inner_ty>) | ||
}; | ||
|
||
let partial_sig = quote! { | ||
#method_doc | ||
async fn #name(#self_param, request: #req_param_type) | ||
}; | ||
|
||
let body_or_semicolon = if generate_default_stubs { | ||
quote! { | ||
{ | ||
Err(tonic::Status::unimplemented("Not yet implemented")) | ||
} | ||
} | ||
(false, true, false) => { | ||
let stream = quote::format_ident!("{}Stream", method.identifier()); | ||
let stream_doc = generate_doc_comment(format!( | ||
" Server streaming response type for the {} method.", | ||
method.identifier() | ||
)); | ||
|
||
quote! { | ||
#stream_doc | ||
type #stream: tonic::codegen::tokio_stream::Stream<Item = std::result::Result<#res_message, tonic::Status>> + std::marker::Send + 'static; | ||
|
||
#method_doc | ||
async fn #name(#self_param, request: tonic::Request<#req_message>) | ||
-> std::result::Result<tonic::Response<Self::#stream>, tonic::Status>; | ||
} | ||
} else { | ||
quote!(;) | ||
}; | ||
|
||
let method = if !method.server_streaming() { | ||
let return_ty = response_result(res_message); | ||
quote! { | ||
#partial_sig -> #return_ty #body_or_semicolon | ||
} | ||
(true, true, true) => { | ||
quote! { | ||
#method_doc | ||
async fn #name(#self_param, request: tonic::Request<tonic::Streaming<#req_message>>) | ||
-> std::result::Result<tonic::Response<BoxStream<#res_message>>, tonic::Status> { | ||
Err(tonic::Status::unimplemented("Not yet implemented")) | ||
} | ||
} | ||
} else if generate_default_stubs { | ||
let return_ty = response_result(quote!(BoxStream<#res_message>)); | ||
quote! { | ||
#partial_sig -> #return_ty #body_or_semicolon | ||
} | ||
(true, true, false) => { | ||
let stream = quote::format_ident!("{}Stream", method.identifier()); | ||
let stream_doc = generate_doc_comment(format!( | ||
" Server streaming response type for the {} method.", | ||
method.identifier() | ||
)); | ||
|
||
quote! { | ||
#stream_doc | ||
type #stream: tonic::codegen::tokio_stream::Stream<Item = std::result::Result<#res_message, tonic::Status>> + std::marker::Send + 'static; | ||
|
||
#method_doc | ||
async fn #name(#self_param, request: tonic::Request<tonic::Streaming<#req_message>>) | ||
-> std::result::Result<tonic::Response<Self::#stream>, tonic::Status>; | ||
} | ||
} else { | ||
let stream = quote::format_ident!("{}Stream", method.identifier()); | ||
let stream_doc = generate_doc_comment(format!( | ||
" Server streaming response type for the {} method.", | ||
method.identifier() | ||
)); | ||
let stream_item_ty = result(res_message); | ||
let stream_ty = quote!(tonic::codegen::tokio_stream::Stream<Item = #stream_item_ty> + std::marker::Send + 'static); | ||
let return_ty = response_result(quote!(Self::#stream)); | ||
quote! { | ||
#stream_doc | ||
type #stream: #stream_ty; | ||
|
||
#partial_sig -> #return_ty #body_or_semicolon | ||
} | ||
}; | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.