TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Sending UDP Messages in Node.js Without DNS Lookups

46 pointsby hermanradtkeover 2 years ago

8 comments

pgorczakover 2 years ago
The main issue in the article are calls to getaddrinfo() on every send which create a delay, which is not only DNS related. The function does DNS lookups but also translates IP address strings to address data that can be used by the kernel (e.g. the different ways to write IPv6 addresses like “fe80::1%eth0”)
saghulover 2 years ago
The question I have is why were those lookups a problem in the first place? It&#x27;s just a metric, is it really important that it&#x27;s sent one tick later?<p>As another commented pointed out the &quot;solution&quot; only uses a single IP, even if the resolver returned multiple.<p>I would not use this in production.
评论 #33286662 未加载
评论 #33287244 未加载
评论 #33286630 未加载
nemetroidover 2 years ago
Looking at the UDP code in question in brightcove&#x2F;hot-shots [1]. The createUdpTransport() function creates an object with a send() method that forwards to socket.send(), calling the underlying function with the same host and port argument each time. It seems to me that this code should call socket.connect(args.host, args.port) once instead, and skip the host and port arguments to the send() calls. Presumably this would result in just a single DNS lookup.<p>Bonus: the createUdpTransport() function <i>also</i> has a DNS cache, which the author of the article could have enabled with just &quot;cacheDns: true&quot;. But if the library code used socket.connect(), this internal cache shouldn&#x27;t be needed either.<p>1: <a href="https:&#x2F;&#x2F;github.com&#x2F;brightcove&#x2F;hot-shots&#x2F;blob&#x2F;564ac7bfdaaf3669232b4941b4695ef72ae6531e&#x2F;lib&#x2F;transport.js#L46" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;brightcove&#x2F;hot-shots&#x2F;blob&#x2F;564ac7bfdaaf366...</a>
评论 #33288682 未加载
评论 #33287869 未加载
codefloover 2 years ago
Maybe not everybody cares about the wasted tick, but the response to such niche feature requests in open source projects used to be a simple “patches welcome”. I found that way more helpful and less aggressive than what seems to be the new default in Github projects, “won’t fix”. I don’t understand this shift.
评论 #33286687 未加载
评论 #33287724 未加载
nnnover 2 years ago
An alternative approach would be to call &quot;await util.promisify(socket.connect)(8125, host)&quot; so that subsequent calls to send don&#x27;t need to name the destination (and the underlying runtime could call send instead of sendto). Perhaps the author needs the messages to be sent to a new host quickly when DNS records change, but the trade-off might be different depending on the use case, and perhaps some applications might prefer to periodically re-connect the socket to re-resolve DNS.
trillicover 2 years ago
is `send2` supposed to be the node version of UNIX `sendto(2)`? [1]<p>I don&#x27;t see `send2` in the node docs[2].<p>[1] <a href="https:&#x2F;&#x2F;beej.us&#x2F;guide&#x2F;bgnet&#x2F;html&#x2F;#sendtorecv" rel="nofollow">https:&#x2F;&#x2F;beej.us&#x2F;guide&#x2F;bgnet&#x2F;html&#x2F;#sendtorecv</a> [2] <a href="https:&#x2F;&#x2F;nodejs.org&#x2F;api&#x2F;dgram.html" rel="nofollow">https:&#x2F;&#x2F;nodejs.org&#x2F;api&#x2F;dgram.html</a>
评论 #33288067 未加载
wjholdenover 2 years ago
If the service uses a fixed IP address, then why isn&#x27;t the DNS record TTL something very large?
mcstaffordover 2 years ago
TL;DR &quot;Using IP Address Instead of Domain Name&quot;<p>Everything else remains pretty much the same...
评论 #33286323 未加载
评论 #33286053 未加载
评论 #33286546 未加载