Node.JS Unbounded Concurrency / Stream backpressure over TCP

后端 未结 1 389
我在风中等你
我在风中等你 2021-02-04 11:10

As I understand it, one of the consequences of Node\'s evented IO model is the inability to tell a Node process that is (for example) receiving data over a TCP socket, to block,

1条回答
  •  鱼传尺愫
    2021-02-04 12:10

    To answer your first question, I believe your understanding is not accurate -- at least not when piping data between streams. In fact, if you read the documentation for the pipe() function you'll see that it explicitly says that it automatically manages the flow so that "destination is not overwhelmed by a fast readable stream."

    The underlying implementation of pipe() is taking care of all of the heavy lifting for you. The input stream (a Readable stream) will continue to emit data events until the output stream (a Writable stream) is full. As an aside, if I remember correctly, the stream will return false when you attempt to write data that it cannot currently process. At this point, the pipe will pause() the Readable stream, which will prevent it from emitting further data events. Thus, the event loop isn't going to fill up and exhaust your memory nor is it going to emit events that are simply lost. Instead, the Readable will stay paused until the Writable stream emits a drain event. At that point, the pipe will resume() the Readable stream.

    The secret sauce is piping one stream into another, which is managing the back pressure for you automatically. This hopefully answers your second question, which is that Node can and does automatically manage this by simply piping streams.

    And finally, there is really no need to implement this manually (unless you are writing a new stream from scratch) since it is already provided for you. :)

    Handling all of this is not easy, as admitted on the Node blog post that announced the streams2 API in Node. It's a great resource and certainly provides much more information than I could here. One little gotcha that isn't entirely obvious that you should know however, from the docs here and for backwards compatibility reasons:

    If you attach a data event listener, then it will switch the stream into flowing mode, and data will be passed to your handler as soon as it is available.

    So just be aware that attaching the data event listener in an attempt to observe something in the stream will fundamentally alter the stream to the old way of doing things. Ask me how I know.

    0 讨论(0)
提交回复
热议问题