Skip to content

Commit

Permalink
Add deflate compression support (#2117)
Browse files Browse the repository at this point in the history
* Add deflate compression support

* Update test name

---------

Co-authored-by: tottoto <[email protected]>
  • Loading branch information
a1ien and tottoto authored Jan 7, 2025
1 parent e508815 commit 79a06cc
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 20 deletions.
2 changes: 1 addition & 1 deletion tests/compression/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pin-project = "1.0"
prost = "0.13"
tokio = {version = "1.0", features = ["macros", "rt-multi-thread", "net"]}
tokio-stream = "0.1"
tonic = {path = "../../tonic", features = ["gzip", "zstd"]}
tonic = {path = "../../tonic", features = ["gzip", "deflate", "zstd"]}
tower = "0.5"
tower-http = {version = "0.6", features = ["map-response-body", "map-request-body"]}

Expand Down
3 changes: 3 additions & 0 deletions tests/compression/src/bidirectional_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ util::parametrized_tests! {
client_enabled_server_enabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -34,6 +35,7 @@ async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
let expected = match self.encoding {
CompressionEncoding::Gzip => "gzip",
CompressionEncoding::Zstd => "zstd",
CompressionEncoding::Deflate => "deflate",
_ => panic!("unexpected encoding {:?}", self.encoding),
};
assert_eq!(req.headers().get("grpc-encoding").unwrap(), expected);
Expand Down Expand Up @@ -86,6 +88,7 @@ async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
let expected = match encoding {
CompressionEncoding::Gzip => "gzip",
CompressionEncoding::Zstd => "zstd",
CompressionEncoding::Deflate => "deflate",
_ => panic!("unexpected encoding {:?}", encoding),
};
assert_eq!(res.metadata().get("grpc-encoding").unwrap(), expected);
Expand Down
7 changes: 7 additions & 0 deletions tests/compression/src/client_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ util::parametrized_tests! {
client_enabled_server_enabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand All @@ -31,6 +32,7 @@ async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
let expected = match self.encoding {
CompressionEncoding::Gzip => "gzip",
CompressionEncoding::Zstd => "zstd",
CompressionEncoding::Deflate => "deflate",
_ => panic!("unexpected encoding {:?}", self.encoding),
};
assert_eq!(req.headers().get("grpc-encoding").unwrap(), expected);
Expand Down Expand Up @@ -77,6 +79,7 @@ util::parametrized_tests! {
client_disabled_server_enabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -127,6 +130,7 @@ util::parametrized_tests! {
client_enabled_server_disabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -156,6 +160,7 @@ async fn client_enabled_server_disabled(encoding: CompressionEncoding) {
let expected = match encoding {
CompressionEncoding::Gzip => "gzip",
CompressionEncoding::Zstd => "zstd",
CompressionEncoding::Deflate => "deflate",
_ => panic!("unexpected encoding {:?}", encoding),
};
assert_eq!(
Expand All @@ -171,6 +176,7 @@ util::parametrized_tests! {
compressing_response_from_client_stream,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -211,6 +217,7 @@ async fn compressing_response_from_client_stream(encoding: CompressionEncoding)
let expected = match encoding {
CompressionEncoding::Gzip => "gzip",
CompressionEncoding::Zstd => "zstd",
CompressionEncoding::Deflate => "deflate",
_ => panic!("unexpected encoding {:?}", encoding),
};
assert_eq!(res.metadata().get("grpc-encoding").unwrap(), expected);
Expand Down
11 changes: 9 additions & 2 deletions tests/compression/src/compressing_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ util::parametrized_tests! {
client_enabled_server_enabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand All @@ -31,6 +32,7 @@ async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
let expected = match self.encoding {
CompressionEncoding::Gzip => "gzip",
CompressionEncoding::Zstd => "zstd",
CompressionEncoding::Deflate => "deflate",
_ => panic!("unexpected encoding {:?}", self.encoding),
};
assert_eq!(req.headers().get("grpc-encoding").unwrap(), expected);
Expand Down Expand Up @@ -81,6 +83,7 @@ util::parametrized_tests! {
client_enabled_server_enabled_multi_encoding,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand All @@ -89,12 +92,13 @@ async fn client_enabled_server_enabled_multi_encoding(encoding: CompressionEncod

let svc = test_server::TestServer::new(Svc::default())
.accept_compressed(CompressionEncoding::Gzip)
.accept_compressed(CompressionEncoding::Zstd);
.accept_compressed(CompressionEncoding::Zstd)
.accept_compressed(CompressionEncoding::Deflate);

let request_bytes_counter = Arc::new(AtomicUsize::new(0));

fn assert_right_encoding<B>(req: http::Request<B>) -> http::Request<B> {
let supported_encodings = ["gzip", "zstd"];
let supported_encodings = ["gzip", "zstd", "deflate"];
let req_encoding = req.headers().get("grpc-encoding").unwrap();
assert!(supported_encodings.iter().any(|e| e == req_encoding));

Expand Down Expand Up @@ -141,6 +145,7 @@ parametrized_tests! {
client_enabled_server_disabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -171,6 +176,7 @@ async fn client_enabled_server_disabled(encoding: CompressionEncoding) {
let expected = match encoding {
CompressionEncoding::Gzip => "gzip",
CompressionEncoding::Zstd => "zstd",
CompressionEncoding::Deflate => "deflate",
_ => panic!("unexpected encoding {:?}", encoding),
};
assert_eq!(
Expand All @@ -190,6 +196,7 @@ parametrized_tests! {
client_mark_compressed_without_header_server_enabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand Down
15 changes: 14 additions & 1 deletion tests/compression/src/compressing_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ util::parametrized_tests! {
client_enabled_server_enabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -36,6 +37,7 @@ async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
let expected = match self.encoding {
CompressionEncoding::Gzip => "gzip",
CompressionEncoding::Zstd => "zstd",
CompressionEncoding::Deflate => "deflate",
_ => panic!("unexpected encoding {:?}", self.encoding),
};
assert_eq!(
Expand Down Expand Up @@ -85,6 +87,7 @@ async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
let expected = match encoding {
CompressionEncoding::Gzip => "gzip",
CompressionEncoding::Zstd => "zstd",
CompressionEncoding::Deflate => "deflate",
_ => panic!("unexpected encoding {:?}", encoding),
};

Expand All @@ -100,6 +103,7 @@ util::parametrized_tests! {
client_enabled_server_disabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -175,7 +179,8 @@ async fn client_enabled_server_disabled_multi_encoding() {

let mut client = test_client::TestClient::new(mock_io_channel(client).await)
.accept_compressed(CompressionEncoding::Gzip)
.accept_compressed(CompressionEncoding::Zstd);
.accept_compressed(CompressionEncoding::Zstd)
.accept_compressed(CompressionEncoding::Deflate);

let res = client.compress_output_unary(()).await.unwrap();

Expand All @@ -189,6 +194,7 @@ util::parametrized_tests! {
client_disabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -259,6 +265,7 @@ util::parametrized_tests! {
server_replying_with_unsupported_encoding,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -302,6 +309,7 @@ util::parametrized_tests! {
disabling_compression_on_single_response,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -344,6 +352,7 @@ async fn disabling_compression_on_single_response(encoding: CompressionEncoding)
let expected = match encoding {
CompressionEncoding::Gzip => "gzip",
CompressionEncoding::Zstd => "zstd",
CompressionEncoding::Deflate => "deflate",
_ => panic!("unexpected encoding {:?}", encoding),
};
assert_eq!(res.metadata().get("grpc-encoding").unwrap(), expected);
Expand All @@ -356,6 +365,7 @@ util::parametrized_tests! {
disabling_compression_on_response_but_keeping_compression_on_stream,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -400,6 +410,7 @@ async fn disabling_compression_on_response_but_keeping_compression_on_stream(
let expected = match encoding {
CompressionEncoding::Gzip => "gzip",
CompressionEncoding::Zstd => "zstd",
CompressionEncoding::Deflate => "deflate",
_ => panic!("unexpected encoding {:?}", encoding),
};
assert_eq!(res.metadata().get("grpc-encoding").unwrap(), expected);
Expand All @@ -425,6 +436,7 @@ util::parametrized_tests! {
disabling_compression_on_response_from_client_stream,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -469,6 +481,7 @@ async fn disabling_compression_on_response_from_client_stream(encoding: Compress
let expected = match encoding {
CompressionEncoding::Gzip => "gzip",
CompressionEncoding::Zstd => "zstd",
CompressionEncoding::Deflate => "deflate",
_ => panic!("unexpected encoding {:?}", encoding),
};
assert_eq!(res.metadata().get("grpc-encoding").unwrap(), expected);
Expand Down
3 changes: 3 additions & 0 deletions tests/compression/src/server_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ util::parametrized_tests! {
client_enabled_server_enabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand Down Expand Up @@ -45,6 +46,7 @@ async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
let expected = match encoding {
CompressionEncoding::Gzip => "gzip",
CompressionEncoding::Zstd => "zstd",
CompressionEncoding::Deflate => "deflate",
_ => panic!("unexpected encoding {:?}", encoding),
};
assert_eq!(res.metadata().get("grpc-encoding").unwrap(), expected);
Expand Down Expand Up @@ -121,6 +123,7 @@ util::parametrized_tests! {
client_enabled_server_disabled,
zstd: CompressionEncoding::Zstd,
gzip: CompressionEncoding::Gzip,
deflate: CompressionEncoding::Deflate,
}

#[allow(dead_code)]
Expand Down
1 change: 1 addition & 0 deletions tests/compression/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ impl AssertRightEncoding {
let expected = match self.encoding {
CompressionEncoding::Gzip => "gzip",
CompressionEncoding::Zstd => "zstd",
CompressionEncoding::Deflate => "deflate",
_ => panic!("unexpected encoding {:?}", self.encoding),
};
assert_eq!(req.headers().get("grpc-encoding").unwrap(), expected);
Expand Down
1 change: 1 addition & 0 deletions tonic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ version = "0.13.0"
[features]
codegen = ["dep:async-trait"]
gzip = ["dep:flate2"]
deflate = ["dep:flate2"]
zstd = ["dep:zstd"]
default = ["transport", "codegen", "prost"]
prost = ["dep:prost"]
Expand Down
2 changes: 1 addition & 1 deletion tonic/src/client/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ impl GrpcConfig {
.headers_mut()
.insert(CONTENT_TYPE, GRPC_CONTENT_TYPE);

#[cfg(any(feature = "gzip", feature = "zstd"))]
#[cfg(any(feature = "gzip", feature = "deflate", feature = "zstd"))]
if let Some(encoding) = self.send_compression_encodings {
request.headers_mut().insert(
crate::codec::compression::ENCODING_HEADER,
Expand Down
Loading

0 comments on commit 79a06cc

Please sign in to comment.