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,114 @@
|
||||
// #41 (CARD-26, D23) on Deno: the outbound pump waits while the socket is backed up. Deno's
|
||||
// WebSocket fires no drain event, so the adapter polls `bufferedAmount` while over the mark.
|
||||
// Node twin: test/ws-backpressure.test.ts, which explains the cases.
|
||||
//
|
||||
// Run with: npm run test:deno
|
||||
import 'npm:reflect-metadata';
|
||||
import { assert, assertEquals } from 'jsr:@std/assert';
|
||||
import { createApp, Route, Ws, Module } from '../../src/index.ts';
|
||||
import { serveDeno } from '../../src/deno.ts';
|
||||
import { rawWs } from '../helpers/raw-ws.ts';
|
||||
|
||||
const FRAMES = 10_000;
|
||||
const FRAME = 64 * 1024;
|
||||
const frame = (i: number) => String(i).padStart(8, '0') + 'x'.repeat(FRAME - 8);
|
||||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
const state = { pulled: 0, returned: false };
|
||||
|
||||
@Route('/bp')
|
||||
class Flood {
|
||||
@Ws('/flood')
|
||||
flood() {
|
||||
return (async function* () {
|
||||
try {
|
||||
for (let i = 0; i < FRAMES; i++) {
|
||||
state.pulled++;
|
||||
yield frame(i);
|
||||
}
|
||||
} finally {
|
||||
state.returned = true;
|
||||
}
|
||||
})();
|
||||
}
|
||||
}
|
||||
@Route('/early')
|
||||
class Early {
|
||||
// Yields before Deno's socket has left CONNECTING. The pump used to pull 'hi', find the socket
|
||||
// not open, and give up: the client got no frame and a close without a code (1005).
|
||||
@Ws('/hi')
|
||||
hi() {
|
||||
return (async function* () {
|
||||
yield 'hi';
|
||||
await new Promise<never>(() => {});
|
||||
})();
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', controllers: [Flood, Early] })
|
||||
class M {}
|
||||
|
||||
async function serve() {
|
||||
state.pulled = 0;
|
||||
state.returned = false;
|
||||
let port = 0;
|
||||
const server = await serveDeno(createApp({ modules: [M] }), {
|
||||
port: 0,
|
||||
onListen: (addr) => {
|
||||
port = addr.port;
|
||||
},
|
||||
});
|
||||
await sleep(50);
|
||||
return { server, port };
|
||||
}
|
||||
|
||||
Deno.test('serveDeno: ws outbound stops pulling while the client is not reading, then delivers in order (T1)', async () => {
|
||||
const { server, port } = await serve();
|
||||
try {
|
||||
const client = await rawWs(port, '/bp/flood', FRAME);
|
||||
client.pause();
|
||||
await sleep(300);
|
||||
const stalled = state.pulled;
|
||||
await sleep(200);
|
||||
assertEquals(state.pulled, stalled);
|
||||
assert(stalled < FRAMES / 10, `pulled ${stalled} of ${FRAMES} with nobody reading`);
|
||||
|
||||
assertEquals(await client.readAll(FRAMES), { received: FRAMES, inOrder: true });
|
||||
assertEquals(state.pulled, FRAMES);
|
||||
client.destroy();
|
||||
} finally {
|
||||
await server.shutdown();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test('serveDeno: ws outbound ends the wait and calls return() when the client goes away (T2)', async () => {
|
||||
const { server, port } = await serve();
|
||||
try {
|
||||
const client = await rawWs(port, '/bp/flood', FRAME);
|
||||
client.pause();
|
||||
await sleep(300);
|
||||
assert(state.pulled < FRAMES / 10);
|
||||
|
||||
client.destroy();
|
||||
await sleep(300);
|
||||
assertEquals(state.returned, true);
|
||||
assert(state.pulled < FRAMES / 10);
|
||||
} finally {
|
||||
await server.shutdown();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test('serveDeno: a ws route that yields at once still delivers its first frame', async () => {
|
||||
const { server, port } = await serve();
|
||||
try {
|
||||
const first = await new Promise<string>((resolve, reject) => {
|
||||
const ws = new WebSocket(`ws://127.0.0.1:${port}/early/hi`);
|
||||
ws.onmessage = (e) => {
|
||||
resolve(String(e.data));
|
||||
ws.close();
|
||||
};
|
||||
ws.onclose = (e) => reject(new Error(`closed ${e.code} before any frame`));
|
||||
});
|
||||
assertEquals(first, 'hi');
|
||||
} finally {
|
||||
await server.shutdown();
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user