dns.lookup()
在底层,dns.lookup() 使用与大多数其他程序相同的操作系统功能。例如,dns.lookup() 几乎总是以与 ping 命令相同的方式解析给定名称。在大多数类似 POSIX 的操作系统上,可以通过修改 nsswitch.conf(5) 和/或 resolv.conf(5) 中的设置来改变 dns.lookup() 函数的行为,但修改这些文件会改变在同一操作系统上运行的所有其他程序的行为。
【Under the hood, dns.lookup() uses the same operating system facilities
as most other programs. For instance, dns.lookup() will almost always
resolve a given name the same way as the ping command. On most POSIX-like
operating systems, the behavior of the dns.lookup() function can be
modified by changing settings in nsswitch.conf(5) and/or resolv.conf(5),
but changing these files will change the behavior of all other
programs running on the same operating system.】
尽管从 JavaScript 的角度来看,调用 dns.lookup() 是异步的,但它实际上是作为对 getaddrinfo(3) 的同步调用实现的,并在 libuv 的线程池上运行。这对于某些应用可能会产生令人意外的负面性能影响,更多信息请参见 UV_THREADPOOL_SIZE 文档。
【Though the call to dns.lookup() will be asynchronous from JavaScript's
perspective, it is implemented as a synchronous call to getaddrinfo(3) that runs
on libuv's threadpool. This can have surprising negative performance
implications for some applications, see the UV_THREADPOOL_SIZE
documentation for more information.】
各种网络 API 会在内部调用 dns.lookup() 来解析主机名。如果这是一个问题,可以考虑使用 dns.resolve() 将主机名解析为地址,并使用地址而不是主机名。此外,一些网络 API(例如 socket.connect() 和 dgram.createSocket())允许替换默认解析器 dns.lookup()。
【Various networking APIs will call dns.lookup() internally to resolve
host names. If that is an issue, consider resolving the host name to an address
using dns.resolve() and using the address instead of a host name. Also, some
networking APIs (such as socket.connect() and dgram.createSocket())
allow the default resolver, dns.lookup(), to be replaced.】