Skip to content

Commit 40f6e44

Browse files
committed
feat(codec): Add lz4 support
Add support of lz4 compression with lz4_flex
1 parent cde96ac commit 40f6e44

File tree

10 files changed

+125
-16
lines changed

10 files changed

+125
-16
lines changed

tests/compression/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pin-project = "1.0"
1515
prost = "0.14"
1616
tokio = {version = "1.0", features = ["macros", "rt-multi-thread", "net"]}
1717
tokio-stream = "0.1"
18-
tonic = {path = "../../tonic", features = ["gzip", "deflate", "zstd"]}
18+
tonic = {path = "../../tonic", features = ["gzip", "deflate", "zstd", "lz4"]}
1919
tower = "0.5"
2020
tower-http = {version = "0.6", features = ["map-response-body", "map-request-body"]}
2121

tests/compression/src/bidirectional_stream.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ util::parametrized_tests! {
77
zstd: CompressionEncoding::Zstd,
88
gzip: CompressionEncoding::Gzip,
99
deflate: CompressionEncoding::Deflate,
10+
lz4: CompressionEncoding::Lz4,
1011
}
1112

1213
#[allow(dead_code)]
@@ -36,6 +37,7 @@ async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
3637
CompressionEncoding::Gzip => "gzip",
3738
CompressionEncoding::Zstd => "zstd",
3839
CompressionEncoding::Deflate => "deflate",
40+
CompressionEncoding::Lz4 => "lz4",
3941
_ => panic!("unexpected encoding {:?}", self.encoding),
4042
};
4143
assert_eq!(req.headers().get("grpc-encoding").unwrap(), expected);
@@ -89,6 +91,7 @@ async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
8991
CompressionEncoding::Gzip => "gzip",
9092
CompressionEncoding::Zstd => "zstd",
9193
CompressionEncoding::Deflate => "deflate",
94+
CompressionEncoding::Lz4 => "lz4",
9295
_ => panic!("unexpected encoding {encoding:?}"),
9396
};
9497
assert_eq!(res.metadata().get("grpc-encoding").unwrap(), expected);

tests/compression/src/client_stream.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ util::parametrized_tests! {
77
zstd: CompressionEncoding::Zstd,
88
gzip: CompressionEncoding::Gzip,
99
deflate: CompressionEncoding::Deflate,
10+
lz4: CompressionEncoding::Lz4,
1011
}
1112

1213
#[allow(dead_code)]
@@ -33,6 +34,7 @@ async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
3334
CompressionEncoding::Gzip => "gzip",
3435
CompressionEncoding::Zstd => "zstd",
3536
CompressionEncoding::Deflate => "deflate",
37+
CompressionEncoding::Lz4 => "lz4",
3638
_ => panic!("unexpected encoding {:?}", self.encoding),
3739
};
3840
assert_eq!(req.headers().get("grpc-encoding").unwrap(), expected);
@@ -80,6 +82,7 @@ util::parametrized_tests! {
8082
zstd: CompressionEncoding::Zstd,
8183
gzip: CompressionEncoding::Gzip,
8284
deflate: CompressionEncoding::Deflate,
85+
lz4: CompressionEncoding::Lz4,
8386
}
8487

8588
#[allow(dead_code)]
@@ -131,6 +134,7 @@ util::parametrized_tests! {
131134
zstd: CompressionEncoding::Zstd,
132135
gzip: CompressionEncoding::Gzip,
133136
deflate: CompressionEncoding::Deflate,
137+
lz4: CompressionEncoding::Lz4,
134138
}
135139

136140
#[allow(dead_code)]
@@ -161,6 +165,7 @@ async fn client_enabled_server_disabled(encoding: CompressionEncoding) {
161165
CompressionEncoding::Gzip => "gzip",
162166
CompressionEncoding::Zstd => "zstd",
163167
CompressionEncoding::Deflate => "deflate",
168+
CompressionEncoding::Lz4 => "lz4",
164169
_ => panic!("unexpected encoding {encoding:?}"),
165170
};
166171
assert_eq!(
@@ -174,6 +179,7 @@ util::parametrized_tests! {
174179
zstd: CompressionEncoding::Zstd,
175180
gzip: CompressionEncoding::Gzip,
176181
deflate: CompressionEncoding::Deflate,
182+
lz4: CompressionEncoding::Lz4,
177183
}
178184

179185
#[allow(dead_code)]
@@ -215,6 +221,7 @@ async fn compressing_response_from_client_stream(encoding: CompressionEncoding)
215221
CompressionEncoding::Gzip => "gzip",
216222
CompressionEncoding::Zstd => "zstd",
217223
CompressionEncoding::Deflate => "deflate",
224+
CompressionEncoding::Lz4 => "lz4",
218225
_ => panic!("unexpected encoding {encoding:?}"),
219226
};
220227
assert_eq!(res.metadata().get("grpc-encoding").unwrap(), expected);

tests/compression/src/compressing_request.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ util::parametrized_tests! {
77
zstd: CompressionEncoding::Zstd,
88
gzip: CompressionEncoding::Gzip,
99
deflate: CompressionEncoding::Deflate,
10+
lz4: CompressionEncoding::Lz4,
1011
}
1112

1213
#[allow(dead_code)]
@@ -33,6 +34,7 @@ async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
3334
CompressionEncoding::Gzip => "gzip",
3435
CompressionEncoding::Zstd => "zstd",
3536
CompressionEncoding::Deflate => "deflate",
37+
CompressionEncoding::Lz4 => "lz4",
3638
_ => panic!("unexpected encoding {:?}", self.encoding),
3739
};
3840
assert_eq!(req.headers().get("grpc-encoding").unwrap(), expected);
@@ -84,6 +86,7 @@ util::parametrized_tests! {
8486
zstd: CompressionEncoding::Zstd,
8587
gzip: CompressionEncoding::Gzip,
8688
deflate: CompressionEncoding::Deflate,
89+
lz4: CompressionEncoding::Lz4,
8790
}
8891

8992
#[allow(dead_code)]
@@ -93,12 +96,13 @@ async fn client_enabled_server_enabled_multi_encoding(encoding: CompressionEncod
9396
let svc = test_server::TestServer::new(Svc::default())
9497
.accept_compressed(CompressionEncoding::Gzip)
9598
.accept_compressed(CompressionEncoding::Zstd)
96-
.accept_compressed(CompressionEncoding::Deflate);
99+
.accept_compressed(CompressionEncoding::Deflate)
100+
.accept_compressed(CompressionEncoding::Lz4);
97101

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

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

@@ -146,6 +150,7 @@ parametrized_tests! {
146150
zstd: CompressionEncoding::Zstd,
147151
gzip: CompressionEncoding::Gzip,
148152
deflate: CompressionEncoding::Deflate,
153+
lz4: CompressionEncoding::Lz4,
149154
}
150155

151156
#[allow(dead_code)]
@@ -177,6 +182,7 @@ async fn client_enabled_server_disabled(encoding: CompressionEncoding) {
177182
CompressionEncoding::Gzip => "gzip",
178183
CompressionEncoding::Zstd => "zstd",
179184
CompressionEncoding::Deflate => "deflate",
185+
CompressionEncoding::Lz4 => "lz4",
180186
_ => panic!("unexpected encoding {encoding:?}"),
181187
};
182188
assert_eq!(
@@ -194,6 +200,7 @@ parametrized_tests! {
194200
zstd: CompressionEncoding::Zstd,
195201
gzip: CompressionEncoding::Gzip,
196202
deflate: CompressionEncoding::Deflate,
203+
lz4: CompressionEncoding::Lz4,
197204
}
198205

199206
#[allow(dead_code)]

tests/compression/src/compressing_response.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ util::parametrized_tests! {
66
zstd: CompressionEncoding::Zstd,
77
gzip: CompressionEncoding::Gzip,
88
deflate: CompressionEncoding::Deflate,
9+
lz4: CompressionEncoding::Lz4,
910
}
1011

1112
#[allow(dead_code)]
@@ -38,6 +39,7 @@ async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
3839
CompressionEncoding::Gzip => "gzip",
3940
CompressionEncoding::Zstd => "zstd",
4041
CompressionEncoding::Deflate => "deflate",
42+
CompressionEncoding::Lz4 => "lz4",
4143
_ => panic!("unexpected encoding {:?}", self.encoding),
4244
};
4345
assert_eq!(
@@ -88,6 +90,7 @@ async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
8890
CompressionEncoding::Gzip => "gzip",
8991
CompressionEncoding::Zstd => "zstd",
9092
CompressionEncoding::Deflate => "deflate",
93+
CompressionEncoding::Lz4 => "lz4",
9194
_ => panic!("unexpected encoding {encoding:?}"),
9295
};
9396

@@ -104,6 +107,7 @@ util::parametrized_tests! {
104107
zstd: CompressionEncoding::Zstd,
105108
gzip: CompressionEncoding::Gzip,
106109
deflate: CompressionEncoding::Deflate,
110+
lz4: CompressionEncoding::Lz4,
107111
}
108112

109113
#[allow(dead_code)]
@@ -180,7 +184,8 @@ async fn client_enabled_server_disabled_multi_encoding() {
180184
let mut client = test_client::TestClient::new(mock_io_channel(client).await)
181185
.accept_compressed(CompressionEncoding::Gzip)
182186
.accept_compressed(CompressionEncoding::Zstd)
183-
.accept_compressed(CompressionEncoding::Deflate);
187+
.accept_compressed(CompressionEncoding::Deflate)
188+
.accept_compressed(CompressionEncoding::Lz4);
184189

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

@@ -195,6 +200,7 @@ util::parametrized_tests! {
195200
zstd: CompressionEncoding::Zstd,
196201
gzip: CompressionEncoding::Gzip,
197202
deflate: CompressionEncoding::Deflate,
203+
lz4: CompressionEncoding::Lz4,
198204
}
199205

200206
#[allow(dead_code)]
@@ -266,6 +272,7 @@ util::parametrized_tests! {
266272
zstd: CompressionEncoding::Zstd,
267273
gzip: CompressionEncoding::Gzip,
268274
deflate: CompressionEncoding::Deflate,
275+
lz4: CompressionEncoding::Lz4,
269276
}
270277

271278
#[allow(dead_code)]
@@ -310,6 +317,7 @@ util::parametrized_tests! {
310317
zstd: CompressionEncoding::Zstd,
311318
gzip: CompressionEncoding::Gzip,
312319
deflate: CompressionEncoding::Deflate,
320+
lz4: CompressionEncoding::Lz4,
313321
}
314322

315323
#[allow(dead_code)]
@@ -353,6 +361,7 @@ async fn disabling_compression_on_single_response(encoding: CompressionEncoding)
353361
CompressionEncoding::Gzip => "gzip",
354362
CompressionEncoding::Zstd => "zstd",
355363
CompressionEncoding::Deflate => "deflate",
364+
CompressionEncoding::Lz4 => "lz4",
356365
_ => panic!("unexpected encoding {encoding:?}"),
357366
};
358367
assert_eq!(res.metadata().get("grpc-encoding").unwrap(), expected);
@@ -366,6 +375,7 @@ util::parametrized_tests! {
366375
zstd: CompressionEncoding::Zstd,
367376
gzip: CompressionEncoding::Gzip,
368377
deflate: CompressionEncoding::Deflate,
378+
lz4: CompressionEncoding::Lz4,
369379
}
370380

371381
#[allow(dead_code)]
@@ -411,6 +421,7 @@ async fn disabling_compression_on_response_but_keeping_compression_on_stream(
411421
CompressionEncoding::Gzip => "gzip",
412422
CompressionEncoding::Zstd => "zstd",
413423
CompressionEncoding::Deflate => "deflate",
424+
CompressionEncoding::Lz4 => "lz4",
414425
_ => panic!("unexpected encoding {encoding:?}"),
415426
};
416427
assert_eq!(res.metadata().get("grpc-encoding").unwrap(), expected);
@@ -437,6 +448,7 @@ util::parametrized_tests! {
437448
zstd: CompressionEncoding::Zstd,
438449
gzip: CompressionEncoding::Gzip,
439450
deflate: CompressionEncoding::Deflate,
451+
lz4: CompressionEncoding::Lz4,
440452
}
441453

442454
#[allow(dead_code)]
@@ -482,6 +494,7 @@ async fn disabling_compression_on_response_from_client_stream(encoding: Compress
482494
CompressionEncoding::Gzip => "gzip",
483495
CompressionEncoding::Zstd => "zstd",
484496
CompressionEncoding::Deflate => "deflate",
497+
CompressionEncoding::Lz4 => "lz4",
485498
_ => panic!("unexpected encoding {encoding:?}"),
486499
};
487500
assert_eq!(res.metadata().get("grpc-encoding").unwrap(), expected);

tests/compression/src/server_stream.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ util::parametrized_tests! {
77
zstd: CompressionEncoding::Zstd,
88
gzip: CompressionEncoding::Gzip,
99
deflate: CompressionEncoding::Deflate,
10+
lz4: CompressionEncoding::Lz4,
1011
}
1112

1213
#[allow(dead_code)]
@@ -47,6 +48,7 @@ async fn client_enabled_server_enabled(encoding: CompressionEncoding) {
4748
CompressionEncoding::Gzip => "gzip",
4849
CompressionEncoding::Zstd => "zstd",
4950
CompressionEncoding::Deflate => "deflate",
51+
CompressionEncoding::Lz4 => "lz4",
5052
_ => panic!("unexpected encoding {encoding:?}"),
5153
};
5254
assert_eq!(res.metadata().get("grpc-encoding").unwrap(), expected);
@@ -72,6 +74,7 @@ util::parametrized_tests! {
7274
client_disabled_server_enabled,
7375
zstd: CompressionEncoding::Zstd,
7476
gzip: CompressionEncoding::Gzip,
77+
lz4: CompressionEncoding::Lz4,
7578
}
7679

7780
#[allow(dead_code)]
@@ -124,6 +127,7 @@ util::parametrized_tests! {
124127
zstd: CompressionEncoding::Zstd,
125128
gzip: CompressionEncoding::Gzip,
126129
deflate: CompressionEncoding::Deflate,
130+
lz4: CompressionEncoding::Lz4,
127131
}
128132

129133
#[allow(dead_code)]

tests/compression/src/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ impl AssertRightEncoding {
162162
CompressionEncoding::Gzip => "gzip",
163163
CompressionEncoding::Zstd => "zstd",
164164
CompressionEncoding::Deflate => "deflate",
165+
CompressionEncoding::Lz4 => "lz4",
165166
_ => panic!("unexpected encoding {:?}", self.encoding),
166167
};
167168
assert_eq!(req.headers().get("grpc-encoding").unwrap(), expected);

tonic/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ codegen = ["dep:async-trait"]
2424
gzip = ["dep:flate2"]
2525
deflate = ["dep:flate2"]
2626
zstd = ["dep:zstd"]
27+
lz4 = ["dep:lz4_flex"]
2728
default = ["router", "transport", "codegen", "prost"]
2829
prost = ["dep:prost"]
2930
_tls-any = ["dep:tokio-rustls", "dep:tokio", "tokio?/rt", "tokio?/macros"] # Internal. Please choose one of `tls-ring` or `tls-aws-lc`
@@ -91,6 +92,7 @@ webpki-roots = { version = "1", optional = true }
9192
# compression
9293
flate2 = {version = "1.0", optional = true}
9394
zstd = { version = "0.13.0", optional = true }
95+
lz4_flex = { version = "0.11.3", default-features = false, features = ["std", "frame"], optional = true }
9496

9597
# channel
9698
hyper-timeout = {version = "0.5", optional = true}

0 commit comments

Comments
 (0)