Skip to content

Commit

Permalink
feat(codec): Configure max request message size (#1274)
Browse files Browse the repository at this point in the history
* feat(codec): add max_message_size parameter

resolves #1097

* refactor(client): add max size parameters

* refactor(tonic-build): update server gen template

* refactor(tonic-build): update client template

* fix(tonic-build): update client template

* fix(tonic-build): small typo in server.rs

* fix(tonic-build): client.rs generator

* fix(tonic): add apply max message setting size to server

* fix(test): wrong message size

* fix: doctest + generated rs
  • Loading branch information
aoudiamoncef authored Feb 17, 2023
1 parent a52de8a commit 9f716d8
Show file tree
Hide file tree
Showing 11 changed files with 482 additions and 23 deletions.
14 changes: 14 additions & 0 deletions tonic-build/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ pub(crate) fn generate_internal<T: Service>(
self
}

/// Limits the maximum size of a decoded message.
#[must_use]
pub fn max_decoding_message_size(mut self, limit: usize) -> Self {
self.inner = self.inner.max_decoding_message_size(limit);
self
}

/// Limits the maximum size of an encoded message.
#[must_use]
pub fn max_encoding_message_size(mut self, limit: usize) -> Self {
self.inner = self.inner.max_encoding_message_size(limit);
self
}

#methods
}
}
Expand Down
44 changes: 40 additions & 4 deletions tonic-build/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ pub(crate) fn generate_internal<T: Service>(
}
};

let configure_max_message_size_methods = quote! {
/// Limits the maximum size of a decoded message.
#[must_use]
pub fn max_decoding_message_size(mut self, limit: usize) -> Self {
self.max_decoding_message_size = Some(limit);
self
}

/// Limits the maximum size of an encoded message.
#[must_use]
pub fn max_encoding_message_size(mut self, limit: usize) -> Self {
self.max_encoding_message_size = Some(limit);
self
}
};

quote! {
/// Generated server implementations.
#(#mod_attributes)*
Expand All @@ -106,6 +122,8 @@ pub(crate) fn generate_internal<T: Service>(
inner: _Inner<T>,
accept_compression_encodings: EnabledCompressionEncodings,
send_compression_encodings: EnabledCompressionEncodings,
max_decoding_message_size: Option<usize>,
max_encoding_message_size: Option<usize>,
}

struct _Inner<T>(Arc<T>);
Expand All @@ -121,6 +139,8 @@ pub(crate) fn generate_internal<T: Service>(
inner,
accept_compression_encodings: Default::default(),
send_compression_encodings: Default::default(),
max_decoding_message_size: None,
max_encoding_message_size: None,
}
}

Expand All @@ -132,6 +152,8 @@ pub(crate) fn generate_internal<T: Service>(
}

#configure_compression_methods

#configure_max_message_size_methods
}

impl<T, B> tonic::codegen::Service<http::Request<B>> for #server_service<T>
Expand Down Expand Up @@ -173,6 +195,8 @@ pub(crate) fn generate_internal<T: Service>(
inner,
accept_compression_encodings: self.accept_compression_encodings,
send_compression_encodings: self.send_compression_encodings,
max_decoding_message_size: self.max_decoding_message_size,
max_encoding_message_size: self.max_encoding_message_size,
}
}
}
Expand Down Expand Up @@ -414,14 +438,17 @@ fn generate_unary<T: Method>(

let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let max_decoding_message_size = self.max_decoding_message_size;
let max_encoding_message_size = self.max_encoding_message_size;
let inner = self.inner.clone();
let fut = async move {
let inner = inner.0;
let method = #service_ident(inner);
let codec = #codec_name::default();

let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(accept_compression_encodings, send_compression_encodings);
.apply_compression_config(accept_compression_encodings, send_compression_encodings)
.apply_max_message_size_config(max_decoding_message_size, max_encoding_message_size);

let res = grpc.unary(method, req).await;
Ok(res)
Expand Down Expand Up @@ -466,14 +493,17 @@ fn generate_server_streaming<T: Method>(

let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let max_decoding_message_size = self.max_decoding_message_size;
let max_encoding_message_size = self.max_encoding_message_size;
let inner = self.inner.clone();
let fut = async move {
let inner = inner.0;
let method = #service_ident(inner);
let codec = #codec_name::default();

let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(accept_compression_encodings, send_compression_encodings);
.apply_compression_config(accept_compression_encodings, send_compression_encodings)
.apply_max_message_size_config(max_decoding_message_size, max_encoding_message_size);

let res = grpc.server_streaming(method, req).await;
Ok(res)
Expand Down Expand Up @@ -516,14 +546,17 @@ fn generate_client_streaming<T: Method>(

let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let max_decoding_message_size = self.max_decoding_message_size;
let max_encoding_message_size = self.max_encoding_message_size;
let inner = self.inner.clone();
let fut = async move {
let inner = inner.0;
let method = #service_ident(inner);
let codec = #codec_name::default();

let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(accept_compression_encodings, send_compression_encodings);
.apply_compression_config(accept_compression_encodings, send_compression_encodings)
.apply_max_message_size_config(max_decoding_message_size, max_encoding_message_size);

let res = grpc.client_streaming(method, req).await;
Ok(res)
Expand Down Expand Up @@ -569,14 +602,17 @@ fn generate_streaming<T: Method>(

let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let max_decoding_message_size = self.max_decoding_message_size;
let max_encoding_message_size = self.max_encoding_message_size;
let inner = self.inner.clone();
let fut = async move {
let inner = inner.0;
let method = #service_ident(inner);
let codec = #codec_name::default();

let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(accept_compression_encodings, send_compression_encodings);
.apply_compression_config(accept_compression_encodings, send_compression_encodings)
.apply_max_message_size_config(max_decoding_message_size, max_encoding_message_size);

let res = grpc.streaming(method, req).await;
Ok(res)
Expand Down
42 changes: 42 additions & 0 deletions tonic-health/src/generated/grpc.health.v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ pub mod health_client {
self.inner = self.inner.accept_compressed(encoding);
self
}
/// Limits the maximum size of a decoded message.
#[must_use]
pub fn max_decoding_message_size(mut self, limit: usize) -> Self {
self.inner = self.inner.max_decoding_message_size(limit);
self
}
/// Limits the maximum size of an encoded message.
#[must_use]
pub fn max_encoding_message_size(mut self, limit: usize) -> Self {
self.inner = self.inner.max_encoding_message_size(limit);
self
}
/// If the requested service is unknown, the call will fail with status
/// NOT_FOUND.
pub async fn check(
Expand Down Expand Up @@ -224,6 +236,8 @@ pub mod health_server {
inner: _Inner<T>,
accept_compression_encodings: EnabledCompressionEncodings,
send_compression_encodings: EnabledCompressionEncodings,
max_decoding_message_size: Option<usize>,
max_encoding_message_size: Option<usize>,
}
struct _Inner<T>(Arc<T>);
impl<T: Health> HealthServer<T> {
Expand All @@ -236,6 +250,8 @@ pub mod health_server {
inner,
accept_compression_encodings: Default::default(),
send_compression_encodings: Default::default(),
max_decoding_message_size: None,
max_encoding_message_size: None,
}
}
pub fn with_interceptor<F>(
Expand All @@ -259,6 +275,18 @@ pub mod health_server {
self.send_compression_encodings.enable(encoding);
self
}
/// Limits the maximum size of a decoded message.
#[must_use]
pub fn max_decoding_message_size(mut self, limit: usize) -> Self {
self.max_decoding_message_size = Some(limit);
self
}
/// Limits the maximum size of an encoded message.
#[must_use]
pub fn max_encoding_message_size(mut self, limit: usize) -> Self {
self.max_encoding_message_size = Some(limit);
self
}
}
impl<T, B> tonic::codegen::Service<http::Request<B>> for HealthServer<T>
where
Expand Down Expand Up @@ -301,6 +329,8 @@ pub mod health_server {
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let max_decoding_message_size = self.max_decoding_message_size;
let max_encoding_message_size = self.max_encoding_message_size;
let inner = self.inner.clone();
let fut = async move {
let inner = inner.0;
Expand All @@ -310,6 +340,10 @@ pub mod health_server {
.apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
)
.apply_max_message_size_config(
max_decoding_message_size,
max_encoding_message_size,
);
let res = grpc.unary(method, req).await;
Ok(res)
Expand Down Expand Up @@ -340,6 +374,8 @@ pub mod health_server {
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let max_decoding_message_size = self.max_decoding_message_size;
let max_encoding_message_size = self.max_encoding_message_size;
let inner = self.inner.clone();
let fut = async move {
let inner = inner.0;
Expand All @@ -349,6 +385,10 @@ pub mod health_server {
.apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
)
.apply_max_message_size_config(
max_decoding_message_size,
max_encoding_message_size,
);
let res = grpc.server_streaming(method, req).await;
Ok(res)
Expand Down Expand Up @@ -377,6 +417,8 @@ pub mod health_server {
inner,
accept_compression_encodings: self.accept_compression_encodings,
send_compression_encodings: self.send_compression_encodings,
max_decoding_message_size: self.max_decoding_message_size,
max_encoding_message_size: self.max_encoding_message_size,
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions tonic-reflection/src/generated/grpc.reflection.v1alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ pub mod server_reflection_client {
self.inner = self.inner.accept_compressed(encoding);
self
}
/// Limits the maximum size of a decoded message.
#[must_use]
pub fn max_decoding_message_size(mut self, limit: usize) -> Self {
self.inner = self.inner.max_decoding_message_size(limit);
self
}
/// Limits the maximum size of an encoded message.
#[must_use]
pub fn max_encoding_message_size(mut self, limit: usize) -> Self {
self.inner = self.inner.max_encoding_message_size(limit);
self
}
/// The reflection service is structured as a bidirectional stream, ensuring
/// all related requests go to a single server.
pub async fn server_reflection_info(
Expand Down Expand Up @@ -270,6 +282,8 @@ pub mod server_reflection_server {
inner: _Inner<T>,
accept_compression_encodings: EnabledCompressionEncodings,
send_compression_encodings: EnabledCompressionEncodings,
max_decoding_message_size: Option<usize>,
max_encoding_message_size: Option<usize>,
}
struct _Inner<T>(Arc<T>);
impl<T: ServerReflection> ServerReflectionServer<T> {
Expand All @@ -282,6 +296,8 @@ pub mod server_reflection_server {
inner,
accept_compression_encodings: Default::default(),
send_compression_encodings: Default::default(),
max_decoding_message_size: None,
max_encoding_message_size: None,
}
}
pub fn with_interceptor<F>(
Expand All @@ -305,6 +321,18 @@ pub mod server_reflection_server {
self.send_compression_encodings.enable(encoding);
self
}
/// Limits the maximum size of a decoded message.
#[must_use]
pub fn max_decoding_message_size(mut self, limit: usize) -> Self {
self.max_decoding_message_size = Some(limit);
self
}
/// Limits the maximum size of an encoded message.
#[must_use]
pub fn max_encoding_message_size(mut self, limit: usize) -> Self {
self.max_encoding_message_size = Some(limit);
self
}
}
impl<T, B> tonic::codegen::Service<http::Request<B>> for ServerReflectionServer<T>
where
Expand Down Expand Up @@ -352,6 +380,8 @@ pub mod server_reflection_server {
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let max_decoding_message_size = self.max_decoding_message_size;
let max_encoding_message_size = self.max_encoding_message_size;
let inner = self.inner.clone();
let fut = async move {
let inner = inner.0;
Expand All @@ -361,6 +391,10 @@ pub mod server_reflection_server {
.apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
)
.apply_max_message_size_config(
max_decoding_message_size,
max_encoding_message_size,
);
let res = grpc.streaming(method, req).await;
Ok(res)
Expand Down Expand Up @@ -389,6 +423,8 @@ pub mod server_reflection_server {
inner,
accept_compression_encodings: self.accept_compression_encodings,
send_compression_encodings: self.send_compression_encodings,
max_decoding_message_size: self.max_decoding_message_size,
max_encoding_message_size: self.max_encoding_message_size,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tonic/benches/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ macro_rules! bench {
b.iter(|| {
rt.block_on(async {
let decoder = MockDecoder::new($message_size);
let mut stream = Streaming::new_request(decoder, body.clone(), None);
let mut stream = Streaming::new_request(decoder, body.clone(), None, None);

let mut count = 0;
while let Some(msg) = stream.message().await.unwrap() {
Expand Down
Loading

0 comments on commit 9f716d8

Please sign in to comment.