Skip to content

Commit

Permalink
Merge pull request #27 from mgorczyca/connection_name_change
Browse files Browse the repository at this point in the history
For connection name, adjust for new node versions
  • Loading branch information
simov committed Apr 7, 2015
2 parents e45a677 + 48b7461 commit 8304db3
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ var util = require('util')
, net = require('net')
, tls = require('tls')
, AgentSSL = require('https').Agent

function getConnectionName(host, port) {
var name = ''
if (typeof host === 'string') {
name = host + ':' + port
} else {
// For node.js v012.0 and iojs-v1.5.1, host is an object. And any existing localAddress is part of the connection name.
name = host.host + ':' + host.port + ':' + (host.localAddress ? (host.localAddress + ':') : ':')
}
return name
}

function ForeverAgent(options) {
var self = this
Expand All @@ -16,7 +27,8 @@ function ForeverAgent(options) {
self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets
self.minSockets = self.options.minSockets || ForeverAgent.defaultMinSockets
self.on('free', function(socket, host, port) {
var name = host + ':' + port
var name = getConnectionName(host, port)

if (self.requests[name] && self.requests[name].length) {
self.requests[name].shift().onSocket(socket)
} else if (self.sockets[name].length < self.minSockets) {
Expand Down Expand Up @@ -47,13 +59,14 @@ ForeverAgent.defaultMinSockets = 5
ForeverAgent.prototype.createConnection = net.createConnection
ForeverAgent.prototype.addRequestNoreuse = Agent.prototype.addRequest
ForeverAgent.prototype.addRequest = function(req, host, port) {
var name = getConnectionName(host, port)

if (typeof host !== 'string') {
var options = host
port = options.port
host = options.host
}

var name = host + ':' + port
if (this.freeSockets[name] && this.freeSockets[name].length > 0 && !req.useChunkedEncodingByDefault) {
var idleSocket = this.freeSockets[name].pop()
idleSocket.removeListener('error', idleSocket._onIdleError)
Expand Down

0 comments on commit 8304db3

Please sign in to comment.