Skip to content

Commit

Permalink
restify#878 Fix a couple breakages in HttpClient.close() from last co…
Browse files Browse the repository at this point in the history
…mmit
  • Loading branch information
trentm committed Sep 29, 2015
1 parent afdfb77 commit 0138298
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/clients/http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ HttpClient.prototype.close = function close() {
*
* The *tunnel-agent* package's Agent has:
* <agent>.sockets = <array of socket objects>
* where sometimes that "socket object" is a placeholder `{}` (i.e. no
* `.end()` method).
*
* This code needs to handle both.
*
Expand All @@ -462,18 +464,19 @@ HttpClient.prototype.close = function close() {
* implement a `.close()` method to handle this. Then alternative agents,
* like "tunnel-agent", could implement that API.
*/
var self = this;
if (this.agent) {
var sockets = this.agent.sockets;

if (Array.isArray(sockets)) {
this.log.trace('ending %d sockets', sockets.length);
sockets.forEach(function (s) {
s.end();
s.end && s.end();
});
} else {
Object.keys((sockets || {})).forEach(function (k) {
if (Array.isArray(sockets[k])) {
this.log.trace({host: k}, 'ending %d sockets');
self.log.trace({host: k}, 'ending %d sockets');
sockets[k].forEach(function (s) {
s.end();
});
Expand All @@ -484,7 +487,7 @@ HttpClient.prototype.close = function close() {
sockets = this.agent.idleSockets || this.agent.freeSockets;
Object.keys((sockets || {})).forEach(function (k) {
sockets[k].forEach(function (s) {
this.log.trace({host: k}, 'ending idle/free %d sockets');
self.log.trace({host: k}, 'ending idle/free %d sockets');
s.end();
});
});
Expand Down

0 comments on commit 0138298

Please sign in to comment.