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

Commit

Permalink
Browse files Browse the repository at this point in the history
http client: Destroy on timeout
  • Loading branch information
isaacs committed May 2, 2012
1 parent bce6813 commit c9be1d5
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions lib/http.js
Expand Up @@ -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
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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();
});

Expand Down

0 comments on commit c9be1d5

Please sign in to comment.