Skip to content

Commit 0982810

Browse files
committedAug 1, 2017
dns: add channel.cancel()
This can be used to implement custom timeouts. Fixes: #7231 PR-URL: #14518 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 69e41dc commit 0982810

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed
 

‎doc/api/dns.md

+8
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ The following methods from the `dns` module are available:
9595
* [`resolver.resolveTxt()`][`dns.resolveTxt()`]
9696
* [`resolver.reverse()`][`dns.reverse()`]
9797

98+
### resolver.cancel()
99+
<!-- YAML
100+
added: REPLACEME
101+
-->
102+
103+
Cancel all outstanding DNS queries made by this resolver. The corresponding
104+
callbacks will be called with an error with code `ECANCELLED`.
105+
98106
## dns.getServers()
99107
<!-- YAML
100108
added: v0.11.3

‎lib/dns.js

+4
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ class Resolver {
246246
constructor() {
247247
this._handle = new ChannelWrap();
248248
}
249+
250+
cancel() {
251+
this._handle.cancel();
252+
}
249253
}
250254

251255
function resolver(bindingName) {

‎src/cares_wrap.cc

+8
Original file line numberDiff line numberDiff line change
@@ -2131,6 +2131,13 @@ void SetServers(const FunctionCallbackInfo<Value>& args) {
21312131
args.GetReturnValue().Set(err);
21322132
}
21332133

2134+
void Cancel(const FunctionCallbackInfo<Value>& args) {
2135+
ChannelWrap* channel;
2136+
ASSIGN_OR_RETURN_UNWRAP(&channel, args.Holder());
2137+
2138+
ares_cancel(channel->cares_channel());
2139+
}
2140+
21342141

21352142
void StrError(const FunctionCallbackInfo<Value>& args) {
21362143
Environment* env = Environment::GetCurrent(args);
@@ -2215,6 +2222,7 @@ void Initialize(Local<Object> target,
22152222

22162223
env->SetProtoMethod(channel_wrap, "getServers", GetServers);
22172224
env->SetProtoMethod(channel_wrap, "setServers", SetServers);
2225+
env->SetProtoMethod(channel_wrap, "cancel", Cancel);
22182226

22192227
channel_wrap->SetClassName(
22202228
FIXED_ONE_BYTE_STRING(env->isolate(), "ChannelWrap"));
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
const common = require('../common');
3+
const dnstools = require('../common/dns');
4+
const { Resolver } = require('dns');
5+
const assert = require('assert');
6+
const dgram = require('dgram');
7+
8+
const server = dgram.createSocket('udp4');
9+
const resolver = new Resolver();
10+
11+
server.bind(0, common.mustCall(() => {
12+
resolver.setServers([`127.0.0.1:${server.address().port}`]);
13+
resolver.resolve4('example.org', common.mustCall((err, res) => {
14+
assert.strictEqual(err.code, 'ECANCELLED');
15+
assert.strictEqual(err.errno, 'ECANCELLED');
16+
assert.strictEqual(err.syscall, 'queryA');
17+
assert.strictEqual(err.hostname, 'example.org');
18+
server.close();
19+
}));
20+
}));
21+
22+
server.on('message', common.mustCall((msg, { address, port }) => {
23+
const parsed = dnstools.parseDNSPacket(msg);
24+
const domain = parsed.questions[0].domain;
25+
assert.strictEqual(domain, 'example.org');
26+
27+
// Do not send a reply.
28+
resolver.cancel();
29+
}));

0 commit comments

Comments
 (0)
Please sign in to comment.