Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Node Js use libuv Thread Pool for Network I/O

Tags:

node.js

libuv

I am learning Node.js

I have found this note on libuv official documentation-

libuv uses a thread pool to make asynchronous file I/O operations possible, but network I/O is always performed in a single thread, each loop’s thread.”

My question is for below statement(from an unofficial resource) -

"Today’s operating systems already provide asynchronous interfaces for many I/O tasks (e.g. AIO on Linux). Whenever possible, libuv will use those asynchronous interfaces, avoiding usage of the thread pool."

-- is this Statement true for asynchronous file I/O operations or only applicable for Network I/O?

  1. Means if there is File I/O operation then in this case thread pool will be used compulsory or libuv will use those asynchronous interfaces, avoiding usage of the thread pool?
  2. Does Libuv use thread pool for Network I/O ?
like image 564
Curious Avatar asked Mar 05 '23 16:03

Curious


1 Answers

For some standard library function calls, the node C++ side and libuv decide to do expensive calculations outside of the event loop entirely.They make something called a thread pool that thread pool is a series of four threads that can be used for running computationally intensive tasks such as hashing functions or reading files in hard drive(fs module functions). There are ONLY FOUR things that use the thread pool - DNS lookup, fs, crypto and zlib.

So just as the node standard library has some functions that make use of libuv thread pool it also has some functions that make use of code that is built into the underlying operating system through libuv. Neither libuv nor node has any code to handle all of this low level network request operations. Instead libuv delegates the request making to the underlying operating system. So it's actually our operating system that does the real network requests. Libuv is used to issue the request and then it just waits on the operating system to emit a signal that some response has come back to the request. Network I/O is done by network hardware in your system and ISP.OS just keeps tracking of connections and once I/O operations done OS will pass the results to Libuv.

like image 158
Yilmaz Avatar answered Mar 16 '23 09:03

Yilmaz