Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 5f795ef

Browse files
committed
net: Timeouts should work before DNS resolution
1 parent 33c3394 commit 5f795ef

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
lines changed

lib/net.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var util = require('util');
22
var events = require('events');
33
var stream = require('stream');
4+
var timers = require('timers');;
45

56
var kMinPoolSpace = 128;
67
var kPoolSize = 40 * 1024;
@@ -420,7 +421,7 @@ Socket.prototype._writeOut = function(data, encoding, fd, cb) {
420421
debug('wrote ' + bytesWritten + ' to socket. [fd, off, len] = ' +
421422
JSON.stringify([this.fd, off, len]) + '\n');
422423

423-
require('timers').active(this);
424+
timers.active(this);
424425

425426
if (bytesWritten == len) {
426427
// awesome. sent to buffer.
@@ -498,6 +499,8 @@ Socket.prototype.setEncoding = function(encoding) {
498499

499500

500501
function doConnect(socket, port, host) {
502+
timers.active(socket);
503+
501504
try {
502505
connect(socket.fd, port, host);
503506
} catch (e) {
@@ -601,7 +604,7 @@ Socket.prototype._onReadable = function() {
601604
if (self.onend) self.onend();
602605
} else if (bytesRead > 0) {
603606

604-
require('timers').active(self);
607+
timers.active(self);
605608

606609
var start = pool.used;
607610
var end = pool.used + bytesRead;
@@ -635,7 +638,7 @@ Socket.prototype.connect = function() {
635638
if (self.fd) throw new Error('Socket already opened');
636639
if (!self._readWatcher) throw new Error('No readWatcher');
637640

638-
require('timers').active(socket);
641+
timers.active(this);
639642

640643
self._connecting = true; // set false in doConnect
641644
self.writable = true;
@@ -659,6 +662,7 @@ Socket.prototype.connect = function() {
659662
if (err) {
660663
self.emit('error', err);
661664
} else {
665+
timers.active(self);
662666
self.type = addressType == 4 ? 'tcp4' : 'tcp6';
663667
self.fd = socket(self.type);
664668
doConnect(self, port, ip);
@@ -688,10 +692,10 @@ Socket.prototype.setKeepAlive = function(enable, time) {
688692

689693
Socket.prototype.setTimeout = function(msecs) {
690694
if (msecs > 0) {
691-
require('timers').enroll(this, msecs);
692-
if (this.fd) { require('timers').active(this); }
695+
timers.enroll(this, msecs);
696+
if (this.fd) { timers.active(this); }
693697
} else if (msecs === 0) {
694-
require('timers').unenroll(this);
698+
timers.unenroll(this);
695699
}
696700
};
697701

@@ -742,7 +746,7 @@ Socket.prototype.destroy = function(exception) {
742746
this._readWatcher = null;
743747
}
744748

745-
require('timers').unenroll(this);
749+
timers.unenroll(this);
746750

747751
if (this.server) {
748752
this.server.connections--;
Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,73 @@
11
// This example attempts to time out before the connection is established
22
// https://groups.google.com/forum/#!topic/nodejs/UE0ZbfLt6t8
33
// https://groups.google.com/forum/#!topic/nodejs-dev/jR7-5UDqXkw
4+
//
5+
// TODO: how to do this without relying on the responses of specific sites?
46

57
var common = require('../common');
68
var net = require('net');
79
var assert = require('assert');
810

911
var start = new Date();
10-
var gotTimeout = false;
11-
var gotConnect = false;
12+
13+
var gotTimeout0 = false;
14+
var gotTimeout1 = false;
15+
16+
var gotConnect0 = false;
17+
var gotConnect1 = false;
18+
1219
var T = 100;
1320

14-
var socket = net.createConnection(9999, '23.23.23.23');
1521

16-
socket.setTimeout(T);
22+
// With DNS
23+
24+
var socket0 = net.createConnection(9999, 'google.com');
25+
26+
socket0.setTimeout(T);
27+
28+
socket0.on('timeout', function() {
29+
console.error("timeout");
30+
gotTimeout0 = true;
31+
var now = new Date();
32+
assert.ok(now - start < T + 500);
33+
socket0.end();
34+
});
35+
36+
socket0.on('connect', function() {
37+
console.error("connect");
38+
gotConnect0 = true;
39+
socket0.end();
40+
});
41+
42+
43+
// Without DNS
1744

45+
var socket1 = net.createConnection(9999, '24.24.24.24');
1846

19-
socket.on('timeout', function() {
47+
socket1.setTimeout(T);
48+
49+
socket1.on('timeout', function() {
2050
console.error("timeout");
21-
gotTimeout = true;
51+
gotTimeout1 = true;
2252
var now = new Date();
2353
assert.ok(now - start < T + 500);
24-
socket.end();
54+
socket1.end();
2555
});
2656

27-
socket.on('connect', function() {
57+
socket1.on('connect', function() {
2858
console.error("connect");
29-
gotConnect = true;
30-
socket.end();
59+
gotConnect1 = true;
60+
socket1.end();
3161
});
3262

3363

64+
65+
66+
3467
process.on('exit', function() {
35-
assert.ok(gotTimeout);
36-
assert.ok(!gotConnect);
68+
assert.ok(gotTimeout0);
69+
assert.ok(!gotConnect0);
70+
71+
assert.ok(gotTimeout1);
72+
assert.ok(!gotConnect1);
3773
});

0 commit comments

Comments
 (0)