Skip to content

Commit 75e7091

Browse files
committed
timers
1 parent b865ad2 commit 75e7091

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lib/connection.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Net = require('net');
22
var util = require('util');
33
var Tls = require('tls');
4+
var Timers = require('timers');
45
var EventEmitter = require('events').EventEmitter;
56
var Queue = require('double-ended-queue');
67

@@ -83,8 +84,13 @@ function Connection (opts)
8384
this.packetParser = new PacketParser(function (p) { connection.handlePacket(p); });
8485

8586
this.stream.on('data', function (data) {
87+
if (connection.connectTimeout) {
88+
Timers.clearTimeout(connection.connectTimeout);
89+
connection.connectTimeout = null;
90+
}
8691
connection.packetParser.execute(data);
8792
});
93+
8894
this.stream.on('end', function () {
8995
// we need to set this flag everywhere where we want connection to close
9096
if (connection._closing) {
@@ -120,10 +126,7 @@ function Connection (opts)
120126

121127
if (this.config.connectTimeout) {
122128
var timeoutHandler = this._handleTimeoutError.bind(this);
123-
this.stream.setTimeout(this.config.connectTimeout, timeoutHandler);
124-
this.stream.once('connect', function () {
125-
connection.stream.setTimeout(0, timeoutHandler);
126-
});
129+
this.connectTimeout = Timers.setTimeout(timeoutHandler, this.config.connectTimeout);
127130
}
128131
}
129132
util.inherits(Connection, EventEmitter);
@@ -147,7 +150,8 @@ Connection.prototype._handleNetworkError = function (err) {
147150

148151
Connection.prototype._handleTimeoutError = function () {
149152
if (this.stream) {
150-
this.stream.setTimeout(0);
153+
Timers.clearTimeout(this.connectTimeout);
154+
this.connectTimeout = null;
151155
this.stream.destroy();
152156
}
153157

0 commit comments

Comments
 (0)