Skip to content
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

Allow specifying compression encoding order preference #1727

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion tests/compression/src/compressing_request.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use http_body::Body;
use tonic::codec::CompressionEncoding;
use tonic::codec::{CompressionEncoding, EnabledCompressionEncodings};

util::parametrized_tests! {
client_enabled_server_enabled,
Expand Down Expand Up @@ -230,3 +230,85 @@ async fn client_mark_compressed_without_header_server_enabled(encoding: Compress
"protocol error: received message with compressed-flag but no grpc-encoding was specified"
);
}

#[test]
fn test_compression_priority() {
let mut encodings = EnabledCompressionEncodings::default();
encodings.enable(CompressionEncoding::Gzip);
encodings.enable(CompressionEncoding::Zstd);

assert_eq!(encodings.priority(CompressionEncoding::Gzip), Some(1));
assert_eq!(encodings.priority(CompressionEncoding::Zstd), Some(0));

encodings.enable(CompressionEncoding::Gzip);

assert_eq!(encodings.priority(CompressionEncoding::Gzip), Some(0));
assert_eq!(encodings.priority(CompressionEncoding::Zstd), Some(1));
}

#[allow(dead_code)]
fn build_accept_encoding_header(encodings: &str) -> http::HeaderMap {
let mut headers = http::HeaderMap::new();
headers.insert("grpc-accept-encoding", encodings.parse().unwrap());
headers
}

#[allow(dead_code)]
fn build_and_run_accept_encoding_header_test(
encodings: &str,
enabled_encodings: &[CompressionEncoding],
expected: Option<CompressionEncoding>,
) {
let mut compression_encodings = EnabledCompressionEncodings::default();
for encoding in enabled_encodings {
compression_encodings.enable(*encoding);
}

let compression = CompressionEncoding::from_accept_encoding_header(
&build_accept_encoding_header(encodings),
&compression_encodings,
);
assert_eq!(compression, expected);
}

#[test]
fn test_from_accept_encoding_header() {
build_and_run_accept_encoding_header_test(
"gzip",
&[CompressionEncoding::Gzip],
Some(CompressionEncoding::Gzip),
);

build_and_run_accept_encoding_header_test(
"zstd",
&[CompressionEncoding::Zstd],
Some(CompressionEncoding::Zstd),
);

// Client provides ordering preferring gzip, but we prefer zstd
build_and_run_accept_encoding_header_test(
"gzip,zstd",
&[CompressionEncoding::Zstd, CompressionEncoding::Gzip],
Some(CompressionEncoding::Zstd),
);

// Client provides ordering preferring zstd, but we prefer gzip
build_and_run_accept_encoding_header_test(
"zstd,gzip",
&[CompressionEncoding::Gzip, CompressionEncoding::Zstd],
Some(CompressionEncoding::Gzip),
);

// Client provides ordering preferring gzip, and we also prefer gzip
build_and_run_accept_encoding_header_test(
"gzip,zstd",
&[CompressionEncoding::Gzip, CompressionEncoding::Zstd],
Some(CompressionEncoding::Gzip),
);

// Client provides two, but we don't support any
build_and_run_accept_encoding_header_test("gzip,zstd", &[], None);

// Client provides gzip, but we only support zstd
build_and_run_accept_encoding_header_test("gzip", &[CompressionEncoding::Zstd], None);
}
20 changes: 10 additions & 10 deletions tonic-build/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ pub(crate) fn generate_internal<T: Service>(
let inner = self.inner.clone();
Self {
inner,
accept_compression_encodings: self.accept_compression_encodings,
send_compression_encodings: self.send_compression_encodings,
accept_compression_encodings: self.accept_compression_encodings.clone(),
send_compression_encodings: self.send_compression_encodings.clone(),
max_decoding_message_size: self.max_decoding_message_size,
max_encoding_message_size: self.max_encoding_message_size,
}
Expand Down Expand Up @@ -486,8 +486,8 @@ fn generate_unary<T: Method>(
}
}

let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let accept_compression_encodings = self.accept_compression_encodings.clone();
let send_compression_encodings = self.send_compression_encodings.clone();
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();
Expand Down Expand Up @@ -554,8 +554,8 @@ fn generate_server_streaming<T: Method>(
}
}

let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let accept_compression_encodings = self.accept_compression_encodings.clone();
let send_compression_encodings = self.send_compression_encodings.clone();
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();
Expand Down Expand Up @@ -613,8 +613,8 @@ fn generate_client_streaming<T: Method>(
}
}

let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let accept_compression_encodings = self.accept_compression_encodings.clone();
let send_compression_encodings = self.send_compression_encodings.clone();
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();
Expand Down Expand Up @@ -682,8 +682,8 @@ fn generate_streaming<T: Method>(
}
}

let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let accept_compression_encodings = self.accept_compression_encodings.clone();
let send_compression_encodings = self.send_compression_encodings.clone();
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();
Expand Down
12 changes: 6 additions & 6 deletions tonic-health/src/generated/grpc_health_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ pub mod health_server {
Box::pin(fut)
}
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let accept_compression_encodings = self.accept_compression_encodings.clone();
let send_compression_encodings = self.send_compression_encodings.clone();
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();
Expand Down Expand Up @@ -391,8 +391,8 @@ pub mod health_server {
Box::pin(fut)
}
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let accept_compression_encodings = self.accept_compression_encodings.clone();
let send_compression_encodings = self.send_compression_encodings.clone();
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();
Expand Down Expand Up @@ -434,8 +434,8 @@ pub mod health_server {
let inner = self.inner.clone();
Self {
inner,
accept_compression_encodings: self.accept_compression_encodings,
send_compression_encodings: self.send_compression_encodings,
accept_compression_encodings: self.accept_compression_encodings.clone(),
send_compression_encodings: self.send_compression_encodings.clone(),
max_decoding_message_size: self.max_decoding_message_size,
max_encoding_message_size: self.max_encoding_message_size,
}
Expand Down
8 changes: 4 additions & 4 deletions tonic-reflection/src/generated/grpc_reflection_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ pub mod server_reflection_server {
Box::pin(fut)
}
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let accept_compression_encodings = self.accept_compression_encodings.clone();
let send_compression_encodings = self.send_compression_encodings.clone();
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();
Expand Down Expand Up @@ -442,8 +442,8 @@ pub mod server_reflection_server {
let inner = self.inner.clone();
Self {
inner,
accept_compression_encodings: self.accept_compression_encodings,
send_compression_encodings: self.send_compression_encodings,
accept_compression_encodings: self.accept_compression_encodings.clone(),
send_compression_encodings: self.send_compression_encodings.clone(),
max_decoding_message_size: self.max_decoding_message_size,
max_encoding_message_size: self.max_encoding_message_size,
}
Expand Down
8 changes: 4 additions & 4 deletions tonic-reflection/src/generated/grpc_reflection_v1alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ pub mod server_reflection_server {
Box::pin(fut)
}
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let accept_compression_encodings = self.accept_compression_encodings.clone();
let send_compression_encodings = self.send_compression_encodings.clone();
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();
Expand Down Expand Up @@ -442,8 +442,8 @@ pub mod server_reflection_server {
let inner = self.inner.clone();
Self {
inner,
accept_compression_encodings: self.accept_compression_encodings,
send_compression_encodings: self.send_compression_encodings,
accept_compression_encodings: self.accept_compression_encodings.clone(),
send_compression_encodings: self.send_compression_encodings.clone(),
max_decoding_message_size: self.max_decoding_message_size,
max_encoding_message_size: self.max_encoding_message_size,
}
Expand Down
4 changes: 2 additions & 2 deletions tonic/src/client/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ impl<T> Grpc<T> {
{
let encoding = CompressionEncoding::from_encoding_header(
response.headers(),
self.config.accept_compression_encodings,
&self.config.accept_compression_encodings,
)?;

let status_code = response.status();
Expand Down Expand Up @@ -435,7 +435,7 @@ impl<T: Clone> Clone for Grpc<T> {
config: GrpcConfig {
origin: self.config.origin.clone(),
send_compression_encodings: self.config.send_compression_encodings,
accept_compression_encodings: self.config.accept_compression_encodings,
accept_compression_encodings: self.config.accept_compression_encodings.clone(),
max_encoding_message_size: self.config.max_encoding_message_size,
max_decoding_message_size: self.config.max_decoding_message_size,
},
Expand Down
Loading
Loading