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
@@ -0,0 +1,109 @@
|
||||
// #41 (CARD-26, D23): the outbound pump waits while the socket is backed up.
|
||||
//
|
||||
// Over a real `ws` server on Node: a route that can produce 10,000 frames of 64 KiB (640 MiB)
|
||||
// for a client that has stopped reading. Without backpressure the pump pulls all of them into
|
||||
// the send buffer at once; with it, it stops pulling after about the high-water mark plus what
|
||||
// the kernel's socket buffers absorb. The Deno and Bun twins live in test/deno and test/bun; the
|
||||
// exact "mark plus one frame" bound is pinned with a fake socket in ws-core.test.ts.
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import WebSocket from 'ws';
|
||||
import { createHttpServer, type WsRouteDef } from '../src/http';
|
||||
|
||||
const FRAMES = 10_000;
|
||||
const FRAME = 64 * 1024;
|
||||
/** Frame `i`: its index, zero-padded, then filler up to 64 KiB. */
|
||||
const frame = (i: number) => String(i).padStart(8, '0') + 'x'.repeat(FRAME - 8);
|
||||
|
||||
const url = (server: import('http').Server, path: string) => {
|
||||
const a = server.address();
|
||||
if (a && typeof a === 'object') return `ws://127.0.0.1:${a.port}${path}`;
|
||||
throw new Error('no address');
|
||||
};
|
||||
|
||||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
describe('ws outbound backpressure (#41)', () => {
|
||||
it('stops pulling while the client is not reading, then delivers every frame in order (T1)', async () => {
|
||||
let pulled = 0;
|
||||
const wsRoutes: WsRouteDef[] = [
|
||||
{
|
||||
pattern: '/flood',
|
||||
open: async () =>
|
||||
(async function* () {
|
||||
for (let i = 0; i < FRAMES; i++) {
|
||||
pulled++;
|
||||
yield frame(i);
|
||||
}
|
||||
})(),
|
||||
},
|
||||
];
|
||||
const server = createHttpServer([], wsRoutes);
|
||||
await new Promise<void>((resolve) => server.listen(0, resolve));
|
||||
|
||||
const client = new WebSocket(url(server, '/flood'));
|
||||
await new Promise((resolve) => client.once('upgrade', resolve));
|
||||
// The client stops reading from TCP: the server's send buffer is all that can grow.
|
||||
(client as any)._socket.pause();
|
||||
|
||||
await sleep(300);
|
||||
const stalled = pulled;
|
||||
await sleep(200);
|
||||
|
||||
// Pulling has stopped, far short of the 10,000 frames on offer: about the 1 MiB mark plus the
|
||||
// kernel's socket buffers. Before #41 every frame was pulled at once, 640 MiB in memory.
|
||||
expect(pulled).toBe(stalled);
|
||||
expect(stalled).toBeLessThan(FRAMES / 10);
|
||||
|
||||
let next = 0;
|
||||
let inOrder = true;
|
||||
const done = new Promise<void>((resolve) => {
|
||||
client.on('message', (data) => {
|
||||
if (data.toString('latin1', 0, 8) !== String(next).padStart(8, '0')) inOrder = false;
|
||||
if (++next === FRAMES) resolve();
|
||||
});
|
||||
});
|
||||
(client as any)._socket.resume();
|
||||
await done;
|
||||
|
||||
expect(inOrder).toBe(true);
|
||||
expect(pulled).toBe(FRAMES);
|
||||
client.close();
|
||||
server.close();
|
||||
}, 60_000);
|
||||
|
||||
it('calls the iterator return() when the client goes away while the pump waits (T2)', async () => {
|
||||
let pulled = 0;
|
||||
let returned = false;
|
||||
const wsRoutes: WsRouteDef[] = [
|
||||
{
|
||||
pattern: '/flood',
|
||||
open: async () =>
|
||||
(async function* () {
|
||||
try {
|
||||
for (let i = 0; i < FRAMES; i++) {
|
||||
pulled++;
|
||||
yield frame(i);
|
||||
}
|
||||
} finally {
|
||||
returned = true;
|
||||
}
|
||||
})(),
|
||||
},
|
||||
];
|
||||
const server = createHttpServer([], wsRoutes);
|
||||
await new Promise<void>((resolve) => server.listen(0, resolve));
|
||||
|
||||
const client = new WebSocket(url(server, '/flood'));
|
||||
await new Promise((resolve) => client.once('upgrade', resolve));
|
||||
(client as any)._socket.pause();
|
||||
await sleep(300);
|
||||
expect(pulled).toBeLessThan(FRAMES / 10); // waiting, not finished
|
||||
|
||||
client.terminate();
|
||||
await sleep(200);
|
||||
|
||||
expect(returned).toBe(true);
|
||||
expect(pulled).toBeLessThan(FRAMES / 10);
|
||||
server.close();
|
||||
}, 30_000);
|
||||
});
|
||||
Reference in new issue
Block a user