diff options
author | isaacs <i@izs.me> | 2013-03-09 10:56:17 -0800 |
---|---|---|
committer | isaacs <i@izs.me> | 2013-03-10 11:04:48 -0700 |
commit | cd2b9f542c34ce59d2df7820a6237f7911c702f3 (patch) | |
tree | 8be4feda3f05e696fe0b716ae24f4d6edec7f1af /lib/_stream_readable.js | |
parent | 738347b90433a38538ab298bf38860e83a4abc53 (diff) | |
download | node-cd2b9f542c34ce59d2df7820a6237f7911c702f3.tar.gz |
stream: Avoid nextTick warning filling read buffer
In the function that pre-emptively fills the Readable queue, it relies
on a recursion through:
stream.push(chunk) ->
maybeReadMore(stream, state) ->
if (not reading more and < hwm) stream.read(0) ->
stream._read() ->
stream.push(chunk) -> repeat.
Since this was only calling read() a single time, and then relying on a
future nextTick to collect more data, it ends up causing a nextTick
recursion error (and potentially a RangeError, even) if you have a very
high highWaterMark, and are getting very small chunks pushed
synchronously in _read (as happens with TLS, or many simple test
streams).
This change implements a new approach, so that read(0) is called
repeatedly as long as it is effective (that is, the length keeps
increasing), and thus quickly fills up the buffer for streams such as
these, without any stacks overflowing.
Diffstat (limited to 'lib/_stream_readable.js')
-rw-r--r-- | lib/_stream_readable.js | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index d6ef327a8..41cf8e89c 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -386,17 +386,23 @@ function maybeReadMore(stream, state) { if (!state.readingMore) { state.readingMore = true; process.nextTick(function() { - state.readingMore = false; maybeReadMore_(stream, state); }); } } function maybeReadMore_(stream, state) { - if (!state.reading && !state.flowing && !state.ended && - state.length < state.highWaterMark) { + var len = state.length; + while (!state.reading && !state.flowing && !state.ended && + state.length < state.highWaterMark) { stream.read(0); + if (len === state.length) + // didn't get any data, stop spinning. + break; + else + len = state.length; } + state.readingMore = false; } // abstract method. to be overridden in specific implementation classes. |