Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

http client: Destroy on timeout

  • Loading branch information
isaacs committed May 2, 2012
1 parent bce6813 commit c9be1d5ffdb3249332394e98a9f009379ac41873
Showing with 28 additions and 3 deletions.
  1. +28 −3 lib/http.js
@@ -1284,6 +1284,7 @@ ClientRequest.prototype.onSocket = function(socket) {
});

};

ClientRequest.prototype._deferToConnect = function(method, arguments_, cb) {
// This function is for calls that need to happen once the socket is
// connected and writable. It's an important promisy thing for all the socket
@@ -1312,9 +1313,33 @@ ClientRequest.prototype._deferToConnect = function(method, arguments_, cb) {
onSocket();
}
};
ClientRequest.prototype.setTimeout = function() {
this._deferToConnect('setTimeout', arguments);

ClientRequest.prototype.setTimeout = function(msecs, callback) {
if (callback) this.once('timeout', callback);

var self = this;
function emitTimeout() {
self.emit('timeout');
self.destroy(new Error('timeout'));
}

if (this.socket && this.socket.writable) {
this.socket.setTimeout(msecs, emitTimeout);
return;
}

if (this.socket) {
this.socket.on('connect', function() {

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis May 2, 2012

Member

Shouldn't that be this.once('connect', ...)?

This comment has been minimized.

Copy link
@isaacs

isaacs May 3, 2012

Author

Yes, good catch.

this.setTimeout(msecs, emitTimeout);
});
return;
}

this.on('socket', function(sock) {

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis May 2, 2012

Member

Likewise.

This comment has been minimized.

Copy link
@isaacs

isaacs May 3, 2012

Author

Less relevant than above, but yes, this should only happen once.

this.setTimeout(msecs, emitTimeout);
});
};

ClientRequest.prototype.setNoDelay = function() {
this._deferToConnect('setNoDelay', arguments);
};
@@ -1403,7 +1428,7 @@ function connectionListener(socket) {
httpSocketSetup(socket);

socket.setTimeout(2 * 60 * 1000); // 2 minute timeout
socket.addListener('timeout', function() {
socket.once('timeout', function() {
socket.destroy();
});

0 comments on commit c9be1d5

Please sign in to comment.
You can’t perform that action at this time.