Skip to content

Commit 77e0318

Browse files
ronagBethGriggs
authored andcommitted
stream: increase MAX_HWM
MAX_HWM was added in 9208c89 where the highwatermark was changed to always increase in steps of highest power of 2 to prevent increasing hwm excessivly in tiny amounts. Why a limit was added on the highwatermark is unclear but breaks existing usage where a larger read size is used. The invariant for read(n) is that a buffer of size n is always returned. Considering a maximum ceiling on the buffer size breaks this invariant. This PR significantly increases the limit to make it less likely to break the previous invariant and also documents the limit. Fixes: #29933 PR-URL: #29938 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent a1b095d commit 77e0318

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

doc/api/stream.md

+2
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,8 @@ buffer will be returned.
10101010
If the `size` argument is not specified, all of the data contained in the
10111011
internal buffer will be returned.
10121012

1013+
The `size` argument must be less than or equal to 1 GB.
1014+
10131015
The `readable.read()` method should only be called on `Readable` streams
10141016
operating in paused mode. In flowing mode, `readable.read()` is called
10151017
automatically until the internal buffer is fully drained.

lib/_stream_readable.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,11 @@ Readable.prototype.setEncoding = function(enc) {
340340
return this;
341341
};
342342

343-
// Don't raise the hwm > 8MB
344-
const MAX_HWM = 0x800000;
343+
// Don't raise the hwm > 1GB
344+
const MAX_HWM = 0x40000000;
345345
function computeNewHighWaterMark(n) {
346346
if (n >= MAX_HWM) {
347+
// TODO(ronag): Throw ERR_VALUE_OUT_OF_RANGE.
347348
n = MAX_HWM;
348349
} else {
349350
// Get the next highest power of 2 to prevent increasing hwm excessively in
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { Readable } = require('stream');
4+
5+
// Make sure that readable completes
6+
// even when reading larger buffer.
7+
const bufferSize = 10 * 1024 * 1024;
8+
let n = 0;
9+
const r = new Readable({
10+
read() {
11+
// Try to fill readable buffer piece by piece.
12+
r.push(Buffer.alloc(bufferSize / 10));
13+
14+
if (n++ > 10) {
15+
r.push(null);
16+
}
17+
}
18+
});
19+
20+
r.on('readable', () => {
21+
while (true) {
22+
const ret = r.read(bufferSize);
23+
if (ret === null)
24+
break;
25+
}
26+
});
27+
r.on('end', common.mustCall());

0 commit comments

Comments
 (0)