- This topic is empty.
-
AuthorPosts
-
September 2, 2022 at 10:17 am #9274marcos-casagrandeParticipant
For those who perform thousand or millions of requests per day, and need a solution to this issue:
It’s quite normal to get
getaddrinfo EAI_AGAIN
errors when performing a lot of requests on your server. Node.js itself doesn’t perform any DNS caching, it delegates everything DNS related to the OS.You need to have in mind that every http/https request performs a DNS lookup, this can become quite expensive, to avoid this bottleneck and
getaddrinfo
errors, you can implement a DNS cache.http.request
(and https) accepts alookup
property which defaults todns.lookup()
http.get('http://example.com', { lookup: yourLookupImplementation }, response => { // do something here with response });
I strongly recommend to use an already tested module, instead of writing a DNS cache yourself, since you’ll have to handle TTL correctly, among other things to avoid hard to track bugs.
I personally use
cacheable-lookup
which is the one thatgot
uses (seednsCache
option).You can use it on specific requests
const http = require('http'); const CacheableLookup = require('cacheable-lookup'); const cacheable = new CacheableLookup(); http.get('http://example.com', {lookup: cacheable.lookup}, response => { // Handle the response here });
or globally
const http = require('http'); const https = require('https'); const CacheableLookup = require('cacheable-lookup'); const cacheable = new CacheableLookup(); cacheable.install(http.globalAgent); cacheable.install(https.globalAgent);
NOTE: have in mind that if a request is not performed through Node.js
http/https
module, using.install
on the global agent won’t have any effect on said request, for example requests made usingundici
-
AuthorPosts
- You must be logged in to reply to this topic.