|
@ -181,15 +181,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/*legacy*/, |
|
|
} else if (sockLen < this.maxSockets) { |
|
|
} else if (sockLen < this.maxSockets) { |
|
|
debug('call onSocket', sockLen, freeLen); |
|
|
debug('call onSocket', sockLen, freeLen); |
|
|
// If we are under maxSockets create a new one.
|
|
|
// If we are under maxSockets create a new one.
|
|
|
this.createSocket(req, options, function(err, newSocket) { |
|
|
this.createSocket(req, options, handleSocketCreation(req, true)); |
|
|
if (err) { |
|
|
|
|
|
nextTick(newSocket._handle.getAsyncId(), function() { |
|
|
|
|
|
req.emit('error', err); |
|
|
|
|
|
}); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
req.onSocket(newSocket); |
|
|
|
|
|
}); |
|
|
|
|
|
} else { |
|
|
} else { |
|
|
debug('wait for socket'); |
|
|
debug('wait for socket'); |
|
|
// We are over limit so we'll add it to the queue.
|
|
|
// We are over limit so we'll add it to the queue.
|
|
@ -222,6 +214,7 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) { |
|
|
const newSocket = self.createConnection(options, oncreate); |
|
|
const newSocket = self.createConnection(options, oncreate); |
|
|
if (newSocket) |
|
|
if (newSocket) |
|
|
oncreate(null, newSocket); |
|
|
oncreate(null, newSocket); |
|
|
|
|
|
|
|
|
function oncreate(err, s) { |
|
|
function oncreate(err, s) { |
|
|
if (called) |
|
|
if (called) |
|
|
return; |
|
|
return; |
|
@ -294,15 +287,7 @@ Agent.prototype.removeSocket = function removeSocket(s, options) { |
|
|
debug('removeSocket, have a request, make a socket'); |
|
|
debug('removeSocket, have a request, make a socket'); |
|
|
var req = this.requests[name][0]; |
|
|
var req = this.requests[name][0]; |
|
|
// If we have pending requests and a socket gets closed make a new one
|
|
|
// If we have pending requests and a socket gets closed make a new one
|
|
|
this.createSocket(req, options, function(err, newSocket) { |
|
|
this.createSocket(req, options, handleSocketCreation(req, false)); |
|
|
if (err) { |
|
|
|
|
|
nextTick(newSocket._handle.getAsyncId(), function() { |
|
|
|
|
|
req.emit('error', err); |
|
|
|
|
|
}); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
newSocket.emit('free'); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
@ -332,6 +317,22 @@ Agent.prototype.destroy = function destroy() { |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
function handleSocketCreation(request, informRequest) { |
|
|
|
|
|
return function handleSocketCreation_Inner(err, socket) { |
|
|
|
|
|
if (err) { |
|
|
|
|
|
const asyncId = (socket && socket._handle && socket._handle.getAsyncId) ? |
|
|
|
|
|
socket._handle.getAsyncId() : |
|
|
|
|
|
null; |
|
|
|
|
|
nextTick(asyncId, () => request.emit('error', err)); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
if (informRequest) |
|
|
|
|
|
request.onSocket(socket); |
|
|
|
|
|
else |
|
|
|
|
|
socket.emit('free'); |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
module.exports = { |
|
|
module.exports = { |
|
|
Agent, |
|
|
Agent, |
|
|
globalAgent: new Agent() |
|
|
globalAgent: new Agent() |
|
|