I am reading Node.js Design Patterns. I am stuck in the understanding of the reactor pattern. I do not see any call stack here. I thought the call stack was one of the main parts of Node.js design. Can anyone please help me understand this diagram? Also, there is no callback queue.
The reactor design pattern is an event handling pattern for handling service requests delivered concurrently to a service handler by one or more inputs. The service handler then demultiplexes the incoming requests and dispatches them synchronously to the associated request handlers.
Event Loop checks the event queue, picks the event and provides result to its callback handler. The event loop send the response to the application by callback handler or stores another event in the event demultiplexer. Event loop will run continuously and check the event queue until event queue get empty.
libuv: libuv is a C library originally written for Node. js to abstract non-blocking I/O operations. Event-driven asynchronous I/O model is integrated. It allows the CPU and other resources to be used simultaneously while still performing I/O operations, thereby resulting in efficient use of resources and network.
Everything starts with the application, application makes requests and the event demultiplexer gathers those requests then forms queues, Event Queues. Event demultiplexer is run by libuv which is an asynchronous IO library that allows Node.js performs I/O.
In the diagram you see one event queue. actually there is not only 1 event queue, there are 7 basics queues. those queues have ascending priorities, the queue that highest priority checked first by the event loop.
Timers queue has the highest priority. setTimeout and setInterval functions get queued here. Once the events are done in this queue, or time is up, event loop passes those functions to call stack, in the diagram it is named execute handler.
Once one of the event queues are done, instead of jumping to next queue, event loop firstly will check 2 other queues which queues other micro tasks and process.nextTick functions. Then it will jump to next queue. this diagram will help u visualize the event loop.
If there are no events in the event queue or the Event Demultiplexer has no pending requests, the program will complete.
note:callback queue that you mentioned is event queue and call stack is execute handler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With