Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dns cache #2440 #2552

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.js
Expand Up @@ -165,3 +165,6 @@ module.exports.MockClient = MockClient
module.exports.MockPool = MockPool
module.exports.MockAgent = MockAgent
module.exports.mockErrors = mockErrors

const DNSResolver = require('./lib/core/dns-resolver')
module.exports.DNSResolver = DNSResolver
10 changes: 10 additions & 0 deletions lib/agent.js
Expand Up @@ -7,6 +7,7 @@ const Pool = require('./pool')
const Client = require('./client')
const util = require('./core/util')
const createRedirectInterceptor = require('./interceptor/redirectInterceptor')
const DNSResolver = require('./core/dns-resolver')

const kOnConnect = Symbol('onConnect')
const kOnDisconnect = Symbol('onDisconnect')
Expand All @@ -22,6 +23,14 @@ function defaultFactory (origin, opts) {
: new Pool(origin, opts)
}

function getDnsResolver (options = {}) {
if (options.DNSResolver instanceof DNSResolver) {
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
return options.DNSResolver
}

return options?.dnsResolver?.disable ? undefined : new DNSResolver(options?.dnsResolver)
}

class Agent extends DispatcherBase {
constructor ({ factory = defaultFactory, maxRedirections = 0, connect, ...options } = {}) {
super()
Expand Down Expand Up @@ -50,6 +59,7 @@ class Agent extends DispatcherBase {
this[kOptions].interceptors = options.interceptors
? { ...options.interceptors }
: undefined
this[kOptions].dnsResolver = getDnsResolver(options)
this[kMaxRedirections] = maxRedirections
this[kFactory] = factory
this[kClients] = new Map()
Expand Down