New upstream version 0.6.1

parent 7cbe4a4f
......@@ -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
......@@ -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) {
......@@ -24,7 +36,7 @@ function ForeverAgent(options) {
self.freeSockets[name].push(socket)
// if an error happens while we don't use the socket anyway, meh, throw the socket away
function onIdleError() {
var onIdleError = function() {
socket.destroy()
}
socket._onIdleError = onIdleError
......@@ -47,7 +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 = host + ':' + port
var name = getConnectionName(host, port)
if (typeof host !== 'string') {
var options = host
port = options.port
host = options.host
}
if (this.freeSockets[name] && this.freeSockets[name].length > 0 && !req.useChunkedEncodingByDefault) {
var idleSocket = this.freeSockets[name].pop()
idleSocket.removeListener('error', idleSocket._onIdleError)
......
......@@ -2,7 +2,8 @@
"author": "Mikeal Rogers <mikeal.rogers@gmail.com> (http://www.futurealoof.com)",
"name": "forever-agent",
"description": "HTTP Agent that keeps socket connections alive between keep-alive requests. Formerly part of mikeal/request, now a standalone module.",
"version": "0.5.1",
"version": "0.6.1",
"license": "Apache-2.0",
"repository": {
"url": "https://github.com/mikeal/forever-agent"
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment