gpt4 book ai didi

node.js - 为什么libuv通过多线程进行DNS请求

转载 作者:太空宇宙 更新时间:2023-11-03 22:10:11 25 4
gpt4 key购买 nike

有时我的服务总是发送差异很大的主机名url,我会重新构建我的docker容器,容器同时重新启动,一些http请求会失败:

events.js:154
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND www.videojj.com www.videojj.com:80
at errnoException (dns.js:26:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:77:26)

我不确定原因,我知道DNS,libuv中多线程文件操作。我很困惑为什么 DNS 请求不能使用 libuv 中的 IO 复用机制

最佳答案

正如您所提到的,根据 documentation (强调我的):

libuv provides a threadpool which can be used to run user code and get notified in the loop thread. This thread pool is internally used to run all file system operations, as well as getaddrinfo and getnameinfo requests.

uvbook给出了一些其他提示:

Threads are used internally to fake the asynchronous nature of all of the system calls. libuv also uses threads to allow you, the application, to perform a task asynchronously that is actually blocking, by spawning a thread and collecting the result when it is done.

那么,回到你的问题:

why DNS request can't use IO-multiplexing mechanism in libuv

这是因为 fs 操作和(让我说)DNS 请求正在阻塞系统调用。因此,如果在主线程上执行,它们将破坏 libuv 的异步特性并强制循环停止。没有办法,只能在单独的线程上启动它们,以保持循环正常运行,直到作业完成。

请注意,也存在 getaddrinfogetnameinfo 的非阻塞版本,但它们不可移植,因此 libuv 无法一直使用它们。
请参阅this question了解更多详细信息。

关于node.js - 为什么libuv通过多线程进行DNS请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44603059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com