Skip to content

Commit d05bdbb

Browse files
committed
refactor creating connection name
Used an 'if' block, as requested. Created getConnectionName function to avoid duplicate code. Deleted lines no longer needed in addRequest.
1 parent 0f6a673 commit d05bdbb

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

index.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ var util = require('util')
66
, net = require('net')
77
, tls = require('tls')
88
, AgentSSL = require('https').Agent
9+
10+
function getConnectionName(host, port) {
11+
var name = ''
12+
if (typeof host === 'string') {
13+
name = host + ':' + port
14+
} else {
15+
// For node.js v012.0 and iojs-v1.5.1, host is an object. And any existing localAddress is part of the connection name.
16+
name = host.host + ':' + host.port + ':' + (host.localAddress ? (host.localAddress + ':') : ':')
17+
}
18+
return name
19+
}
920

1021
function ForeverAgent(options) {
1122
var self = this
@@ -16,10 +27,7 @@ function ForeverAgent(options) {
1627
self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets
1728
self.minSockets = self.options.minSockets || ForeverAgent.defaultMinSockets
1829
self.on('free', function(socket, host, port) {
19-
// For node.js v0.12.0 and iojs-v1.5.1, host is a structure instead of a string
20-
// Also, any existing localAddress is part of the connection name
21-
var name = typeof host === 'string' ? host + ':' + port :
22-
host.host + ':' + host.port + (host.localAddress ? (':' + host.localAddress + ':') : '::')
30+
var name = getConnectionName(host, port)
2331

2432
if (self.requests[name] && self.requests[name].length) {
2533
self.requests[name].shift().onSocket(socket)
@@ -51,16 +59,7 @@ ForeverAgent.defaultMinSockets = 5
5159
ForeverAgent.prototype.createConnection = net.createConnection
5260
ForeverAgent.prototype.addRequestNoreuse = Agent.prototype.addRequest
5361
ForeverAgent.prototype.addRequest = function(req, host, port) {
54-
if (typeof host !== 'string') {
55-
var options = host
56-
port = options.port
57-
host = options.host
58-
}
59-
60-
// For node.js v0.12.0 and iojs-v1.5.1, host is a structure instead of a string
61-
// Also, any existing localAddress is part of the connection name
62-
var name = typeof host === 'string' ? host + ':' + port :
63-
host.host + ':' + host.port + (host.localAddress ? (':' + host.localAddress + ':') : '::')
62+
var name = getConnectionName(host, port)
6463

6564
if (this.freeSockets[name] && this.freeSockets[name].length > 0 && !req.useChunkedEncodingByDefault) {
6665
var idleSocket = this.freeSockets[name].pop()

0 commit comments

Comments
 (0)