perf(gateway): read worker frames through a 64 KiB read-ahead buffer (#235)

The gateway read each frame with two exact reads, one for the 4-byte prefix and
one for the body, so every frame cost two pipe I/O completions and two
thread-pool wake-ups. WorkerFrameReader now reads as much as its buffer has
room for, so one read normally serves a whole frame or a batch of frames.

Length checks still run on the prefix before any payload allocation; frames
larger than the buffer are assembled in a pooled array; EOF anywhere reports
EndOfStream with an EndOfStreamException inner exception as before. New tests
cover multiple frames per read, frames split across reads, frames straddling
the buffer end, frames larger than the buffer, oversize frames with their body
buffered, and EOF in the prefix, body, large body and at a frame boundary.
This commit is contained in:
Joseph Doherty
2026-11-21 08:20:00 -05:00
parent 9b9fdecc9b
commit 027d2b5d1a
3 changed files with 323 additions and 43 deletions
+12
View File
@@ -22,6 +22,18 @@ public gRPC cap plus a 64 KiB envelope-overhead reserve (16842752 bytes) so a
maximally-sized accepted gRPC payload always fits one worker frame once wrapped
in a `WorkerEnvelope`.
The gateway's reader (`ZB.MOM.WW.MxGateway.Server.Workers.WorkerFrameReader`)
reads ahead into a 64 KiB buffer rather than issuing an exact read for the
prefix and another for the body. One pipe read then normally yields a whole
frame, or several frames when the worker wrote a batch, instead of two I/O
completions — two thread-pool wake-ups — per frame (mxaccessgw#235). The length
checks still run on the prefix alone, before anything is allocated for the
payload. A frame larger than the buffer is assembled in a pooled array from the
buffered head and an exact read of the rest. The stream ending anywhere inside a
frame, or between frames, is reported as `EndOfStream` as before. Because the
buffer carries bytes between calls, every read of the gateway end of the pipe
goes through the one reader instance, handshake included.
The gateway is the source of truth for this maximum: it conveys the negotiated
value in the handshake as `GatewayHello.max_frame_bytes`, and the worker adopts
it as its `WorkerFrameProtocolOptions.MaxMessageBytes` instead of a hard-coded