Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

relay: pin the 4 KiB first-message boundary, stop the throttle test's pump on failure, spread load-test sends

milind-soni0864cbce42bf
diff --git a/relay/load/load.ts b/relay/load/load.ts
index 12b3a0604e..1d4bbd1ad5 100644
--- a/relay/load/load.ts
+++ b/relay/load/load.ts
@@ -146,7 +146,7 @@ async function runWorker(input: WorkerInput): Promise<WorkerReport> {
}
const tickMs = 20;
const perTick = (input.bytesPerSecondEachWay * tickMs) / 1000 / Math.max(1, sessions.length);
- let owed = 0;
+ const owed = sessions.map(() => Math.random() * input.size);
const stopAt = performance.now() + input.seconds * 1000;
await new Promise<void>((resolve) => {
const timer = setInterval(() => {
@@ -155,10 +155,10 @@ async function runWorker(input: WorkerInput): Promise<WorkerReport> {
resolve();
return;
}
- owed += perTick;
- const messages = Math.floor(owed / input.size);
- owed -= messages * input.size;
- for (const { phone, data } of sessions) {
+ sessions.forEach(({ phone, data }, i) => {
+ owed[i]! += perTick;
+ const messages = Math.floor(owed[i]! / input.size);
+ owed[i]! -= messages * input.size;
for (let m = 0; m < messages; m++) {
for (const ws of [phone, data]) {
if (ws.readyState !== WebSocket.OPEN || ws.bufferedAmount > 4 * 1024 * 1024) continue;
@@ -168,7 +168,7 @@ async function runWorker(input: WorkerInput): Promise<WorkerReport> {
report.sent += message.length;
}
}
- }
+ });
}, tickMs);
});
await new Promise((resolve) => setTimeout(resolve, 1_000));
diff --git a/relay/test/fuzz.test.ts b/relay/test/fuzz.test.ts
index 1502a9e192..94eeb31818 100644
--- a/relay/test/fuzz.test.ts
+++ b/relay/test/fuzz.test.ts
@@ -115,6 +115,21 @@ describe("pre-auth fuzzing", () => {
for (const code of [Close.Malformed, 1009]) assert.ok(seen.has(code), `never saw ${code} in ${JSON.stringify([...seen])}`);
});
+ test("the first message may be 4,096 bytes and not one more, even when it is otherwise valid", async () => {
+ const sized = (bytes: number) => {
+ const message = { t: "auth", v: 1, token: mintToken(t.key), pad: "" };
+ message.pad = "x".repeat(bytes - Buffer.byteLength(JSON.stringify(message)));
+ return JSON.stringify(message);
+ };
+ const fits = await Client.open(t.url, "/v1/host");
+ fits.send(sized(4_096));
+ assert.equal((await fits.json()).t, "hello");
+ fits.ws.close();
+ const over = await Client.open(t.url, "/v1/host");
+ over.send(sized(4_097));
+ assert.equal(await over.closeCode(), Close.Malformed);
+ });
+
test("the expected code for each kind of bad first frame", async () => {
const cases: Array<[string, (client: Client) => void, number]> = [
["binary", (c) => c.sendBinary(Buffer.from("{}")), Close.Malformed],
diff --git a/relay/test/splice.test.ts b/relay/test/splice.test.ts
index a471d0c109..fdd24ef091 100644
--- a/relay/test/splice.test.ts
+++ b/relay/test/splice.test.ts
@@ -19,14 +19,18 @@ describe("limits that need a short clock", () => {
const pump = setInterval(() => {
if (phone.ws.bufferedAmount < 4 * MiB) for (let i = 0; i < 16; i++) phone.sendBinary(randomBytes(60_000));
}, 5);
- const stopAt = performance.now() + 6_000;
- while (t.relay.ctx.metrics.count("relay_throttled_sessions_total") === 0 && performance.now() < stopAt) await sleep(20);
- assert.equal(t.relay.ctx.metrics.count("relay_throttled_sessions_total"), 1);
- await sleep(300);
- const before = bytes;
- await sleep(2_000);
- const rate = ((bytes - before) * 8) / 2;
- clearInterval(pump);
+ let rate: number;
+ try {
+ const stopAt = performance.now() + 6_000;
+ while (t.relay.ctx.metrics.count("relay_throttled_sessions_total") === 0 && performance.now() < stopAt) await sleep(20);
+ assert.equal(t.relay.ctx.metrics.count("relay_throttled_sessions_total"), 1);
+ await sleep(300);
+ const before = bytes;
+ await sleep(2_000);
+ rate = ((bytes - before) * 8) / 2;
+ } finally {
+ clearInterval(pump);
+ }
assert.ok(rate < throttleBps * 1.5, `${Math.round(rate / 1e6)} Mbit/s after the throttle`);
assert.ok(rate > throttleBps * 0.3, `${Math.round(rate / 1e6)} Mbit/s: the session must keep moving`);
assert.ok(t.logs.some((event) => event.ev === "throttle"));