From 51cdc5f018dcd3faa0b17b4a6e25e77da25f218f Mon Sep 17 00:00:00 2001 From: Brian Botha Date: Wed, 31 Jan 2024 17:20:18 +1100 Subject: [PATCH] fix: added `readableChunkSize` parameter to config and defaulted the readable buffer to 4kb * Related: #83 [ci skip] --- package-lock.json | 48 +++++++++++++++++++++++++++++++++++++++++++++++ src/QUICStream.ts | 20 +++++--------------- src/config.ts | 2 ++ src/types.ts | 8 +++++++- 4 files changed, 62 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index 74417029..e439b2a2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1468,6 +1468,54 @@ "resolved": "https://registry.npmjs.org/@matrixai/logger/-/logger-3.1.2.tgz", "integrity": "sha512-nNliLCnbg6hGS2+gGtQfeeyVNJzOmvqz90AbrQsHPNiE08l3YsENL2JQt9d454SorD1Ud51ymZdDCkeMLWY93A==" }, + "node_modules/@matrixai/quic-darwin-arm64": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/@matrixai/quic-darwin-arm64/-/quic-darwin-arm64-1.1.4.tgz", + "integrity": "sha512-/Dho39T4sjbdOeC01F4rDesssQ9nvfdH57Ap95870LoY1KwXWh+c7cAtdv9FVma2R+9cqdKNkfsMFcS68Y15KA==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@matrixai/quic-darwin-x64": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/@matrixai/quic-darwin-x64/-/quic-darwin-x64-1.1.4.tgz", + "integrity": "sha512-soxYopXZjoHxSMvpoNEKQQS1uQcXK2aIuaJHDgZINz4UbeLW0NsoEqMgUJKXeWZNR+aaVxhL5fE5+5Ypwej80Q==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/@matrixai/quic-linux-x64": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/@matrixai/quic-linux-x64/-/quic-linux-x64-1.1.4.tgz", + "integrity": "sha512-LRKkVX++UZEj00XhfD0cPe4uFQJeHOBpQl+v6BIL7LSB9KxsmQRgbDh5mvxvWAwnh5EI51aSgn4auWEdHMDjTw==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/@matrixai/quic-win32-x64": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/@matrixai/quic-win32-x64/-/quic-win32-x64-1.1.4.tgz", + "integrity": "sha512-JNIQn1GRCRDm23v7hkqNVRm0oV928AHVfNPXJOB6FqBtbmbe/DaqUnppFi3qqUxm5Agguhd1rgNfKkL4HRhq8A==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "win32" + ] + }, "node_modules/@matrixai/resources": { "version": "1.1.5", "resolved": "https://registry.npmjs.org/@matrixai/resources/-/resources-1.1.5.tgz", diff --git a/src/QUICStream.ts b/src/QUICStream.ts index ef98b783..37a985b3 100644 --- a/src/QUICStream.ts +++ b/src/QUICStream.ts @@ -239,21 +239,11 @@ class QUICStream implements ReadableWritablePair { // This will setup the readable chunk buffer with the size set to the // configured per-stream buffer size. Note that this doubles the memory // usage of each stream due to maintaining both the Rust and JS buffers - if (this.type === 'uni') { - if (initiated === 'local') { - // We expect the readable stream to be closed - this.readableChunk = undefined; - } else if (initiated === 'peer') { - this.readableChunk = Buffer.allocUnsafe(config.initialMaxStreamDataUni); - } - } else if (this.type === 'bidi' && initiated === 'local') { - this.readableChunk = Buffer.allocUnsafe( - config.initialMaxStreamDataBidiLocal, - ); - } else if (this.type === 'bidi' && initiated === 'peer') { - this.readableChunk = Buffer.allocUnsafe( - config.initialMaxStreamDataBidiRemote, - ); + if (this.type === 'uni' && initiated === 'local') { + // We expect the readable stream to be closed + this.readableChunk = undefined; + } else { + this.readableChunk = Buffer.allocUnsafe(config.readableChunkSize); } if (this.type === 'uni' && initiated === 'local') { // This is just a dummy stream that will be auto-closed during creation diff --git a/src/config.ts b/src/config.ts index 81d4c57e..43ac5ac6 100644 --- a/src/config.ts +++ b/src/config.ts @@ -61,6 +61,7 @@ const clientDefault: QUICConfig = { disableActiveMigration: true, applicationProtos: ['quic'], enableEarlyData: true, + readableChunkSize: 4 * 1024, }; const serverDefault: QUICConfig = { @@ -83,6 +84,7 @@ const serverDefault: QUICConfig = { disableActiveMigration: true, applicationProtos: ['quic'], enableEarlyData: true, + readableChunkSize: 4 * 1024, }; /** diff --git a/src/types.ts b/src/types.ts index 851b37a1..4cdd4d18 100644 --- a/src/types.ts +++ b/src/types.ts @@ -177,7 +177,7 @@ type QUICConfig = { logKeys?: string; /** - * Enable "Generate Random extensions and Sustain Extensibilty". + * Enable "Generate Random extensions and Sustain Extensibility". * This prevents protocol ossification by periodically introducing * random no-op values in the optional fields in TLS. * This defaults to true. @@ -305,6 +305,12 @@ type QUICConfig = { applicationProtos: string[]; enableEarlyData: boolean; + + /** + * Defines the size of the Buffer used to read out data from the readable stream. + * This affects amount of memory reserved by the stream. + */ + readableChunkSize: number; }; type QUICClientConfigInput = Partial;