forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dns: refactor dns.resolve*() to return an eventemitter
This seeks to address issue nodejs#1071 in a couple of ways: 1. An internal ref counter is used to count the number of outstanding dns.resolve() requests are still pending. If this number is > 0 when dns.setServers() is called, an error will be thrown. 2. dns.resolve() will now return an EventEmitter that can emit three possible events: `'error'`, `'resolve'` and `'complete'`. Previously, if there were any errors reported while *setting up* the dns.resolve operation, they would be thrown immediately. However, any errors that occur *during* the dns.operation would be passed to the callback function as the first argument. Now, all errors are routed through the `'error'` event on the EventEmitter. This makes the error handling more consistent but changes the flow since `dns.resolve*()` will no longer throw immediately. If a callback is passed to `dns.resolve*()`, which is the current usage pattern, it is set as the handler for **both** the `'resolve'` and `'error'` events. Alternatively, it is now possible to omit the callback and add listeners directly to the EventEmitter returned by dns.resolve*(). The `'resolve'` listener is passed *only* the results of the resolve (errors are passed to `'error'`). So, for example, you can continue to do this: ```js dns.resolve('www.example.org', 'A', (err, addresses) => { if (err) { /** ... **/ } // do stuff }); ``` Or you can do: ```js dns.resolve('www.example.org', 'A') .on('resolve', (addresses) => { // do stuff }) .on('error', (error) => { // handle it }) .on('complete', () => { // do this here because otherwise it'll throw. dns.setServers([]); }). ``` On the `dns.setServers()` part, the `'complete'` event is emitted using `setImmediate()`. This ensures that the event is not emitted until after c-ares has an opportunity to clean up. This avoids the assert that brings down the Node process if setServers is called at the wrong time.
- Loading branch information
Showing
5 changed files
with
95 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const dns = require('dns'); | ||
const assert = require('assert'); | ||
|
||
var m = dns.resolve('www.example.com', 'A', (err, addresses) => { | ||
assert.throws(() => { | ||
dns.setServers([]); | ||
}, /Cannot call setServers\(\) while resolving./); | ||
}); | ||
|
||
m.on('complete', common.mustCall(() => { | ||
dns.setServers([]); | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters