2017-01-03 22:16:48 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-12 16:30:48 +01:00
|
|
|
const {
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectKeys,
|
|
|
|
ObjectSetPrototypeOf,
|
|
|
|
ObjectValues,
|
2019-11-30 16:55:29 +01:00
|
|
|
Symbol,
|
2019-11-12 16:30:48 +01:00
|
|
|
} = primordials;
|
2019-04-09 09:55:53 +02:00
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const net = require('net');
|
2015-09-17 00:45:29 +02:00
|
|
|
const EventEmitter = require('events');
|
2019-03-09 17:06:48 +01:00
|
|
|
const debug = require('internal/util/debuglog').debuglog('http');
|
2018-02-11 22:35:59 +01:00
|
|
|
const { async_id_symbol } = require('internal/async_hooks').symbols;
|
2019-09-12 09:22:05 +02:00
|
|
|
const {
|
|
|
|
codes: {
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
2020-05-07 08:18:11 +02:00
|
|
|
ERR_INVALID_OPT_VALUE,
|
2019-09-12 09:22:05 +02:00
|
|
|
},
|
|
|
|
} = require('internal/errors');
|
2020-04-12 22:40:58 +02:00
|
|
|
const { once } = require('internal/util');
|
|
|
|
|
2019-10-21 23:27:50 +02:00
|
|
|
const kOnKeylog = Symbol('onkeylog');
|
2013-04-11 23:47:15 +02:00
|
|
|
// New Agent code.
|
|
|
|
|
|
|
|
// The largest departure from the previous implementation is that
|
|
|
|
// an Agent instance holds connections for a variable number of host:ports.
|
|
|
|
// Surprisingly, this is still API compatible as far as third parties are
|
|
|
|
// concerned. The only code that really notices the difference is the
|
|
|
|
// request object.
|
|
|
|
|
|
|
|
// Another departure is that all code related to HTTP parsing is in
|
|
|
|
// ClientRequest.onSocket(). The Agent is now *strictly*
|
|
|
|
// concerned with managing a connection pool.
|
|
|
|
|
2019-05-05 21:29:32 +02:00
|
|
|
class ReusedHandle {
|
|
|
|
constructor(type, handle) {
|
|
|
|
this.type = type;
|
|
|
|
this.handle = handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 12:29:45 +01:00
|
|
|
function freeSocketErrorListener(err) {
|
|
|
|
const socket = this;
|
|
|
|
debug('SOCKET ERROR on FREE socket:', err.message, err.stack);
|
|
|
|
socket.destroy();
|
|
|
|
socket.emit('agentRemove');
|
|
|
|
}
|
|
|
|
|
2013-04-11 23:47:15 +02:00
|
|
|
function Agent(options) {
|
2013-05-16 00:10:53 +02:00
|
|
|
if (!(this instanceof Agent))
|
|
|
|
return new Agent(options);
|
|
|
|
|
2013-04-11 23:47:15 +02:00
|
|
|
EventEmitter.call(this);
|
|
|
|
|
2017-10-25 08:34:43 +02:00
|
|
|
this.defaultPort = 80;
|
|
|
|
this.protocol = 'http:';
|
2013-05-23 03:44:24 +02:00
|
|
|
|
2018-12-18 03:15:57 +01:00
|
|
|
this.options = { ...options };
|
2013-05-23 03:44:24 +02:00
|
|
|
|
2018-12-03 17:15:45 +01:00
|
|
|
// Don't confuse net and make it think that we're connecting to a pipe
|
2017-10-25 08:34:43 +02:00
|
|
|
this.options.path = null;
|
|
|
|
this.requests = {};
|
|
|
|
this.sockets = {};
|
|
|
|
this.freeSockets = {};
|
|
|
|
this.keepAliveMsecs = this.options.keepAliveMsecs || 1000;
|
|
|
|
this.keepAlive = this.options.keepAlive || false;
|
|
|
|
this.maxSockets = this.options.maxSockets || Agent.defaultMaxSockets;
|
|
|
|
this.maxFreeSockets = this.options.maxFreeSockets || 256;
|
2020-05-07 08:18:11 +02:00
|
|
|
this.scheduling = this.options.scheduling || 'fifo';
|
|
|
|
|
|
|
|
if (this.scheduling !== 'fifo' && this.scheduling !== 'lifo') {
|
|
|
|
throw new ERR_INVALID_OPT_VALUE('scheduling', this.scheduling);
|
|
|
|
}
|
2017-10-25 08:34:43 +02:00
|
|
|
|
|
|
|
this.on('free', (socket, options) => {
|
2019-03-26 05:21:27 +01:00
|
|
|
const name = this.getName(options);
|
2013-05-23 03:44:24 +02:00
|
|
|
debug('agent.on(free)', name);
|
2013-04-11 23:47:15 +02:00
|
|
|
|
2020-02-28 12:29:45 +01:00
|
|
|
// TODO(ronag): socket.destroy(err) might have been called
|
|
|
|
// before coming here and have an 'error' scheduled. In the
|
|
|
|
// case of socket.destroy() below this 'error' has no handler
|
|
|
|
// and could cause unhandled exception.
|
|
|
|
|
2020-04-12 22:40:58 +02:00
|
|
|
if (!socket.writable) {
|
|
|
|
socket.destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const requests = this.requests[name];
|
|
|
|
if (requests && requests.length) {
|
|
|
|
const req = requests.shift();
|
2018-06-08 06:42:27 +02:00
|
|
|
setRequestSocket(this, req, socket);
|
2020-04-12 22:40:58 +02:00
|
|
|
if (requests.length === 0) {
|
2017-10-25 08:34:43 +02:00
|
|
|
delete this.requests[name];
|
2013-04-11 23:47:15 +02:00
|
|
|
}
|
2020-04-12 22:40:58 +02:00
|
|
|
return;
|
2013-04-11 23:47:15 +02:00
|
|
|
}
|
2020-04-12 22:40:58 +02:00
|
|
|
|
|
|
|
// If there are no pending requests, then put it in
|
|
|
|
// the freeSockets pool, but only if we're allowed to do so.
|
|
|
|
const req = socket._httpMessage;
|
|
|
|
if (!req || !req.shouldKeepAlive || !this.keepAlive) {
|
|
|
|
socket.destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let freeSockets = this.freeSockets[name];
|
|
|
|
const freeLen = freeSockets ? freeSockets.length : 0;
|
|
|
|
let count = freeLen;
|
|
|
|
if (this.sockets[name])
|
|
|
|
count += this.sockets[name].length;
|
|
|
|
|
|
|
|
if (count > this.maxSockets ||
|
|
|
|
freeLen >= this.maxFreeSockets ||
|
|
|
|
!this.keepSocketAlive(socket)) {
|
|
|
|
socket.destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeSockets = freeSockets || [];
|
|
|
|
this.freeSockets[name] = freeSockets;
|
|
|
|
socket[async_id_symbol] = -1;
|
|
|
|
socket._httpMessage = null;
|
|
|
|
this.removeSocket(socket, options);
|
|
|
|
|
|
|
|
socket.once('error', freeSocketErrorListener);
|
|
|
|
freeSockets.push(socket);
|
2013-04-11 23:47:15 +02:00
|
|
|
});
|
2019-10-21 23:27:50 +02:00
|
|
|
|
|
|
|
// Don't emit keylog events unless there is a listener for them.
|
|
|
|
this.on('newListener', maybeEnableKeylog);
|
2013-04-11 23:47:15 +02:00
|
|
|
}
|
2019-11-12 16:30:48 +01:00
|
|
|
ObjectSetPrototypeOf(Agent.prototype, EventEmitter.prototype);
|
|
|
|
ObjectSetPrototypeOf(Agent, EventEmitter);
|
2013-04-11 23:47:15 +02:00
|
|
|
|
2019-10-21 23:27:50 +02:00
|
|
|
function maybeEnableKeylog(eventName) {
|
|
|
|
if (eventName === 'keylog') {
|
|
|
|
this.removeListener('newListener', maybeEnableKeylog);
|
|
|
|
// Future sockets will listen on keylog at creation.
|
|
|
|
const agent = this;
|
|
|
|
this[kOnKeylog] = function onkeylog(keylog) {
|
|
|
|
agent.emit('keylog', keylog, this);
|
|
|
|
};
|
|
|
|
// Existing sockets will start listening on keylog now.
|
2019-12-14 14:32:41 +01:00
|
|
|
for (const socket of ObjectValues(this.sockets)) {
|
|
|
|
socket.on('keylog', this[kOnKeylog]);
|
2019-10-21 23:27:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-16 00:10:53 +02:00
|
|
|
Agent.defaultMaxSockets = Infinity;
|
2013-04-11 23:47:15 +02:00
|
|
|
|
2013-05-21 23:02:18 +02:00
|
|
|
Agent.prototype.createConnection = net.createConnection;
|
2013-05-23 03:44:24 +02:00
|
|
|
|
|
|
|
// Get the key for a given set of request options
|
2016-10-12 13:03:13 +02:00
|
|
|
Agent.prototype.getName = function getName(options) {
|
2019-11-06 12:19:49 +01:00
|
|
|
let name = options.host || 'localhost';
|
2013-05-23 03:44:24 +02:00
|
|
|
|
|
|
|
name += ':';
|
|
|
|
if (options.port)
|
|
|
|
name += options.port;
|
2015-09-11 23:18:01 +02:00
|
|
|
|
2013-05-23 03:44:24 +02:00
|
|
|
name += ':';
|
|
|
|
if (options.localAddress)
|
|
|
|
name += options.localAddress;
|
2015-09-11 23:18:01 +02:00
|
|
|
|
2016-05-09 16:35:20 +02:00
|
|
|
// Pacify parallel/test-http-agent-getname by only appending
|
|
|
|
// the ':' when options.family is set.
|
|
|
|
if (options.family === 4 || options.family === 6)
|
2017-11-10 08:06:21 +01:00
|
|
|
name += `:${options.family}`;
|
2016-05-09 16:35:20 +02:00
|
|
|
|
2017-05-25 09:32:04 +02:00
|
|
|
if (options.socketPath)
|
2017-11-10 08:06:21 +01:00
|
|
|
name += `:${options.socketPath}`;
|
2017-05-25 09:32:04 +02:00
|
|
|
|
2013-05-23 03:44:24 +02:00
|
|
|
return name;
|
|
|
|
};
|
|
|
|
|
2018-03-25 16:27:38 +02:00
|
|
|
Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
|
|
|
|
localAddress/* legacy */) {
|
2016-02-11 01:59:25 +01:00
|
|
|
// Legacy API: addRequest(req, host, port, localAddress)
|
2013-08-07 19:23:45 +02:00
|
|
|
if (typeof options === 'string') {
|
|
|
|
options = {
|
|
|
|
host: options,
|
2017-04-24 08:19:30 +02:00
|
|
|
port,
|
|
|
|
localAddress
|
2013-08-07 19:23:45 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-18 03:15:57 +01:00
|
|
|
options = { ...options, ...this.options };
|
2017-05-25 09:32:04 +02:00
|
|
|
if (options.socketPath)
|
|
|
|
options.path = options.socketPath;
|
2016-03-14 16:56:02 +01:00
|
|
|
|
2019-04-19 21:51:24 +02:00
|
|
|
if (!options.servername && options.servername !== '')
|
2017-08-10 15:11:19 +02:00
|
|
|
options.servername = calculateServerName(options, req);
|
2016-09-18 13:34:48 +02:00
|
|
|
|
2019-03-26 05:21:27 +01:00
|
|
|
const name = this.getName(options);
|
2013-04-11 23:47:15 +02:00
|
|
|
if (!this.sockets[name]) {
|
|
|
|
this.sockets[name] = [];
|
|
|
|
}
|
2013-05-16 00:10:53 +02:00
|
|
|
|
2020-02-28 22:27:39 +01:00
|
|
|
const freeSockets = this.freeSockets[name];
|
|
|
|
let socket;
|
|
|
|
if (freeSockets) {
|
|
|
|
while (freeSockets.length && freeSockets[0].destroyed) {
|
|
|
|
freeSockets.shift();
|
|
|
|
}
|
2020-05-07 08:18:11 +02:00
|
|
|
socket = this.scheduling === 'fifo' ?
|
|
|
|
freeSockets.shift() :
|
|
|
|
freeSockets.pop();
|
2020-02-28 22:27:39 +01:00
|
|
|
if (!freeSockets.length)
|
|
|
|
delete this.freeSockets[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
const freeLen = freeSockets ? freeSockets.length : 0;
|
2019-03-26 05:21:27 +01:00
|
|
|
const sockLen = freeLen + this.sockets[name].length;
|
2013-08-05 05:54:52 +02:00
|
|
|
|
2020-02-28 22:27:39 +01:00
|
|
|
if (socket) {
|
2017-07-22 01:10:56 +02:00
|
|
|
// Guard against an uninitialized or user supplied Socket.
|
2019-05-05 21:29:32 +02:00
|
|
|
const handle = socket._handle;
|
|
|
|
if (handle && typeof handle.asyncReset === 'function') {
|
2018-10-01 17:11:25 +02:00
|
|
|
// Assign the handle a new asyncId and run any destroy()/init() hooks.
|
2019-05-05 21:29:32 +02:00
|
|
|
handle.asyncReset(new ReusedHandle(handle.getProviderType(), handle));
|
|
|
|
socket[async_id_symbol] = handle.getAsyncId();
|
2017-07-22 01:10:56 +02:00
|
|
|
}
|
2013-05-16 00:10:53 +02:00
|
|
|
|
2017-05-12 22:53:15 +02:00
|
|
|
this.reuseSocket(socket, req);
|
2018-06-08 06:42:27 +02:00
|
|
|
setRequestSocket(this, req, socket);
|
2013-08-05 05:54:52 +02:00
|
|
|
this.sockets[name].push(socket);
|
|
|
|
} else if (sockLen < this.maxSockets) {
|
|
|
|
debug('call onSocket', sockLen, freeLen);
|
2013-04-11 23:47:15 +02:00
|
|
|
// If we are under maxSockets create a new one.
|
2020-04-30 23:36:19 +02:00
|
|
|
this.createSocket(req, options, (err, socket) => {
|
|
|
|
if (err)
|
|
|
|
req.onSocket(socket, err);
|
|
|
|
else
|
|
|
|
setRequestSocket(this, req, socket);
|
|
|
|
});
|
2013-04-11 23:47:15 +02:00
|
|
|
} else {
|
2013-05-23 03:44:24 +02:00
|
|
|
debug('wait for socket');
|
2013-04-11 23:47:15 +02:00
|
|
|
// We are over limit so we'll add it to the queue.
|
|
|
|
if (!this.requests[name]) {
|
|
|
|
this.requests[name] = [];
|
|
|
|
}
|
|
|
|
this.requests[name].push(req);
|
|
|
|
}
|
|
|
|
};
|
2013-05-15 23:24:08 +02:00
|
|
|
|
2016-10-12 13:03:13 +02:00
|
|
|
Agent.prototype.createSocket = function createSocket(req, options, cb) {
|
2018-12-18 03:15:57 +01:00
|
|
|
options = { ...options, ...this.options };
|
2017-05-25 09:32:04 +02:00
|
|
|
if (options.socketPath)
|
|
|
|
options.path = options.socketPath;
|
2013-04-11 23:47:15 +02:00
|
|
|
|
2019-04-19 21:51:24 +02:00
|
|
|
if (!options.servername && options.servername !== '')
|
2017-08-10 15:11:19 +02:00
|
|
|
options.servername = calculateServerName(options, req);
|
2013-04-11 23:47:15 +02:00
|
|
|
|
2019-03-26 05:21:27 +01:00
|
|
|
const name = this.getName(options);
|
2015-07-23 06:18:38 +02:00
|
|
|
options._agentKey = name;
|
2013-05-23 03:44:24 +02:00
|
|
|
|
|
|
|
debug('createConnection', name, options);
|
2013-11-03 23:32:03 +01:00
|
|
|
options.encoding = null;
|
2017-07-03 05:52:41 +02:00
|
|
|
|
2020-04-12 22:40:58 +02:00
|
|
|
const oncreate = once((err, s) => {
|
2016-01-12 05:41:01 +01:00
|
|
|
if (err)
|
|
|
|
return cb(err);
|
2017-10-25 08:34:43 +02:00
|
|
|
if (!this.sockets[name]) {
|
|
|
|
this.sockets[name] = [];
|
2016-01-12 05:41:01 +01:00
|
|
|
}
|
2017-10-25 08:34:43 +02:00
|
|
|
this.sockets[name].push(s);
|
|
|
|
debug('sockets', name, this.sockets[name].length);
|
|
|
|
installListeners(this, s, options);
|
2016-01-12 05:41:01 +01:00
|
|
|
cb(null, s);
|
2020-04-12 22:40:58 +02:00
|
|
|
});
|
2017-10-25 08:34:43 +02:00
|
|
|
|
|
|
|
const newSocket = this.createConnection(options, oncreate);
|
|
|
|
if (newSocket)
|
|
|
|
oncreate(null, newSocket);
|
2013-04-11 23:47:15 +02:00
|
|
|
};
|
2013-05-15 23:24:08 +02:00
|
|
|
|
2017-08-10 15:11:19 +02:00
|
|
|
function calculateServerName(options, req) {
|
|
|
|
let servername = options.host;
|
|
|
|
const hostHeader = req.getHeader('host');
|
|
|
|
if (hostHeader) {
|
2019-09-12 09:22:05 +02:00
|
|
|
if (typeof hostHeader !== 'string') {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('options.headers.host',
|
|
|
|
'String', hostHeader);
|
|
|
|
}
|
|
|
|
|
2017-08-10 15:11:19 +02:00
|
|
|
// abc => abc
|
|
|
|
// abc:123 => abc
|
|
|
|
// [::1] => ::1
|
|
|
|
// [::1]:123 => ::1
|
|
|
|
if (hostHeader.startsWith('[')) {
|
|
|
|
const index = hostHeader.indexOf(']');
|
|
|
|
if (index === -1) {
|
|
|
|
// Leading '[', but no ']'. Need to do something...
|
|
|
|
servername = hostHeader;
|
|
|
|
} else {
|
|
|
|
servername = hostHeader.substr(1, index - 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
servername = hostHeader.split(':', 1)[0];
|
|
|
|
}
|
|
|
|
}
|
2019-06-02 18:11:48 +02:00
|
|
|
// Don't implicitly set invalid (IP) servernames.
|
|
|
|
if (net.isIP(servername))
|
|
|
|
servername = '';
|
2017-08-10 15:11:19 +02:00
|
|
|
return servername;
|
|
|
|
}
|
|
|
|
|
2016-12-05 02:51:51 +01:00
|
|
|
function installListeners(agent, s, options) {
|
|
|
|
function onFree() {
|
|
|
|
debug('CLIENT socket onFree');
|
|
|
|
agent.emit('free', s, options);
|
|
|
|
}
|
|
|
|
s.on('free', onFree);
|
|
|
|
|
|
|
|
function onClose(err) {
|
|
|
|
debug('CLIENT socket onClose');
|
|
|
|
// This is the only place where sockets get removed from the Agent.
|
|
|
|
// If you want to remove a socket from the pool, just close it.
|
|
|
|
// All socket errors end in a close event anyway.
|
|
|
|
agent.removeSocket(s, options);
|
|
|
|
}
|
|
|
|
s.on('close', onClose);
|
|
|
|
|
2020-02-28 22:27:39 +01:00
|
|
|
function onTimeout() {
|
|
|
|
debug('CLIENT socket onTimeout');
|
|
|
|
|
|
|
|
// Destroy if in free list.
|
|
|
|
// TODO(ronag): Always destroy, even if not in free list.
|
|
|
|
const sockets = agent.freeSockets;
|
|
|
|
for (const name of ObjectKeys(sockets)) {
|
|
|
|
if (sockets[name].includes(s)) {
|
|
|
|
return s.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.on('timeout', onTimeout);
|
|
|
|
|
2016-12-05 02:51:51 +01:00
|
|
|
function onRemove() {
|
|
|
|
// We need this function for cases like HTTP 'upgrade'
|
|
|
|
// (defined by WebSockets) where we need to remove a socket from the
|
|
|
|
// pool because it'll be locked up indefinitely
|
|
|
|
debug('CLIENT socket onRemove');
|
|
|
|
agent.removeSocket(s, options);
|
|
|
|
s.removeListener('close', onClose);
|
|
|
|
s.removeListener('free', onFree);
|
2020-02-28 22:27:39 +01:00
|
|
|
s.removeListener('timeout', onTimeout);
|
2016-12-05 02:51:51 +01:00
|
|
|
s.removeListener('agentRemove', onRemove);
|
|
|
|
}
|
|
|
|
s.on('agentRemove', onRemove);
|
2019-10-21 23:27:50 +02:00
|
|
|
|
|
|
|
if (agent[kOnKeylog]) {
|
|
|
|
s.on('keylog', agent[kOnKeylog]);
|
|
|
|
}
|
2016-12-05 02:51:51 +01:00
|
|
|
}
|
|
|
|
|
2016-10-12 13:03:13 +02:00
|
|
|
Agent.prototype.removeSocket = function removeSocket(s, options) {
|
2019-03-26 05:21:27 +01:00
|
|
|
const name = this.getName(options);
|
2016-01-12 05:41:01 +01:00
|
|
|
debug('removeSocket', name, 'writable:', s.writable);
|
2019-03-26 05:21:27 +01:00
|
|
|
const sets = [this.sockets];
|
2013-11-06 12:58:03 +01:00
|
|
|
|
|
|
|
// If the socket was destroyed, remove it from the free buffers too.
|
2016-01-12 05:41:01 +01:00
|
|
|
if (!s.writable)
|
2013-11-06 12:58:03 +01:00
|
|
|
sets.push(this.freeSockets);
|
|
|
|
|
2019-12-14 14:32:41 +01:00
|
|
|
for (const sockets of sets) {
|
2013-11-06 12:58:03 +01:00
|
|
|
if (sockets[name]) {
|
2019-11-06 12:19:49 +01:00
|
|
|
const index = sockets[name].indexOf(s);
|
2013-11-06 12:58:03 +01:00
|
|
|
if (index !== -1) {
|
|
|
|
sockets[name].splice(index, 1);
|
|
|
|
// Don't leak
|
|
|
|
if (sockets[name].length === 0)
|
|
|
|
delete sockets[name];
|
2013-04-11 23:47:15 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-14 05:15:24 +02:00
|
|
|
}
|
|
|
|
|
2013-04-11 23:47:15 +02:00
|
|
|
if (this.requests[name] && this.requests[name].length) {
|
2013-05-23 03:44:24 +02:00
|
|
|
debug('removeSocket, have a request, make a socket');
|
2018-06-08 06:42:27 +02:00
|
|
|
const req = this.requests[name][0];
|
2013-05-23 03:44:24 +02:00
|
|
|
// If we have pending requests and a socket gets closed make a new one
|
2020-04-30 23:36:19 +02:00
|
|
|
this.createSocket(req, options, (err, socket) => {
|
|
|
|
if (err)
|
|
|
|
req.onSocket(socket, err);
|
|
|
|
else
|
|
|
|
socket.emit('free');
|
|
|
|
});
|
2013-04-11 23:47:15 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-12 22:53:15 +02:00
|
|
|
Agent.prototype.keepSocketAlive = function keepSocketAlive(socket) {
|
|
|
|
socket.setKeepAlive(true, this.keepAliveMsecs);
|
|
|
|
socket.unref();
|
|
|
|
|
2020-04-28 17:42:34 +02:00
|
|
|
const agentTimeout = this.options.timeout || 0;
|
|
|
|
if (socket.timeout !== agentTimeout) {
|
|
|
|
socket.setTimeout(agentTimeout);
|
|
|
|
}
|
|
|
|
|
2017-05-12 22:53:15 +02:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
Agent.prototype.reuseSocket = function reuseSocket(socket, req) {
|
|
|
|
debug('have free socket');
|
2020-02-28 12:29:45 +01:00
|
|
|
socket.removeListener('error', freeSocketErrorListener);
|
2019-09-26 05:56:59 +02:00
|
|
|
req.reusedSocket = true;
|
2017-05-12 22:53:15 +02:00
|
|
|
socket.ref();
|
|
|
|
};
|
|
|
|
|
2016-10-12 13:03:13 +02:00
|
|
|
Agent.prototype.destroy = function destroy() {
|
2019-12-14 14:32:41 +01:00
|
|
|
for (const set of [this.freeSockets, this.sockets]) {
|
|
|
|
for (const key of ObjectKeys(set)) {
|
|
|
|
for (const setName of set[key]) {
|
|
|
|
setName.destroy();
|
2014-08-14 05:15:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-16 00:10:53 +02:00
|
|
|
};
|
|
|
|
|
2018-06-08 06:42:27 +02:00
|
|
|
function setRequestSocket(agent, req, socket) {
|
|
|
|
req.onSocket(socket);
|
|
|
|
const agentTimeout = agent.options.timeout || 0;
|
|
|
|
if (req.timeout === undefined || req.timeout === agentTimeout) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
socket.setTimeout(req.timeout);
|
|
|
|
}
|
|
|
|
|
2017-02-28 01:09:32 +01:00
|
|
|
module.exports = {
|
|
|
|
Agent,
|
|
|
|
globalAgent: new Agent()
|
|
|
|
};
|