-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
dns: add setDefaultResolver and getDefaultResolver #14620
dns: add setDefaultResolver and getDefaultResolver #14620
Conversation
Since `dns.Resolver` was added, it gives a chance to manage the default resolver by ourselves. So `dns.setDefaultResolver` and `dns.getDefaultResolver` is added here.
semver-minor? |
@Trott yep |
As far as I know, the only difference between channels is the set of DNS servers, so which advantage does this API offer, compared to modifying the default channel using Adding It might be a better approach to be able to pass |
@tniessen If we have several DNS servers to be candidates and we sometime choose one of them, I think this will be much more convenient. const resolvers = {
google: new Resolver(), // assume we've already `setServer([ '8.8.8.8', '8.8.4.4' ])`
other: new Resolver()
};
// some logic
function changeDefaultResolver(name) {
dns.setDefaultResolver(resolvers[name]);
} And if no this function, we should do this: const servers = {
google: [ '8.8.8.8', '8.8.4.4' ],
other: [ ... ]
};
function changeServer(name) {
dns.setServer(servers[name]);
} And what's more, if we add the |
|
||
function setDefaultResolver(resolver) { | ||
if (!(resolver instanceof Resolver)) { | ||
throw new errors.TypeError('ERR_INVALID_DNS_RESOLVER'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just reuse the existing ERR_INVALID_ARG_TYPE
?
--> | ||
|
||
Get the default resolver which may used by Node.js modules such as `http` and | ||
provides functions like `dns.resolve()`, `dns.setServers()`, etc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps just...
Returns the default `dns.Resolver` used by Node.js.
--> | ||
|
||
Set the default resolver which may used by Node.js modules such as `http` and | ||
provides functions like `dns.resolve()`, `dns.setServers()`, etc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps just...
Changes the default `Resolver` used by Node.js.
Set the default resolver which may used by Node.js modules such as `http` and | ||
provides functions like `dns.resolve()`, `dns.setServers()`, etc. | ||
|
||
The only parameter must be a `dns.Resolver` instance that to replace the default |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd omit this sentence.
Get the default resolver which may used by Node.js modules such as `http` and | ||
provides functions like `dns.resolve()`, `dns.setServers()`, etc. | ||
|
||
## dns.setDefaultResolver() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### dns.setDefaultResolver(resolver)
<!-- YAML
added: REPLACEME
-->
* `resolver` {dns.Resolver}
defaultResolver = resolver; | ||
DEFAULT_RESOLVER_EXPORTED_FUNCTIONS.forEach((key) => { | ||
module.exports[key] = defaultResolver[key].bind(defaultResolver); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels... hackish. Perhaps changing the module.exports properties into getters that return the equivalent method from defaultResolver
would be a cleaner approach?
e.g.
module.exports = {
/** ... **/
get getServers() {
return defaultResolver.getServers.bind(defaultResolver);
}
/** ... **/
}
That way the module's exported properties never change.
IMO we should minimize the side effects of our API, and certainly not encourage using APIs with side effects. Having a default channel is unavoidable both for convenience and backward compatibility, but promoting its use (and primarily reconfiguration) does not seem like a good idea to me. If multiple modules try to configure the default channel or even attempt to replace it, just to enforce specific options for their DNS requests, they will interfere with each other. Instead, if a module requires the resolver to have specific properties, it should create a new resolver and either use it directly or pass it to the respective API (which we would need to adapt accordingly, e.g. by adding a cc @addaleax for designing the |
@tniessen I’m already 👍ing your comments because I really have nothing to add, I just very plainly agree with you. :) |
Thinking about it further, I agree. Rather than allowing the default to be overridden, I'd prefer to make it easier / possible to use specific |
So does this PR still need opening? |
Let's leave it open for the time being. We should investigate how feasible it would be to support the other option of passing in |
@jasnell what should be done here? I am not certain how we will progress with it and is just keeping it open the right thing? |
I'm assigning myselfto this. I'll be able to look at it in detail in the next week or so. |
Closing due to long inactivity. If anyone thinks this should be reopened, please feel free to do so. |
ping ... perhaps @addaleax has some thoughts? |
Since
dns.Resolver
was added, it gives a chance to manage the defaultresolver by ourselves. So
dns.setDefaultResolver
anddns.getDefaultResolver
is added here.Checklist
make -j4 test
(UNIX)Affected core subsystem(s)
dns