Skip to content

Commit 21388e4

Browse files
authored
fix(minidb): buffer large RESP requests in linear time (#393)
## Requirement or Bug `test-minidb` times out on slower CI runners in `RESP: an oversized request gets -ERR and the connection recovers` (20 s, 0 bytes back). It failed twice on #392, which does not touch minidb. ## Bug Reproduction Steps 1. Start the minidb RESP server. 2. Send one `SET` whose bulk value is 65 MB (over the 64 MB request cap), then `PING`. 3. The `-ERR ... too large` reply takes about 2.4 s on an M5 Max and more than 20 s on a loaded CI runner. ## Root Cause `RespParser.feed` joined each incoming socket chunk onto the whole buffered request (`Buffer.concat([this.buf, chunk])`). A 65 MB request arrives in about a thousand chunks, so the parser copied tens of gigabytes before it reached the cap. This is a fix at the root, not a timeout increase. ## Code Changes ```diff RespParser.feed(chunk) - buf = concat(buf, chunk) every chunk + pending.push(chunk); size += chunk.length + size < need -> return bulk string still incomplete + buf = concat(buf, ...pending) once RespParser.tryParse() + bulk string incomplete -> need = bytes required ``` The oversized-request check and its buffer reset do not change; they now count `size`, which includes the pending chunks. ## Behavior Changes and Affected Users | Behavior | Before | After | Who relies on the old behavior | Escape hatch | |---|---|---|---|---| | RESP request larger than one chunk | Buffer copied on every chunk | Chunks joined once the bulk string can complete | Nobody | n/a | Tests: oversized-request test 2381 ms before, 48 ms after (local). New test `RESP: a bulk value split across many small writes is reassembled` covers the split path. minidb suite 554 passed; `tsc` clean; `pnpm run lint` 0 errors. [skip changeset] Users cannot see this change: it only changes how fast the RESP server buffers large requests. ## Checklist - [x] I have read the [CONTRIBUTING](https://github.com/PyModel/pythinker-code/blob/main/CONTRIBUTING.md) document. - [ ] I have linked a related issue (external PRs: issue must have a maintainer's `/approve`). - [x] I have added tests that prove my feature works. - [x] The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR. - [x] Ran `gen-changesets` skill, or this PR needs no changeset. - [x] Ran `gen-docs` skill, or this PR needs no doc update.
1 parent 2c087e3 commit 21388e4

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎packages/minidb/src/server.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,38 @@ const reply = {
3333

3434
class RespParser {
3535
private buf: Buffer = Buffer.alloc(0);
36+
// Chunks received while a bulk string is still incomplete. Joining them only
37+
// once enough bytes are in keeps a large request linear instead of copying
38+
// the whole buffer on every chunk.
39+
private pending: Buffer[] = [];
40+
private size = 0;
41+
private need = 0;
3642
private readonly maxBuf: number;
3743

3844
constructor({ maxBuf = 64 * 1024 * 1024 }: { maxBuf?: number } = {}) {
3945
this.maxBuf = maxBuf;
4046
}
4147

4248
*feed(chunk: Buffer): Generator<Buffer[]> {
43-
this.buf = this.buf.length ? Buffer.concat([this.buf, chunk]) : chunk;
44-
if (this.buf.length > this.maxBuf) {
49+
this.pending.push(chunk);
50+
this.size += chunk.length;
51+
if (this.size > this.maxBuf) {
4552
// Drop the buffered oversized request before reporting: without the
4653
// reset every later chunk would fail with the same error and the giant
4754
// buffer would be retained for the life of the connection.
4855
this.buf = Buffer.alloc(0);
56+
this.pending = [];
57+
this.size = 0;
58+
this.need = 0;
4959
throw new Error(`RESP request too large (>${this.maxBuf} bytes)`);
5060
}
61+
if (this.size < this.need) return;
62+
this.buf = this.buf.length || this.pending.length > 1 ? Buffer.concat([this.buf, ...this.pending]) : chunk;
63+
this.pending = [];
64+
this.need = 0;
5165
while (this.buf.length) {
5266
const parsed = this.tryParse();
67+
this.size = this.buf.length;
5368
if (!parsed) break;
5469
yield parsed;
5570
}
@@ -78,7 +93,10 @@ class RespParser {
7893
if (end === -1) return null;
7994
const len = Number(this.buf.subarray(pos, end).toString());
8095
pos = end + 2;
81-
if (this.buf.length - pos < len + 2) return null;
96+
if (this.buf.length - pos < len + 2) {
97+
this.need = pos + len + 2;
98+
return null;
99+
}
82100
args.push(this.buf.subarray(pos, pos + len));
83101
pos += len + 2;
84102
}

‎packages/minidb/test/server-extra.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,27 @@ test('RESP: an oversized request gets -ERR and the connection recovers', { timeo
203203
}
204204
});
205205

206+
test('RESP: a bulk value split across many small writes is reassembled', async () => {
207+
const dir = await tmpDir();
208+
const srv = await startServer({ dir, port: 0, fsyncPolicy: 'no' });
209+
try {
210+
const sock = await connect(srv.port);
211+
sock.setNoDelay(true);
212+
const value = 'abcdefghij'.repeat(50);
213+
const wire = Buffer.from(encode('SET', 'split', value) + encode('GET', 'split'));
214+
const reply = collectUntil(sock, (s) => s.endsWith(`$${value.length}\r\n${value}\r\n`));
215+
for (let i = 0; i < wire.length; i += 7) {
216+
sock.write(wire.subarray(i, i + 7));
217+
await new Promise((r) => setImmediate(r));
218+
}
219+
assert.equal(await reply, `+OK\r\n$${value.length}\r\n${value}\r\n`);
220+
sock.end();
221+
} finally {
222+
await srv.close();
223+
await fs.rm(dir, { recursive: true, force: true });
224+
}
225+
});
226+
206227
test('RESP: one bad command does not starve its pipelined siblings', async () => {
207228
const dir = await tmpDir();
208229
const srv = await startServer({ dir, port: 0, fsyncPolicy: 'no' });

0 commit comments

Comments
 (0)