Sending UDP Messages in Node.js Without DNS Lookups
hermanradtke.com
hermanradtke.com
As another commented pointed out the "solution" only uses a single IP, even if the resolver returned multiple.
I would not use this in production.
There is only 4 UV threadpool, and DNS resolution use it. When using service with low ttl or if for wahtever reason bug or something and if node has to resolve DNS every time under highload, it's very quickly consume all of thoese 4 UV thread pool.
The title of the entire story already had me confused, as it suggests you somehow need DNS for UDP. Everything that is presented here is a caching strategy.
In this case it is probably 'using the wrong tool, Node.js'. [0]
> The title of the entire story already had me confused
I needed to read the whole article two times to understand what exactly is going on.
It's really should be titled "Sending UDP Messages a bit faster in Node.js because Node.js by default sucks at invoking getaddrinfo()"
Bonus: the createUdpTransport() function also has a DNS cache, which the author of the article could have enabled with just "cacheDns: true". But if the library code used socket.connect(), this internal cache shouldn't be needed either.
1: https://github.com/brightcove/hot-shots/blob/564ac7bfdaaf366...
> If the socket sockfd is of type SOCK_DGRAM, then addr is the address to which datagrams are sent by default, and the only address from which datagrams are received.
After that, you can use send() instead of sendmsg(), so it saves you on having to pass the host on each call to sendmsg.
It can also avoid extra DNS lookups: everything is handled for you by the kernel. See also: https://stackoverflow.com/a/51296247
Actually, I don't know what happens if the DNS record expires during the connection. I would presume nothing -- that the socket will stay open with the same (possibly now invalid) destination IP until closed.
I don't see `send2` in the node docs[2].
[1] https://beej.us/guide/bgnet/html/#sendtorecv [2] https://nodejs.org/api/dgram.html
Using await makes the flow of our code sequential.
We want to call socket.close after all messages are sent
Hence the use of send2Everything else remains pretty much the same...
Slightly worse, in that, the code only ever uses the first IP returned from the DNS lookups it initiates. Some domains may return 16 IPs but that code would use only the first, at least until the cached-entry expires.
I was investigating errors in node code for a few weeks, and traced it to DNS returning a bad IP address. Node didn't even try to call the second IP address, it just returned a "request made but no response" error
Looked deeper, and saw other similar complaints asking for nice to support failover and try the second IP, but it was highly opposed. They suggested writing your own DNS handling code (which is exactly what this person is doing, though for a different reason)