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

2

7

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 ?
Ignominious answered 2/8, 2018 at 12:16 Comment(1)
dns.lookup and getaddrinfo is handled by the thread pool. also crypto and http get post if hostname is provided. refer: youtu.be/P9csgxBgaZ8?t=16m51sJacklin
K
7

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.

Kaveri answered 11/6, 2019 at 5:53 Comment(0)
J
4

I recently got hooked into NodeJS internals and V8 and D8 and libuv those kind of things. So, one of the really weird thing is network i/o is handled by the event loop. But dns.lookup() is handled by the libuv threadpool.

So, http.get/http.post is handled by the event loop only if you provide IP as the url. Not hostname. If hostname is provided then it will internally use dns.lookup which will in turn make the operation handled by thread pool.

Refer: Node's Event Loop From the Inside Out by Sam Roberts, IBM

Note: I am still learning this, so any and every edits are welcome.

Jacklin answered 2/8, 2018 at 12:26 Comment(1)
http is always handled by event loop irrespective of ip or hostname. once dns.lookup() is done resolving via threadpool, the resulting dns.resolve is run in event loop inturn causing the http call to run async via OS internals such as epoll,kqueue etcNovick

© 2022 - 2024 — McMap. All rights reserved.