diff --git a/orchestrator/src/files.test.ts b/orchestrator/src/files.test.ts index c192914..5633b91 100644 --- a/orchestrator/src/files.test.ts +++ b/orchestrator/src/files.test.ts @@ -1,5 +1,5 @@ import assert from 'node:assert/strict'; -import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'; import { createServer, type IncomingHttpHeaders, @@ -130,6 +130,19 @@ test('a read is tried once more when the connection ended during it', async () = assert.equal(counter.spawns, 2); }); +test('a small file is stored while a large file is sent on the same connection', async () => { + setFileServiceSpawnerForTests(real); + const files = fileService('c1'); + await files.list(''); + const large = files.create('', 'large.bin', Buffer.alloc(14 * 1024 * 1024, 1)); + // The small file starts when the large file is in the connection. + await new Promise((resolve) => setImmediate(resolve)); + const small = files.create('', 'small.bin', Buffer.alloc(1024 * 1024, 1)); + assert.deepEqual(await Promise.all([large, small]), ['large.bin', 'small.bin']); + assert.equal(statSync(join(workspace, 'large.bin')).size, 14 * 1024 * 1024); + assert.equal(statSync(join(workspace, 'small.bin')).size, 1024 * 1024); +}); + test('a write is not tried again', async () => { const { counter, spawner } = counted(dropping, real); setFileServiceSpawnerForTests(spawner); diff --git a/orchestrator/src/files.ts b/orchestrator/src/files.ts index 12bd20e..b730879 100644 --- a/orchestrator/src/files.ts +++ b/orchestrator/src/files.ts @@ -43,6 +43,9 @@ const REQUEST_TIMEOUT_MS = 30_000; /** How long the walk for the disk usage may take, in milliseconds. It matches a short command in the box. */ const SIZE_TIMEOUT_MS = RUN_TIMEOUT_MS; +/** The size of one piece of a request body that is a buffer, in bytes. */ +const BODY_PIECE = 64 * 1024; + /** The most bytes of a JSON reply. A full directory listing fits. */ const MAX_JSON_REPLY = 4 * 1024 * 1024; @@ -227,7 +230,7 @@ interface Request { query?: Record; /** Extra request headers. */ headers?: OutgoingHttpHeaders; - /** The body. A stream is piped, so it is never held in memory as a whole. */ + /** The body. A stream is piped, so it is never held in memory as a whole. A buffer is sent in pieces. */ body?: string | Buffer | Readable; /** The timeout in milliseconds. REQUEST_TIMEOUT_MS applies when it is absent. */ timeoutMs?: number; @@ -602,10 +605,13 @@ export class FileService { }); stream.on('error', (err) => reject(err instanceof TimedOut ? err : new ConnectionLost())); stream.on('close', () => reject(new ConnectionLost())); - if (req.body instanceof Readable) { - pipeline(req.body, stream).catch(() => {}); + // The session refuses new requests while it holds more than its memory limit, so a + // buffer goes to the session in pieces, as fast as the file service reads them. + const body = Buffer.isBuffer(req.body) ? Readable.from(pieces(req.body)) : req.body; + if (body instanceof Readable) { + pipeline(body, stream).catch(() => {}); } else { - stream.end(req.body); + stream.end(body); } }); } catch (err) { @@ -706,6 +712,18 @@ export class FileService { } } +/** + * Cuts a buffer into pieces of at most {@link BODY_PIECE} bytes. The pieces share the memory of the buffer. + * + * @param bytes The buffer. + * @returns The pieces, in order. + */ +function* pieces(bytes: Buffer): Generator { + for (let start = 0; start < bytes.length; start += BODY_PIECE) { + yield bytes.subarray(start, start + BODY_PIECE); + } +} + /** * Returns the body of a reply stream, as a stream of its own. *