Skip to content

Commit fc281c7

Browse files
committed
fix(volo-grpc): use linkedbytes concat to get the full encoded results
1 parent 9ca4f96 commit fc281c7

File tree

7 files changed

+273
-36
lines changed

7 files changed

+273
-36
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,12 @@ volo = { path = "../volo" }
153153
volo-grpc = { path = "../volo-grpc", features = ["grpc-web"] }
154154
volo-thrift = { path = "../volo-thrift", features = ["multiplex"] }
155155
volo-http = { path = "../volo-http", features = [
156-
"client", "server",
157-
"json", "query", "form", "cookie",
156+
"client",
157+
"server",
158+
"json",
159+
"query",
160+
"form",
161+
"cookie",
158162
"http1",
159163
] }
160164

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
syntax = "proto3";
2+
3+
package codegen;
4+
5+
// 枚举
6+
enum Status {
7+
STATUS_UNSPECIFIED = 0;
8+
OK = 1;
9+
FAIL = 2;
10+
}
11+
12+
// 基础类型全集
13+
message BasicTypes {
14+
bool bool_ = 1;
15+
int32 int32_ = 2;
16+
int64 int64_ = 3;
17+
uint32 uint32_ = 4;
18+
uint64 uint64_ = 5;
19+
sint32 sint32_ = 6;
20+
sint64 sint64_ = 7;
21+
fixed32 fixed32_ = 8;
22+
fixed64 fixed64_ = 9;
23+
sfixed32 sfixed32_ = 10;
24+
sfixed64 sfixed64_ = 11;
25+
float float_ = 12;
26+
double double_ = 13;
27+
string string_ = 14;
28+
bytes bytes_ = 15;
29+
}
30+
31+
// oneof/可选语义
32+
message WithOneof {
33+
oneof value {
34+
int32 int_value = 1;
35+
string string_value = 2;
36+
BasicTypes basic_value = 3;
37+
}
38+
}
39+
40+
// map/repeated
41+
message Collections {
42+
repeated int32 numbers = 1;
43+
repeated BasicTypes items = 2;
44+
map<string, int32> dict = 3;
45+
map<int32, BasicTypes> id_to_item = 4;
46+
}
47+
48+
// 嵌套与保留字段
49+
message Parent {
50+
reserved 3, 4;
51+
reserved "old_field", "legacy";
52+
53+
message Child {
54+
string name = 1;
55+
Status status = 2;
56+
}
57+
58+
repeated Child children = 1;
59+
Status status = 2;
60+
}
61+
62+
// 业务请求/响应
63+
message EchoRequest {
64+
string message = 1;
65+
BasicTypes basic = 2;
66+
Collections coll = 3;
67+
WithOneof maybe = 4;
68+
}
69+
70+
message EchoResponse {
71+
string message = 1;
72+
Status status = 2;
73+
WithOneof echoed = 3;
74+
}
75+
76+
// 流式与非流式服务
77+
service TestService {
78+
rpc UnaryEcho(EchoRequest) returns (EchoResponse);
79+
rpc ClientStreamingEcho(stream EchoRequest) returns (EchoResponse);
80+
rpc ServerStreamingEcho(EchoRequest) returns (stream EchoResponse);
81+
rpc BidiStreamingEcho(stream EchoRequest) returns (stream EchoResponse);
82+
}
83+
84+

tests/code-generation/volo.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ entries:
1616
path: examples/addressbook.proto
1717
includes:
1818
- examples
19+
- idl:
20+
source: local
21+
path: proto/test.proto
22+
includes:
23+
- proto
1924
thrift:
2025
filename: thrift_gen.rs
2126
protocol: thrift

volo-grpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ hyper-util = { workspace = true, features = [
5353
"server",
5454
"http2",
5555
] }
56+
linkedbytes.workspace = true
5657
matchit.workspace = true
5758
paste.workspace = true
5859
percent-encoding.workspace = true

volo-grpc/src/codec/compression.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -274,23 +274,23 @@ impl CompressionEncoding {
274274
/// Compress `len` bytes from `src_buf` into `dest_buf`.
275275
pub(crate) fn compress(
276276
encoding: CompressionEncoding,
277-
src_buf: &mut LinkedBytes,
278-
dest_buf: &mut LinkedBytes,
277+
src_buf: &mut BytesMut,
278+
dest_buf: &mut BytesMut,
279279
) -> Result<(), io::Error> {
280-
let len = src_buf.bytes().len();
280+
let len = src_buf.len();
281281
let capacity = ((len / BUFFER_SIZE) + 1) * BUFFER_SIZE;
282282

283283
dest_buf.reserve(capacity);
284284

285285
match encoding {
286286
#[cfg(feature = "gzip")]
287287
CompressionEncoding::Gzip(Some(config)) => {
288-
let mut gz_encoder = GzEncoder::new(&src_buf.bytes()[0..len], config.level);
288+
let mut gz_encoder = GzEncoder::new(&src_buf[0..len], config.level);
289289
io::copy(&mut gz_encoder, &mut dest_buf.writer())?;
290290
}
291291
#[cfg(feature = "zlib")]
292292
CompressionEncoding::Zlib(Some(config)) => {
293-
let mut zlib_encoder = ZlibEncoder::new(&src_buf.bytes()[0..len], config.level);
293+
let mut zlib_encoder = ZlibEncoder::new(&src_buf[0..len], config.level);
294294
io::copy(&mut zlib_encoder, &mut dest_buf.writer())?;
295295
}
296296
#[cfg(feature = "zstd")]
@@ -302,13 +302,13 @@ pub(crate) fn compress(
302302
level as i32
303303
};
304304
let mut zstd_encoder = zstd::Encoder::new(dest_buf.writer(), zstd_level)?;
305-
io::copy(&mut &src_buf.bytes()[0..len], &mut zstd_encoder)?;
305+
io::copy(&mut &src_buf[0..len], &mut zstd_encoder)?;
306306
zstd_encoder.finish()?;
307307
}
308308
_ => {}
309309
};
310310

311-
src_buf.bytes_mut().advance(len);
311+
src_buf.advance(len);
312312
Ok(())
313313
}
314314

@@ -349,7 +349,7 @@ pub(crate) fn decompress(
349349

350350
#[cfg(test)]
351351
mod tests {
352-
use bytes::BufMut;
352+
use bytes::{BufMut, BytesMut};
353353
use pilota::LinkedBytes;
354354

355355
#[cfg(feature = "gzip")]
@@ -367,11 +367,11 @@ mod tests {
367367

368368
#[test]
369369
fn test_consistency_for_compression() {
370-
let mut src = LinkedBytes::with_capacity(BUFFER_SIZE);
371-
let mut compress_buf = LinkedBytes::new();
372-
let mut de_data = LinkedBytes::with_capacity(BUFFER_SIZE);
370+
let mut src = BytesMut::with_capacity(BUFFER_SIZE);
371+
let mut compress_buf = BytesMut::new();
372+
let mut de_data = BytesMut::with_capacity(BUFFER_SIZE);
373373
let test_data = &b"test compression"[..];
374-
src.put(test_data);
374+
src.extend_from_slice(test_data);
375375

376376
let encodings = [
377377
#[cfg(feature = "gzip")]
@@ -390,11 +390,10 @@ mod tests {
390390
];
391391

392392
for encoding in encodings {
393-
compress_buf.reset();
393+
compress_buf.clear();
394394
compress(encoding, &mut src, &mut compress_buf).expect("compress failed:");
395-
decompress(encoding, compress_buf.bytes_mut(), de_data.bytes_mut())
396-
.expect("decompress failed:");
397-
assert_eq!(test_data, de_data.bytes());
395+
decompress(encoding, &mut compress_buf, &mut de_data).expect("decompress failed:");
396+
assert_eq!(test_data, de_data.as_ref());
398397
}
399398
}
400399
}

0 commit comments

Comments
 (0)