Is nodejs representing Reactor or Proactor design pattern?

前端 未结 2 1382
北恋
北恋 2020-12-20 07:52

Many articles online demonstrates nodejs as an example of reactor pattern. Isn\'t it rather proactor?

As far as I understand, the difference between the two is:

2条回答
  •  清酒与你
    2020-12-20 08:35

    The distinction has nothing to do with multithreading. It is as follows:

    • Reactor - I want to read from a socket, so I subscribe to a data-is-available kind of event and, and when it fires react to it by synchronously reading the available amount.
    • Proactor - I want to read from a socket, so I initiate a reading operation (one that proactively reads the data, without waiting for me to react to it's availability), and subscribe to some kind of read-is-complete notification, wherein the read data is immediately available to me.

    Node has both kinds of APIs, e.g. stream.ReadableStream#readable/stream.ReadableStream#read are a Reactor interface, while fs.readFile is a Proactor.

提交回复
热议问题