Skip to content

Commit f8c069f

Browse files
ronagTrott
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 <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent e55d653 commit f8c069f

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

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

lib/_stream_readable.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,11 @@ Readable.prototype.setEncoding = function(enc) {
375375
return this;
376376
};
377377

378-
// Don't raise the hwm > 8MB
379-
const MAX_HWM = 0x800000;
378+
// Don't raise the hwm > 1GB
379+
const MAX_HWM = 0x40000000;
380380
function computeNewHighWaterMark(n) {
381381
if (n >= MAX_HWM) {
382+
// TODO(ronag): Throw ERR_VALUE_OUT_OF_RANGE.
382383
n = MAX_HWM;
383384
} else {
384385
// 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)