Skip to content

Commit

Permalink
fix: emit agent socket close event
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 19, 2016
1 parent c3c9550 commit 8c83f0f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
// If you want to remove a socket from the pool, just close it.
// All socket errors end in a close event anyway.
self.removeSocket(s, options);

// [patch start]
self.emit('close');
// [patch end]
}
s.on('close', onClose);

Expand Down
1 change: 0 additions & 1 deletion lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class Agent extends OriginalAgent {
this.closeSocketCount++;
});
this.on('error', () => {
console.log('error socket', arguments);
this.errorSocketCount++;
});
}
Expand Down
5 changes: 5 additions & 0 deletions test/agent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,14 @@ describe('test/agent.test.js', () => {
});

it('should remove keepalive socket when server side destroy()', done => {
done = pedding(2, done);
const name = 'localhost:' + port + ':';
assert(!agentkeepalive.sockets[name]);
assert(agentkeepalive.freeSockets[name].length === 1);

// should emit agent close event too
agentkeepalive.once('close', done);

const req = http.get({
agent: agentkeepalive,
port,
Expand Down

0 comments on commit 8c83f0f

Please sign in to comment.