Repository navigation
perf(ws): [NBR] cut WebSocket control, Ack, and framing wire bytes by 45-75% #48440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,9 +70,19 @@ interface Window { | |
| readonly sizes: number[]; | ||
| } | ||
|
|
||
| export interface OutputWindowOptions { | ||
| readonly ackWindow?: number; | ||
| } | ||
|
|
||
| export function withOutputWindow( | ||
| protocol: RpcServer.Protocol["Service"], | ||
| options?: OutputWindowOptions, | ||
| ): RpcServer.Protocol["Service"] { | ||
| // Keep at least two ack windows inside MAX_PENDING_CHUNKS so batching never stalls a stream. | ||
| const ackWindow = Math.max( | ||
| 1, | ||
| Math.min(Math.floor(options?.ackWindow ?? 1), Math.floor(MAX_PENDING_CHUNKS / 2)), | ||
| ); | ||
| // RpcServer drops a disconnected client before its streams exit, so their Exit never | ||
| // reaches send; departed clients are pruned when a window opens instead. | ||
| const clients = new Map<number, Map<string, Window>>(); | ||
|
|
@@ -87,7 +97,12 @@ export function withOutputWindow( | |
| for (const id of clients.keys()) if (!connected.has(id)) clients.delete(id); | ||
| let windows = clients.get(clientId); | ||
| if (!windows) clients.set(clientId, (windows = new Map())); | ||
| if (!windows.has(requestId)) windows.set(requestId, { bytes, sizes: [] }); | ||
| let window = windows.get(requestId); | ||
| if (!window) { | ||
| window = { bytes, sizes: [] }; | ||
| windows.set(requestId, window); | ||
| } | ||
| return window; | ||
| }); | ||
| return { | ||
| ...protocol, | ||
|
|
@@ -102,11 +117,13 @@ export function withOutputWindow( | |
| Effect.andThen(write(clientId, message)), | ||
| ); | ||
| } | ||
| } else if (message._tag === "Interrupt") { | ||
| clients.get(clientId)?.delete(String(message.requestId)); | ||
| } else if (message._tag === "Ack") { | ||
| const window = windowFor(clientId, String(message.requestId)); | ||
| if (window) { | ||
| const wasFull = isFull(window.sizes); | ||
| window.sizes.shift(); | ||
| window.sizes.splice(0, ackWindow); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium A single Ack after a ≥32 KiB chunk releases credit for up to four chunks, so a second unacknowledged chunk is treated as consumed and the server can keep sending past the 64 KiB backpressure limit. 🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Byte-triggered Acks can release more output than the client acknowledged. For For example, four 20 KiB chunks fill the 64 KiB byte window. If the client consumes chunks 1 and 2, it sends one The
|
||
| if (!wasFull || isFull(window.sizes)) return Effect.void; | ||
| } | ||
| } | ||
|
|
@@ -120,8 +137,8 @@ export function withOutputWindow( | |
| if (response._tag === "Exit") { | ||
| clients.get(clientId)?.delete(String(response.requestId)); | ||
| } else if (response._tag === "Chunk") { | ||
| const window = windowFor(clientId, String(response.requestId)); | ||
| if (window) { | ||
| const requestId = String(response.requestId); | ||
| const pushAndAck = (window: Window) => { | ||
| window.sizes.push(window.bytes ? chunkSize(response.values) : 0); | ||
| if (!isFull(window.sizes)) { | ||
| return send.pipe( | ||
|
|
@@ -133,6 +150,16 @@ export function withOutputWindow( | |
| ), | ||
| ); | ||
| } | ||
| return send; | ||
| }; | ||
| const window = windowFor(clientId, requestId); | ||
| if (window) { | ||
| return pushAndAck(window); | ||
| } | ||
| // When windowed Acks are negotiated, lazily count-window every stream so clients | ||
| // can batch Acks uniformly without stalling on unwindowed 1-chunk streams. | ||
| if (ackWindow > 1) { | ||
| return openWindow(clientId, requestId, false).pipe(Effect.flatMap(pushAndAck)); | ||
| } | ||
| } | ||
| return send; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extend the existing output-window test table.
The existing table already drives the same request, backpressure, Ack, and interrupt sequence. The new cases copy the RPC fixture twice. Add a negotiated window and expected release count to the existing harness. Keep the second full-window cycle for count batching and the byte-window assertions for terminal output. (raw.githubusercontent.com)
As per coding guidelines, “When an existing test or table covers the behavior, add a row or an assertion there.” As per path instructions, prefer extending a fitting existing case.
Also applies to: 203-203
Sources: Coding guidelines, Path instructions