fix(ws): wait for the send buffer before pulling the next outbound value (CARD-26, #41)
The outbound pump pulled a @Ws handler's iterator as fast as it yielded, whatever the peer read. With a slow client, 10,000 frames of 64 KiB were all pulled at once and sat in the send buffer: 640 MiB. WsSocket gains one optional member, drained(): it resolves once the socket's send buffer is at or below the adapter's high-water mark (WS_HIGH_WATER_MARK, 1 MiB, internal), or the socket has closed. The pump awaits it before each pull, so the buffer peaks at the mark plus one frame. A close ends the wait and the abort path runs the iterator's return(), as before. A socket without drained() is never waited on. Per runtime: - Node: ws.bufferedAmount, released by each send's write callback; - Bun: getBufferedAmount(), released by the server's drain callback; - Deno: polls bufferedAmount every 10 ms, only while over the mark; - workerd reports no buffered amount, so the edge never waits. The Deno poll also waits while the socket is CONNECTING. Deno hands the socket over before it opens, and a handler that yielded at once had its first value pulled into a socket that was not open yet. The pump then gave up, so the client saw no frame and a 1005 close. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
10 files changed
+616
-8
No files matched your search
@@ -152,3 +152,106 @@ describe('runWsConnection correlation', () => {
|
||||
expect(open.transport).toBe('ws');
|
||||
});
|
||||
});
|
||||
|
||||
describe('runWsConnection outbound backpressure (#41)', () => {
|
||||
/** A fake socket with a send buffer that only empties when the test says so. */
|
||||
function bufferedSocket(mark: number) {
|
||||
const fake = fakeSocket();
|
||||
let buffered = 0;
|
||||
let wake: (() => void) | null = null;
|
||||
const socket: WsSocket = Object.assign(Object.create(fake.socket), {
|
||||
send: (d: string) => {
|
||||
fake.sent.push(d);
|
||||
buffered += d.length;
|
||||
},
|
||||
drained: () =>
|
||||
buffered <= mark || fake.ac.signal.aborted
|
||||
? Promise.resolve()
|
||||
: new Promise<void>((resolve) => {
|
||||
wake = resolve;
|
||||
fake.ac.signal.addEventListener('abort', () => resolve(), { once: true });
|
||||
}),
|
||||
});
|
||||
const flush = () => {
|
||||
buffered = 0;
|
||||
wake?.();
|
||||
wake = null;
|
||||
};
|
||||
return { ...fake, socket, flush, buffered: () => buffered };
|
||||
}
|
||||
|
||||
const flood = (count: number, size: number, onPull: () => void, onReturn = () => {}) => ({
|
||||
pattern: '/ws',
|
||||
open: async () =>
|
||||
(async function* () {
|
||||
try {
|
||||
for (let i = 0; i < count; i++) {
|
||||
onPull();
|
||||
yield String(i).padStart(size, '0');
|
||||
}
|
||||
} finally {
|
||||
onReturn();
|
||||
}
|
||||
})(),
|
||||
});
|
||||
|
||||
it('pulls nothing more once the buffer is over the mark: it peaks at the mark plus one frame (T1)', async () => {
|
||||
let pulled = 0;
|
||||
const { socket, flush, buffered, sent } = bufferedSocket(10);
|
||||
const run = runWsConnection(socket, req, { def: flood(100, 4, () => pulled++), params: {} });
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
expect(pulled).toBe(3); // 4 + 4 + 4 = 12 > 10: waits after the third frame
|
||||
expect(buffered()).toBeLessThanOrEqual(10 + 4);
|
||||
|
||||
while (pulled < 100) {
|
||||
flush();
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
}
|
||||
flush();
|
||||
await run;
|
||||
expect(sent.map(Number)).toEqual([...Array(100).keys()]);
|
||||
});
|
||||
|
||||
it('ends the wait on close and calls the iterator return(), without an unhandled rejection (T2)', async () => {
|
||||
let pulled = 0;
|
||||
let returned = false;
|
||||
const unhandled: unknown[] = [];
|
||||
const onUnhandled = (err: unknown) => unhandled.push(err);
|
||||
process.on('unhandledRejection', onUnhandled);
|
||||
|
||||
try {
|
||||
const { socket, ac } = bufferedSocket(0);
|
||||
const run = runWsConnection(socket, req, {
|
||||
def: flood(100, 4, () => pulled++, () => (returned = true)),
|
||||
params: {},
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
expect(pulled).toBe(1);
|
||||
|
||||
ac.abort(); // the peer went away
|
||||
await run;
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
|
||||
expect(returned).toBe(true);
|
||||
expect(pulled).toBe(1);
|
||||
expect(unhandled).toEqual([]);
|
||||
} finally {
|
||||
process.off('unhandledRejection', onUnhandled);
|
||||
}
|
||||
});
|
||||
|
||||
it('never waits below the mark: drained() answers at once and every frame goes out (T3)', async () => {
|
||||
let waits = 0;
|
||||
const { socket, sent } = fakeSocket();
|
||||
const counted: WsSocket = Object.assign(Object.create(socket), {
|
||||
drained: () => {
|
||||
waits++;
|
||||
return Promise.resolve();
|
||||
},
|
||||
});
|
||||
await runWsConnection(counted, req, { def: flood(50, 4, () => {}), params: {} });
|
||||
expect(sent).toHaveLength(50);
|
||||
expect(waits).toBe(51); // once before each pull, the last one being the pull that ends it
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user