fix(mesh): give the control channel its own inbound queue bound (#148)
The control channel also inherited limits.maxWsQueue (16 undelivered frames, close 1013) from #107. On Node, ws emits every frame of one socket read synchronously, so a teacup with more than 16 RPCs in flight sent a burst the teapot's queue refused: the link dropped and every pending request failed 503. Deno and Bun deliver one frame per task and did not trip. controlLimits() now also raises maxWsQueue for the control channel, to an internal 4,096 frames or maxWsQueue if larger, alongside the size bound from #120. The teapot's frame loop never awaits between frames, so the queue only holds one delivery burst, and one read (at most 64 KiB) holds fewer than 4,096 of the smallest control frame. The bound stays finite, so a peer flooding a channel nobody is reading is still cut off. No new option. User @Ws routes keep closing 1013 at maxWsQueue. Tests: 64 RPCs in one tick through app.fetch on one link all answer 200 (red on Node before), and a user route on the same teapot still closes 1013, on Node, Deno and Bun. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
5 files changed
+165
-8
No files matched your search
@@ -24,6 +24,15 @@ class BigCtl {
|
||||
for await (const msg of inbound) yield `echo:${String(msg).length}`;
|
||||
})();
|
||||
}
|
||||
|
||||
// Never reads `inbound`: every frame stays queued, so the 17th closes 1013 (#107).
|
||||
@Ws('/stall')
|
||||
stall() {
|
||||
return (async function* () {
|
||||
await new Promise<never>(() => {});
|
||||
yield '';
|
||||
})();
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/api', controllers: [BigCtl] })
|
||||
class TeapotModule {}
|
||||
@@ -134,3 +143,40 @@ test('mesh on Bun: the control-channel cap follows a raised maxBodyBytes (T4)',
|
||||
await close();
|
||||
}
|
||||
});
|
||||
|
||||
test('mesh on Bun: 64 concurrent RPCs on one link all succeed (#148)', async () => {
|
||||
const { post, close, teacup } = await pair();
|
||||
try {
|
||||
// In one tick, through `app.fetch`: all 64 rpc-req frames leave together and land in one read,
|
||||
// which is the burst that tripped the 16-frame queue. Over HTTP sockets they trickle in.
|
||||
const rpc = () =>
|
||||
teacup.fetch(
|
||||
new Request('http://x/api/big/echo', {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: json(100),
|
||||
}),
|
||||
);
|
||||
const res = await Promise.all(Array.from({ length: 64 }, rpc));
|
||||
expect(res.map((r) => r.status)).toEqual(Array(64).fill(200));
|
||||
expect((await post(json(1_000))).status).toEqual(200);
|
||||
} finally {
|
||||
await close();
|
||||
}
|
||||
});
|
||||
|
||||
test('mesh on Bun: a user ws route on the same teapot still closes 1013 at maxWsQueue (#148)', async () => {
|
||||
const { close, tPort } = await pair();
|
||||
try {
|
||||
const code = await new Promise<number>((resolve) => {
|
||||
const ws = new WebSocket(`ws://127.0.0.1:${tPort}/api/big/stall`);
|
||||
ws.onopen = () => {
|
||||
for (let i = 0; i < 17; i++) ws.send(String(i));
|
||||
};
|
||||
ws.onclose = (e) => resolve(e.code);
|
||||
});
|
||||
expect(code).toEqual(1013);
|
||||
} finally {
|
||||
await close();
|
||||
}
|
||||
});
|
||||
@@ -24,6 +24,15 @@ class BigCtl {
|
||||
for await (const msg of inbound) yield `echo:${String(msg).length}`;
|
||||
})();
|
||||
}
|
||||
|
||||
// Never reads `inbound`: every frame stays queued, so the 17th closes 1013 (#107).
|
||||
@Ws('/stall')
|
||||
stall() {
|
||||
return (async function* () {
|
||||
await new Promise<never>(() => {});
|
||||
yield '';
|
||||
})();
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/api', controllers: [BigCtl] })
|
||||
class TeapotModule {}
|
||||
@@ -67,7 +76,7 @@ async function pair(maxBodyBytes?: number) {
|
||||
await c.server.shutdown();
|
||||
await t.server.shutdown();
|
||||
};
|
||||
return { post, close, tPort: t.port };
|
||||
return { post, close, tPort: t.port, teacup };
|
||||
}
|
||||
|
||||
const json = (bytes: number) => JSON.stringify({ data: 'x'.repeat(bytes - WRAP) });
|
||||
@@ -130,3 +139,40 @@ Deno.test('mesh on Deno: the control-channel cap follows a raised maxBodyBytes (
|
||||
await close();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test('mesh on Deno: 64 concurrent RPCs on one link all succeed (#148)', async () => {
|
||||
const { post, close, teacup } = await pair();
|
||||
try {
|
||||
// In one tick, through `app.fetch`: all 64 rpc-req frames leave together and land in one read,
|
||||
// which is the burst that tripped the 16-frame queue. Over HTTP sockets they trickle in.
|
||||
const rpc = () =>
|
||||
teacup.fetch(
|
||||
new Request('http://x/api/big/echo', {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: json(100),
|
||||
}),
|
||||
);
|
||||
const res = await Promise.all(Array.from({ length: 64 }, rpc));
|
||||
assertEquals(res.map((r) => r.status), Array(64).fill(200));
|
||||
assertEquals((await post(json(1_000))).status, 200);
|
||||
} finally {
|
||||
await close();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test('mesh on Deno: a user ws route on the same teapot still closes 1013 at maxWsQueue (#148)', async () => {
|
||||
const { close, tPort } = await pair();
|
||||
try {
|
||||
const code = await new Promise<number>((resolve) => {
|
||||
const ws = new WebSocket(`ws://127.0.0.1:${tPort}/api/big/stall`);
|
||||
ws.onopen = () => {
|
||||
for (let i = 0; i < 17; i++) ws.send(String(i));
|
||||
};
|
||||
ws.onclose = (e) => resolve(e.code);
|
||||
});
|
||||
assertEquals(code, 1013);
|
||||
} finally {
|
||||
await close();
|
||||
}
|
||||
});
|
||||
@@ -7,7 +7,8 @@
|
||||
// - a text body does not: every `"` or `\` doubles under JSON escaping, so a 999 KB text body
|
||||
// became a ~2 MB frame and the teapot hung up the whole channel with 1009 (T1b);
|
||||
// - a raised `maxBodyBytes` was capped at 1 MiB on the channel regardless (T4).
|
||||
// The Deno and Bun twins live in test/deno and test/bun.
|
||||
// Its queue bound had the same problem (#148): `maxWsQueue` (16) closed it 1013 under a burst of
|
||||
// RPCs, which Node's `ws` delivers in one go. The Deno and Bun twins live in test/deno and test/bun.
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import WebSocket from 'ws';
|
||||
import { createApp, Route, Post, Module, Ws, body, ctx } from '../../src/index';
|
||||
@@ -28,6 +29,15 @@ class BigCtl {
|
||||
for await (const msg of inbound) yield `echo:${String(msg).length}`;
|
||||
})();
|
||||
}
|
||||
|
||||
// Never reads `inbound`: every frame stays queued, so the 17th closes 1013 (#107).
|
||||
@Ws('/stall')
|
||||
stall() {
|
||||
return (async function* () {
|
||||
await new Promise<never>(() => {});
|
||||
yield '';
|
||||
})();
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/api', controllers: [BigCtl] })
|
||||
class TeapotModule {}
|
||||
@@ -53,7 +63,7 @@ async function pair(maxBodyBytes?: number) {
|
||||
await teacup.close();
|
||||
await teapot.close();
|
||||
};
|
||||
return { post, close, tPort };
|
||||
return { post, close, tPort, teacup };
|
||||
}
|
||||
|
||||
describe('mesh RPC body near maxBodyBytes (#120)', () => {
|
||||
@@ -114,4 +124,37 @@ describe('mesh RPC body near maxBodyBytes (#120)', () => {
|
||||
await close();
|
||||
}
|
||||
});
|
||||
it('64 concurrent RPCs on one link all succeed: the control channel has its own queue bound (#148)', async () => {
|
||||
const { post, close, teacup } = await pair();
|
||||
try {
|
||||
// In one tick, through `app.fetch`: all 64 rpc-req frames leave together and land in one read,
|
||||
// which is the burst that tripped the 16-frame queue. Over HTTP sockets they trickle in.
|
||||
const rpc = () =>
|
||||
teacup.fetch(
|
||||
new Request('http://x/api/big/echo', {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: json(100),
|
||||
}),
|
||||
);
|
||||
const res = await Promise.all(Array.from({ length: 64 }, rpc));
|
||||
expect(res.map((r) => r.status)).toEqual(Array(64).fill(200));
|
||||
expect((await post(json(1_000))).status).toBe(200);
|
||||
} finally {
|
||||
await close();
|
||||
}
|
||||
});
|
||||
|
||||
it('a user ws route on the same teapot still closes 1013 at maxWsQueue (#148)', async () => {
|
||||
const { close, tPort } = await pair();
|
||||
try {
|
||||
const ws = new WebSocket(`ws://127.0.0.1:${tPort}/api/big/stall`);
|
||||
await new Promise((resolve) => ws.once('open', resolve));
|
||||
const closed = new Promise<number>((resolve) => ws.once('close', (code) => resolve(code)));
|
||||
for (let i = 0; i < 17; i++) ws.send(String(i));
|
||||
expect(await closed).toBe(1013);
|
||||
} finally {
|
||||
await close();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user