diff --git a/lib/_http_agent.js b/lib/_http_agent.js index 9409f98..c1ec7a0 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -257,6 +257,14 @@ Agent.prototype.createSocket = function(req, options) { // set the default timer s.setTimeout(self.timeout); + // Add a default error handler to avoid Unhandled 'error' event throw + // https://github.com/node-modules/agentkeepalive/issues/25 + function onError(err) { + debug('CLIENT socket onError: %s', err); + self.emit('error', err); + } + s.on('error', onError); + function onRemove() { // We need this function for cases like HTTP 'upgrade' // (defined by WebSockets) where we need to remove a socket from the @@ -266,6 +274,7 @@ Agent.prototype.createSocket = function(req, options) { s.removeListener('close', onClose); s.removeListener('free', onFree); s.removeListener('agentRemove', onRemove); + s.removeListener('error', onError); // remove timer s.setTimeout(0, onTimeout); } diff --git a/lib/agent.js b/lib/agent.js index 414ce85..11cdf95 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -8,6 +8,7 @@ * * https://github.com/joyent/node/blob/master/lib/_http_agent.js * * Copyright(c) 2012 - 2014 fengmk2 + * Copyright(c) node-modules * MIT Licensed */ @@ -57,6 +58,8 @@ function Agent(options) { var self = this; self.createSocketCount = 0; self.closeSocketCount = 0; + // socket error event count + self.errorSocketCount = 0; self.requestCount = 0; self.timeoutSocketCount = 0; self.on('free', function () { @@ -68,6 +71,9 @@ function Agent(options) { self.on('close', function () { self.closeSocketCount++; }); + self.on('error', function () { + self.errorSocketCount++; + }); } util.inherits(Agent, OriginalAgent); @@ -87,6 +93,7 @@ Agent.prototype.getCurrentStatus = function () { return { createSocketCount: this.createSocketCount, closeSocketCount: this.closeSocketCount, + errorSocketCount: this.errorSocketCount, timeoutSocketCount: this.timeoutSocketCount, requestCount: this.requestCount, freeSockets: inspect(this.freeSockets), diff --git a/test/agent.test.js b/test/agent.test.js index 3bb1882..4cdf6a4 100644 --- a/test/agent.test.js +++ b/test/agent.test.js @@ -2,6 +2,7 @@ * agentkeepalive - test/agent.test.js * * Copyright(c) 2012 - 2013 fengmk2 + * Copyright(c) node-modules * MIT Licensed */ @@ -799,4 +800,14 @@ describe('agent.test.js', function () { }); }); }); + + describe('getCurrentStatus()', function () { + it('should get current agent status', function () { + var status = agentkeepalive.getCurrentStatus(); + console.log(status); + status.should.have.keys('createSocketCount', 'closeSocketCount', 'errorSocketCount', 'timeoutSocketCount', + 'requestCount', 'freeSockets', 'sockets', 'requests'); + }); + }); + }); diff --git a/test/https_agent.test.js b/test/https_agent.test.js index 96778a6..c6d135c 100644 --- a/test/https_agent.test.js +++ b/test/https_agent.test.js @@ -2,6 +2,7 @@ * agentkeepalive - test/https_agent.test.js * * Copyright(c) 2012 - 2013 fengmk2 + * Copyright(c) node-modules * MIT Licensed */