Skip to content

Commit 2a86e92

Browse files
committed
refactor(moonzero): catch up to moonrpc, and read the gRPC framing through it.
The pin was still on moonrpc 0.12.0, from before HPACK and the header type moved to moonhttp, so `@moonrpc.Header`, `HpackEncoder` and `HpackDecoder` are now `@header.Header`, `@hpack.Encoder` and `@hpack.Decoder`. `decode_all_messages` was a word-for-word copy of what moonrpc now calls `split_messages`, and `drain_messages` read the same length prefix a second time. The reading is moonrpc's; what stays here is carrying the remainder across DATA frames, which is this package's own. Signed-off-by: Leo Cheng (heke1228) <chengkelfan@qq.com>
1 parent 098dae5 commit 2a86e92

5 files changed

Lines changed: 25 additions & 47 deletions

File tree

‎.gitignore‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@
44
_build/
55
target/
66

7-
# example packages regenerate these; only the library interface is tracked
87
examples/*/pkg.generated.mbti

‎moon.mod‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "moonbitstack/moonzero"
22

3-
version = "0.8.0"
3+
version = "0.9.0"
44

55
readme = "README.md"
66

@@ -22,9 +22,10 @@ description = "moonzero — a service framework for MoonBit (← go-zero): confi
2222
import {
2323
"moonbitstack/moonapi@0.11.0",
2424
"moonbitstack/moonasgi@0.10.0",
25-
"moonbitstack/moonrpc@0.12.0",
26-
"moonbitstack/mooncrypt@0.2.2",
27-
"moonbitstack/mooncred@0.5.0",
25+
"moonbitstack/moonrpc@0.17.0",
26+
"moonbitstack/mooncrypt@0.3.0",
27+
"moonbitstack/mooncred@0.6.0",
28+
"moonbitstack/moonhttp@0.7.0",
2829
"moonbitstack/moonjson@0.3.0",
2930
"moonbitstack/moonlog@0.1.0",
3031
"moonbitlang/async@0.20.3",

‎moon.pkg‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
"moonbitstack/moonapi",
66
"moonbitstack/moonasgi",
77
"moonbitstack/moonrpc",
8+
"moonbitstack/moonhttp/header",
9+
"moonbitstack/moonhttp/hpack",
810
"moonbitstack/mooncrypt/hash/sha2",
911
"moonbitstack/mooncrypt/mac/hmac",
1012
"moonbitstack/mooncrypt/spec",

‎pkg.generated.mbti‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
"moonbitstack/moonasgi/http",
99
"moonbitstack/moonasgi/spec",
1010
"moonbitstack/mooncred/jwt",
11+
"moonbitstack/moonhttp/hpack",
1112
"moonbitstack/moonlog",
1213
"moonbitstack/moonrpc",
1314
}
@@ -852,8 +853,8 @@ pub fn RoundRobin::pick(Self, Array[Endpoint]) -> Endpoint?
852853

853854
pub struct RpcChannel {
854855
engine : @moonrpc.H2Server
855-
encoder : @moonrpc.HpackEncoder
856-
decoder : @moonrpc.HpackDecoder
856+
encoder : @hpack.Encoder
857+
decoder : @hpack.Decoder
857858
authority : String
858859
mut next_stream_id : Int
859860
}

‎zrpc.mbt‎

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ fn parse_ascii_int(b : Bytes) -> Int {
2828
}
2929

3030
///|
31-
/// The value of the first `@moonrpc.Header` whose name equals the ASCII `name`,
31+
/// The value of the first `@header.Header` whose name equals the ASCII `name`,
3232
/// or `None`.
33-
fn h2_header_value(headers : Array[@moonrpc.Header], name : String) -> Bytes? {
33+
fn h2_header_value(headers : Array[@header.Header], name : String) -> Bytes? {
3434
let want = ascii_bytes(name)
3535
for h in headers {
3636
if h.name == want {
@@ -102,8 +102,8 @@ pub fn RpcServer::to_h2(self : RpcServer) -> @moonrpc.H2Server {
102102
/// across every call on the channel.
103103
pub struct RpcChannel {
104104
engine : @moonrpc.H2Server
105-
encoder : @moonrpc.HpackEncoder
106-
decoder : @moonrpc.HpackDecoder
105+
encoder : @hpack.Encoder
106+
decoder : @hpack.Decoder
107107
authority : String
108108
mut next_stream_id : Int
109109
}
@@ -119,8 +119,8 @@ pub fn RpcChannel::connect(
119119
let engine = server.to_h2()
120120
let ch = {
121121
engine,
122-
encoder: @moonrpc.HpackEncoder::new(),
123-
decoder: @moonrpc.HpackDecoder::new(),
122+
encoder: @hpack.Encoder::new(),
123+
decoder: @hpack.Decoder::new(),
124124
authority,
125125
next_stream_id: 1,
126126
}
@@ -346,17 +346,14 @@ pub fn RpcChannel::call_bidi_streaming(
346346
///|
347347
/// Split a run of concatenated gRPC length-prefixed messages into their payloads,
348348
/// returning the complete messages and any trailing partial-message octets that
349-
/// have not yet arrived in full. Like `decode_all_messages` but surfaces the
349+
/// have not yet arrived in full. Like `@moonrpc.split_messages` but surfaces the
350350
/// remainder so a streaming caller can carry it across DATA frames.
351351
fn drain_messages(body : Bytes) -> (Array[Bytes], Bytes) {
352352
let out : Array[Bytes] = []
353353
let n = body.length()
354354
let mut off = 0
355355
while n - off >= 5 {
356-
let len = (body[off + 1].to_int() << 24) |
357-
(body[off + 2].to_int() << 16) |
358-
(body[off + 3].to_int() << 8) |
359-
body[off + 4].to_int()
356+
let len = @moonrpc.message_len(body[:], at=off).unwrap()
360357
if n - off < 5 + len {
361358
break
362359
}
@@ -379,12 +376,12 @@ fn RpcChannel::open(
379376
let sid = self.next_stream_id
380377
self.next_stream_id = self.next_stream_id + 2
381378
let block = self.encoder.encode([
382-
{ name: b":method", value: b"POST", },
383-
{ name: b":scheme", value: b"http", },
384-
{ name: b":path", value: ascii_bytes(path), },
385-
{ name: b":authority", value: ascii_bytes(self.authority), },
386-
{ name: b"content-type", value: b"application/grpc", },
387-
{ name: b"te", value: b"trailers", },
379+
@header.Header::of(":method", "POST"),
380+
@header.Header::of(":scheme", "http"),
381+
@header.Header::of(":path", path),
382+
@header.Header::of(":authority", self.authority),
383+
@header.Header::of("content-type", "application/grpc"),
384+
@header.Header::of("te", "trailers"),
388385
])
389386
let frames = self.engine.feed(
390387
Headers(
@@ -441,27 +438,5 @@ fn RpcChannel::collect_reply(
441438
_ => ()
442439
}
443440
}
444-
(status_code, decode_all_messages(body.to_bytes()))
445-
}
446-
447-
///|
448-
/// Split a run of concatenated gRPC length-prefixed messages into their payloads,
449-
/// stopping at the first truncated frame. Each message is a 1-byte compression flag
450-
/// plus a 4-byte big-endian length plus that many payload octets.
451-
fn decode_all_messages(body : Bytes) -> Array[Bytes] {
452-
let out : Array[Bytes] = []
453-
let n = body.length()
454-
let mut off = 0
455-
while n - off >= 5 {
456-
let len = (body[off + 1].to_int() << 24) |
457-
(body[off + 2].to_int() << 16) |
458-
(body[off + 3].to_int() << 8) |
459-
body[off + 4].to_int()
460-
if n - off < 5 + len {
461-
break
462-
}
463-
out.push(body[off + 5:off + 5 + len].to_owned())
464-
off = off + 5 + len
465-
}
466-
out
441+
(status_code, @moonrpc.split_messages(body.to_bytes()))
467442
}

0 commit comments

Comments
 (0)