Skip to content

Commit f54d3f2

Browse files
committed
feat: maxReadableStreamBytes config option for readable stream in QUICStream
* Related #5 [ci skip]
1 parent 844f65b commit f54d3f2

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

src/QUICClient.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class QUICClient extends EventTarget {
5555
resolveHostname = utils.resolveHostname,
5656
reasonToCode,
5757
codeToReason,
58+
maxReadableStreamBytes,
5859
logger = new Logger(`${this.name}`),
5960
config = {},
6061
}: {
@@ -70,6 +71,7 @@ class QUICClient extends EventTarget {
7071
resolveHostname?: (hostname: Hostname) => Host | PromiseLike<Host>;
7172
reasonToCode?: StreamReasonToCode;
7273
codeToReason?: StreamCodeToReason;
74+
maxReadableStreamBytes?: number;
7375
logger?: Logger;
7476
config?: Partial<QUICConfig>;
7577
}) {
@@ -153,10 +155,9 @@ class QUICClient extends EventTarget {
153155
config: quicConfig,
154156
reasonToCode,
155157
codeToReason,
158+
maxReadableStreamBytes,
156159
logger: logger.getChild(
157-
`${QUICConnection.name} ${scid.toString().slice(32)}-${Math.floor(
158-
Math.random() * 100,
159-
)}`,
160+
`${QUICConnection.name} ${scid.toString().slice(32)}`,
160161
),
161162
});
162163
connection.addEventListener('error', handleConnectionError, { once: true });

src/QUICConnection.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class QUICConnection extends EventTarget {
4343
public streamMap: Map<StreamId, QUICStream> = new Map();
4444
protected reasonToCode: StreamReasonToCode;
4545
protected codeToReason: StreamCodeToReason;
46+
protected maxReadableStreamBytes: number | undefined;
4647
protected destroyingMap: Map<StreamId, QUICStream> = new Map();
4748

4849
// This basically allows one to await this promise
@@ -102,6 +103,7 @@ class QUICConnection extends EventTarget {
102103
reasonToCode = () => 0,
103104
codeToReason = (type, code) =>
104105
new Error(`${type.toString()} ${code.toString()}`),
106+
maxReadableStreamBytes,
105107
logger = new Logger(`${this.name} ${scid}`),
106108
}: {
107109
scid: QUICConnectionId;
@@ -110,6 +112,7 @@ class QUICConnection extends EventTarget {
110112
config: QUICConfig;
111113
reasonToCode?: StreamReasonToCode;
112114
codeToReason?: StreamCodeToReason;
115+
maxReadableStreamBytes?: number;
113116
logger?: Logger;
114117
}) {
115118
logger.info(`Connect ${this.name}`);
@@ -139,6 +142,7 @@ class QUICConnection extends EventTarget {
139142
remoteInfo,
140143
reasonToCode,
141144
codeToReason,
145+
maxReadableStreamBytes,
142146
logger,
143147
});
144148
socket.connectionMap.set(connection.connectionId, connection);
@@ -158,6 +162,7 @@ class QUICConnection extends EventTarget {
158162
reasonToCode = () => 0,
159163
codeToReason = (type, code) =>
160164
new Error(`${type.toString()} ${code.toString()}`),
165+
maxReadableStreamBytes,
161166
logger = new Logger(`${this.name} ${scid}`),
162167
}: {
163168
scid: QUICConnectionId;
@@ -167,6 +172,7 @@ class QUICConnection extends EventTarget {
167172
config: QUICConfig;
168173
reasonToCode?: StreamReasonToCode;
169174
codeToReason?: StreamCodeToReason;
175+
maxReadableStreamBytes?: number;
170176
logger?: Logger;
171177
}): Promise<QUICConnection> {
172178
logger.info(`Accept ${this.name}`);
@@ -196,6 +202,7 @@ class QUICConnection extends EventTarget {
196202
remoteInfo,
197203
reasonToCode,
198204
codeToReason,
205+
maxReadableStreamBytes,
199206
logger,
200207
});
201208
socket.connectionMap.set(connection.connectionId, connection);
@@ -211,6 +218,7 @@ class QUICConnection extends EventTarget {
211218
remoteInfo,
212219
reasonToCode,
213220
codeToReason,
221+
maxReadableStreamBytes,
214222
logger,
215223
}: {
216224
type: 'client' | 'server';
@@ -220,6 +228,7 @@ class QUICConnection extends EventTarget {
220228
remoteInfo: RemoteInfo;
221229
reasonToCode: StreamReasonToCode;
222230
codeToReason: StreamCodeToReason;
231+
maxReadableStreamBytes: number | undefined;
223232
logger: Logger;
224233
}) {
225234
super();
@@ -233,6 +242,7 @@ class QUICConnection extends EventTarget {
233242
this._remotePort = remoteInfo.port;
234243
this.reasonToCode = reasonToCode;
235244
this.codeToReason = codeToReason;
245+
this.maxReadableStreamBytes = maxReadableStreamBytes;
236246
// Sets the timeout on the first
237247
this.checkTimeout();
238248

@@ -434,11 +444,8 @@ class QUICConnection extends EventTarget {
434444
destroyingMap: this.destroyingMap,
435445
codeToReason: this.codeToReason,
436446
reasonToCode: this.reasonToCode,
437-
logger: this.logger.getChild(
438-
`${QUICStream.name} ${streamId}-${Math.floor(
439-
Math.random() * 100,
440-
)}`,
441-
),
447+
maxReadableStreamBytes: this.maxReadableStreamBytes,
448+
logger: this.logger.getChild(`${QUICStream.name} ${streamId}`),
442449
});
443450
this.dispatchEvent(
444451
new events.QUICConnectionStreamEvent({ detail: quicStream }),
@@ -456,11 +463,8 @@ class QUICConnection extends EventTarget {
456463
codeToReason: this.codeToReason,
457464
reasonToCode: this.reasonToCode,
458465
destroyingMap: this.destroyingMap,
459-
logger: this.logger.getChild(
460-
`${QUICStream.name} ${streamId}-${Math.floor(
461-
Math.random() * 100,
462-
)}`,
463-
),
466+
maxReadableStreamBytes: this.maxReadableStreamBytes,
467+
logger: this.logger.getChild(`${QUICStream.name} ${streamId}`),
464468
});
465469
this.dispatchEvent(
466470
new events.QUICConnectionStreamEvent({ detail: quicStream }),
@@ -659,9 +663,8 @@ class QUICConnection extends EventTarget {
659663
codeToReason: this.codeToReason,
660664
reasonToCode: this.reasonToCode,
661665
destroyingMap: this.destroyingMap,
662-
logger: this.logger.getChild(
663-
`${QUICStream.name} ${streamId!}-${Math.floor(Math.random() * 100)}`,
664-
),
666+
maxReadableStreamBytes: this.maxReadableStreamBytes,
667+
logger: this.logger.getChild(`${QUICStream.name} ${streamId!}`),
665668
});
666669
// Ok the stream is opened and working
667670
if (this.type === 'client' && streamType === 'bidi') {

src/QUICServer.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class QUICServer extends EventTarget {
3838
protected socket: QUICSocket;
3939
protected reasonToCode: StreamReasonToCode | undefined;
4040
protected codeToReason: StreamCodeToReason | undefined;
41+
protected maxReadableStreamBytes?: number | undefined;
4142
protected connectionMap: QUICConnectionMap;
4243

4344
/**
@@ -62,6 +63,7 @@ class QUICServer extends EventTarget {
6263
resolveHostname = utils.resolveHostname,
6364
reasonToCode,
6465
codeToReason,
66+
maxReadableStreamBytes,
6567
logger,
6668
}: {
6769
crypto: {
@@ -76,6 +78,7 @@ class QUICServer extends EventTarget {
7678
resolveHostname?: (hostname: Hostname) => Host | PromiseLike<Host>;
7779
reasonToCode?: StreamReasonToCode;
7880
codeToReason?: StreamCodeToReason;
81+
maxReadableStreamBytes?: number;
7982
logger?: Logger;
8083
}) {
8184
super();
@@ -103,6 +106,7 @@ class QUICServer extends EventTarget {
103106
this.config = quicConfig;
104107
this.reasonToCode = reasonToCode;
105108
this.codeToReason = codeToReason;
109+
this.maxReadableStreamBytes = maxReadableStreamBytes;
106110
}
107111

108112
@ready(new errors.ErrorQUICServerNotRunning())
@@ -276,10 +280,9 @@ class QUICServer extends EventTarget {
276280
config: this.config,
277281
reasonToCode: this.reasonToCode,
278282
codeToReason: this.codeToReason,
283+
maxReadableStreamBytes: this.maxReadableStreamBytes,
279284
logger: this.logger.getChild(
280-
`${QUICConnection.name} ${scid.toString().slice(32)}-${Math.floor(
281-
Math.random() * 100,
282-
)}`,
285+
`${QUICConnection.name} ${scid.toString().slice(32)}`,
283286
),
284287
});
285288

src/QUICStream.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,15 @@ class QUICStream
6969
reasonToCode = () => 0,
7070
codeToReason = (type, code) =>
7171
new Error(`${type.toString()} ${code.toString()}`),
72+
maxReadableStreamBytes = 1_000_000, // About 1KB
7273
logger = new Logger(`${this.name} ${streamId}`),
7374
}: {
7475
streamId: StreamId;
7576
connection: QUICConnection;
7677
destroyingMap: Map<StreamId, QUICStream>;
7778
reasonToCode?: StreamReasonToCode;
7879
codeToReason?: StreamCodeToReason;
80+
maxReadableStreamBytes?: number;
7981
logger?: Logger;
8082
}): Promise<QUICStream> {
8183
logger.info(`Create ${this.name}`);
@@ -92,6 +94,7 @@ class QUICStream
9294
reasonToCode,
9395
codeToReason,
9496
destroyingMap,
97+
maxReadableStreamBytes,
9598
logger,
9699
});
97100
connection.streamMap.set(stream.streamId, stream);
@@ -105,13 +108,15 @@ class QUICStream
105108
reasonToCode,
106109
codeToReason,
107110
destroyingMap,
111+
maxReadableStreamBytes,
108112
logger,
109113
}: {
110114
streamId: StreamId;
111115
connection: QUICConnection;
112116
reasonToCode: StreamReasonToCode;
113117
codeToReason: StreamCodeToReason;
114118
destroyingMap: Map<StreamId, QUICStream>;
119+
maxReadableStreamBytes: number;
115120
logger: Logger;
116121
}) {
117122
super();
@@ -124,8 +129,6 @@ class QUICStream
124129
this.codeToReason = codeToReason;
125130
this.destroyingMap = destroyingMap;
126131

127-
// Try the BYOB later, it seems more performant
128-
129132
this.readable = new ReadableStream(
130133
{
131134
type: 'bytes',
@@ -142,7 +145,8 @@ class QUICStream
142145
},
143146
},
144147
{
145-
highWaterMark: 20,
148+
highWaterMark: maxReadableStreamBytes,
149+
size: (chunk) => chunk?.byteLength ?? 0,
146150
},
147151
);
148152

0 commit comments

Comments
 (0)