Skip to content

Commit b5625f6

Browse files
ronagtargos
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 60a0f1e commit b5625f6

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
@@ -1080,6 +1080,8 @@ buffer will be returned.
10801080
If the `size` argument is not specified, all of the data contained in the
10811081
internal buffer will be returned.
10821082

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

lib/_stream_readable.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,11 @@ Readable.prototype.setEncoding = function(enc) {
358358
return this;
359359
};
360360

361-
// Don't raise the hwm > 8MB
362-
const MAX_HWM = 0x800000;
361+
// Don't raise the hwm > 1GB
362+
const MAX_HWM = 0x40000000;
363363
function computeNewHighWaterMark(n) {
364364
if (n >= MAX_HWM) {
365+
// TODO(ronag): Throw ERR_VALUE_OUT_OF_RANGE.
365366
n = MAX_HWM;
366367
} else {
367368
// 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)