2017-01-03 21:16:48 +00: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 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-12 15:30:48 +00:00
|
|
|
const {
|
2022-06-21 12:50:55 +00:00
|
|
|
NumberParseInt,
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectKeys,
|
|
|
|
ObjectSetPrototypeOf,
|
|
|
|
ObjectValues,
|
2019-11-30 15:55:29 +00:00
|
|
|
Symbol,
|
2019-11-12 15:30:48 +00:00
|
|
|
} = primordials;
|
2019-04-09 07:55:53 +00:00
|
|
|
|
2015-01-21 16:36:59 +00:00
|
|
|
const net = require('net');
|
2015-09-16 22:45:29 +00:00
|
|
|
const EventEmitter = require('events');
|
2020-03-14 11:55:44 +00:00
|
|
|
let debug = require('internal/util/debuglog').debuglog('http', (fn) => {
|
|
|
|
debug = fn;
|
|
|
|
});
|
2020-07-20 13:05:33 +00:00
|
|
|
const { AsyncResource } = require('async_hooks');
|
2018-02-11 21:35:59 +00:00
|
|
|
const { async_id_symbol } = require('internal/async_hooks').symbols;
|
2022-05-21 09:52:47 +00:00
|
|
|
const {
|
|
|
|
kEmptyObject,
|
|
|
|
once,
|
|
|
|
} = require('internal/util');
|
2021-05-16 11:22:48 +00:00
|
|
|
const {
|
|
|
|
validateNumber,
|
|
|
|
validateOneOf,
|
|
|
|
validateString,
|
|
|
|
} = require('internal/validators');
|
2020-04-12 20:40:58 +00:00
|
|
|
|
2019-10-21 21:27:50 +00:00
|
|
|
const kOnKeylog = Symbol('onkeylog');
|
2020-05-28 15:15:55 +00:00
|
|
|
const kRequestOptions = Symbol('requestOptions');
|
2020-07-20 13:05:33 +00:00
|
|
|
const kRequestAsyncResource = Symbol('requestAsyncResource');
|
2024-09-09 13:17:10 +00:00
|
|
|
|
|
|
|
// TODO(jazelly): make this configurable
|
|
|
|
const HTTP_AGENT_KEEP_ALIVE_TIMEOUT_BUFFER = 1000;
|
2013-04-11 21:47:15 +00: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 19:29:32 +00:00
|
|
|
class ReusedHandle {
|
|
|
|
constructor(type, handle) {
|
|
|
|
this.type = type;
|
|
|
|
this.handle = handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 11:29:45 +00: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 21:47:15 +00:00
|
|
|
function Agent(options) {
|
2013-05-15 22:10:53 +00:00
|
|
|
if (!(this instanceof Agent))
|
|
|
|
return new Agent(options);
|
|
|
|
|
2024-07-06 08:33:16 +00:00
|
|
|
EventEmitter.call(this);
|
2013-04-11 21:47:15 +00:00
|
|
|
|
2017-10-25 06:34:43 +00:00
|
|
|
this.defaultPort = 80;
|
|
|
|
this.protocol = 'http:';
|
2013-05-23 01:44:24 +00:00
|
|
|
|
2020-12-06 13:52:08 +00:00
|
|
|
this.options = { __proto__: null, ...options };
|
2013-05-23 01:44:24 +00:00
|
|
|
|
2022-03-09 14:37:49 +00:00
|
|
|
if (this.options.noDelay === undefined)
|
|
|
|
this.options.noDelay = true;
|
|
|
|
|
2018-12-03 16:15:45 +00:00
|
|
|
// Don't confuse net and make it think that we're connecting to a pipe
|
2017-10-25 06:34:43 +00:00
|
|
|
this.options.path = null;
|
2023-01-10 05:38:36 +00:00
|
|
|
this.requests = { __proto__: null };
|
|
|
|
this.sockets = { __proto__: null };
|
|
|
|
this.freeSockets = { __proto__: null };
|
2017-10-25 06:34:43 +00:00
|
|
|
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-12-30 12:02:27 +00:00
|
|
|
this.scheduling = this.options.scheduling || 'lifo';
|
2020-05-28 15:15:55 +00:00
|
|
|
this.maxTotalSockets = this.options.maxTotalSockets;
|
|
|
|
this.totalSocketCount = 0;
|
2020-05-07 06:18:11 +00:00
|
|
|
|
2020-08-08 16:01:59 +00:00
|
|
|
validateOneOf(this.scheduling, 'scheduling', ['fifo', 'lifo']);
|
2017-10-25 06:34:43 +00:00
|
|
|
|
2020-05-28 15:15:55 +00:00
|
|
|
if (this.maxTotalSockets !== undefined) {
|
2023-01-13 14:39:54 +00:00
|
|
|
validateNumber(this.maxTotalSockets, 'maxTotalSockets', 1);
|
2020-05-28 15:15:55 +00:00
|
|
|
} else {
|
|
|
|
this.maxTotalSockets = Infinity;
|
|
|
|
}
|
|
|
|
|
2017-10-25 06:34:43 +00:00
|
|
|
this.on('free', (socket, options) => {
|
2019-03-26 04:21:27 +00:00
|
|
|
const name = this.getName(options);
|
2013-05-23 01:44:24 +00:00
|
|
|
debug('agent.on(free)', name);
|
2013-04-11 21:47:15 +00:00
|
|
|
|
2020-02-28 11:29:45 +00: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 20:40:58 +00:00
|
|
|
if (!socket.writable) {
|
|
|
|
socket.destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const requests = this.requests[name];
|
2024-09-24 19:48:15 +00:00
|
|
|
if (requests?.length) {
|
2024-07-06 08:33:16 +00:00
|
|
|
const req = requests.shift();
|
2020-07-20 13:05:33 +00:00
|
|
|
const reqAsyncRes = req[kRequestAsyncResource];
|
|
|
|
if (reqAsyncRes) {
|
|
|
|
// Run request within the original async context.
|
|
|
|
reqAsyncRes.runInAsyncScope(() => {
|
|
|
|
asyncResetHandle(socket);
|
|
|
|
setRequestSocket(this, req, socket);
|
|
|
|
});
|
|
|
|
req[kRequestAsyncResource] = null;
|
|
|
|
} else {
|
|
|
|
setRequestSocket(this, req, socket);
|
|
|
|
}
|
2020-04-12 20:40:58 +00:00
|
|
|
if (requests.length === 0) {
|
2017-10-25 06:34:43 +00:00
|
|
|
delete this.requests[name];
|
2013-04-11 21:47:15 +00:00
|
|
|
}
|
2020-04-12 20:40:58 +00:00
|
|
|
return;
|
2013-04-11 21:47:15 +00:00
|
|
|
}
|
2020-04-12 20:40:58 +00: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;
|
|
|
|
}
|
|
|
|
|
2020-05-25 10:36:33 +00:00
|
|
|
const freeSockets = this.freeSockets[name] || [];
|
|
|
|
const freeLen = freeSockets.length;
|
2020-04-12 20:40:58 +00:00
|
|
|
let count = freeLen;
|
|
|
|
if (this.sockets[name])
|
|
|
|
count += this.sockets[name].length;
|
|
|
|
|
2020-05-28 15:15:55 +00:00
|
|
|
if (this.totalSocketCount > this.maxTotalSockets ||
|
|
|
|
count > this.maxSockets ||
|
2020-04-12 20:40:58 +00:00
|
|
|
freeLen >= this.maxFreeSockets ||
|
|
|
|
!this.keepSocketAlive(socket)) {
|
|
|
|
socket.destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.freeSockets[name] = freeSockets;
|
|
|
|
socket[async_id_symbol] = -1;
|
|
|
|
socket._httpMessage = null;
|
|
|
|
this.removeSocket(socket, options);
|
|
|
|
|
|
|
|
socket.once('error', freeSocketErrorListener);
|
2024-07-06 08:33:16 +00:00
|
|
|
freeSockets.push(socket);
|
2013-04-11 21:47:15 +00:00
|
|
|
});
|
2019-10-21 21:27:50 +00:00
|
|
|
|
|
|
|
// Don't emit keylog events unless there is a listener for them.
|
|
|
|
this.on('newListener', maybeEnableKeylog);
|
2013-04-11 21:47:15 +00:00
|
|
|
}
|
2019-11-12 15:30:48 +00:00
|
|
|
ObjectSetPrototypeOf(Agent.prototype, EventEmitter.prototype);
|
|
|
|
ObjectSetPrototypeOf(Agent, EventEmitter);
|
2013-04-11 21:47:15 +00:00
|
|
|
|
2019-10-21 21:27:50 +00: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.
|
2021-01-29 13:53:16 +00:00
|
|
|
const sockets = ObjectValues(this.sockets);
|
|
|
|
for (let i = 0; i < sockets.length; i++) {
|
|
|
|
sockets[i].on('keylog', this[kOnKeylog]);
|
2019-10-21 21:27:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-15 22:10:53 +00:00
|
|
|
Agent.defaultMaxSockets = Infinity;
|
2013-04-11 21:47:15 +00:00
|
|
|
|
2013-05-21 21:02:18 +00:00
|
|
|
Agent.prototype.createConnection = net.createConnection;
|
2013-05-23 01:44:24 +00:00
|
|
|
|
|
|
|
// Get the key for a given set of request options
|
2022-05-21 09:52:47 +00:00
|
|
|
Agent.prototype.getName = function getName(options = kEmptyObject) {
|
2019-11-06 11:19:49 +00:00
|
|
|
let name = options.host || 'localhost';
|
2013-05-23 01:44:24 +00:00
|
|
|
|
|
|
|
name += ':';
|
|
|
|
if (options.port)
|
|
|
|
name += options.port;
|
2015-09-11 21:18:01 +00:00
|
|
|
|
2013-05-23 01:44:24 +00:00
|
|
|
name += ':';
|
|
|
|
if (options.localAddress)
|
|
|
|
name += options.localAddress;
|
2015-09-11 21:18:01 +00:00
|
|
|
|
2016-05-09 14:35:20 +00: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 07:06:21 +00:00
|
|
|
name += `:${options.family}`;
|
2016-05-09 14:35:20 +00:00
|
|
|
|
2017-05-25 07:32:04 +00:00
|
|
|
if (options.socketPath)
|
2017-11-10 07:06:21 +00:00
|
|
|
name += `:${options.socketPath}`;
|
2017-05-25 07:32:04 +00:00
|
|
|
|
2013-05-23 01:44:24 +00:00
|
|
|
return name;
|
|
|
|
};
|
|
|
|
|
2018-03-25 14:27:38 +00:00
|
|
|
Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
|
|
|
|
localAddress/* legacy */) {
|
2016-02-11 00:59:25 +00:00
|
|
|
// Legacy API: addRequest(req, host, port, localAddress)
|
2013-08-07 17:23:45 +00:00
|
|
|
if (typeof options === 'string') {
|
|
|
|
options = {
|
2020-12-06 13:52:08 +00:00
|
|
|
__proto__: null,
|
2013-08-07 17:23:45 +00:00
|
|
|
host: options,
|
2017-04-24 06:19:30 +00:00
|
|
|
port,
|
2023-02-26 10:34:02 +00:00
|
|
|
localAddress,
|
2013-08-07 17:23:45 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-06 13:52:08 +00:00
|
|
|
options = { __proto__: null, ...options, ...this.options };
|
2017-05-25 07:32:04 +00:00
|
|
|
if (options.socketPath)
|
|
|
|
options.path = options.socketPath;
|
2016-03-14 15:56:02 +00:00
|
|
|
|
2023-10-06 13:31:05 +00:00
|
|
|
normalizeServerName(options, req);
|
2016-09-18 11:34:48 +00:00
|
|
|
|
2019-03-26 04:21:27 +00:00
|
|
|
const name = this.getName(options);
|
2024-10-09 06:42:16 +00:00
|
|
|
this.sockets[name] ||= [];
|
2013-05-15 22:10:53 +00:00
|
|
|
|
2020-02-28 21:27:39 +00:00
|
|
|
const freeSockets = this.freeSockets[name];
|
|
|
|
let socket;
|
|
|
|
if (freeSockets) {
|
|
|
|
while (freeSockets.length && freeSockets[0].destroyed) {
|
2024-07-06 08:33:16 +00:00
|
|
|
freeSockets.shift();
|
2020-02-28 21:27:39 +00:00
|
|
|
}
|
2020-05-07 06:18:11 +00:00
|
|
|
socket = this.scheduling === 'fifo' ?
|
2024-07-06 08:33:16 +00:00
|
|
|
freeSockets.shift() :
|
|
|
|
freeSockets.pop();
|
2020-02-28 21:27:39 +00:00
|
|
|
if (!freeSockets.length)
|
|
|
|
delete this.freeSockets[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
const freeLen = freeSockets ? freeSockets.length : 0;
|
2019-03-26 04:21:27 +00:00
|
|
|
const sockLen = freeLen + this.sockets[name].length;
|
2013-08-05 03:54:52 +00:00
|
|
|
|
2020-02-28 21:27:39 +00:00
|
|
|
if (socket) {
|
2020-07-20 13:05:33 +00:00
|
|
|
asyncResetHandle(socket);
|
2017-05-12 20:53:15 +00:00
|
|
|
this.reuseSocket(socket, req);
|
2018-06-08 04:42:27 +00:00
|
|
|
setRequestSocket(this, req, socket);
|
2024-07-06 08:33:16 +00:00
|
|
|
this.sockets[name].push(socket);
|
2020-05-28 15:15:55 +00:00
|
|
|
} else if (sockLen < this.maxSockets &&
|
|
|
|
this.totalSocketCount < this.maxTotalSockets) {
|
2013-08-05 03:54:52 +00:00
|
|
|
debug('call onSocket', sockLen, freeLen);
|
2013-04-11 21:47:15 +00:00
|
|
|
// If we are under maxSockets create a new one.
|
2020-04-30 21:36:19 +00:00
|
|
|
this.createSocket(req, options, (err, socket) => {
|
|
|
|
if (err)
|
|
|
|
req.onSocket(socket, err);
|
|
|
|
else
|
|
|
|
setRequestSocket(this, req, socket);
|
|
|
|
});
|
2013-04-11 21:47:15 +00:00
|
|
|
} else {
|
2013-05-23 01:44:24 +00:00
|
|
|
debug('wait for socket');
|
2013-04-11 21:47:15 +00:00
|
|
|
// We are over limit so we'll add it to the queue.
|
2024-10-09 06:42:16 +00:00
|
|
|
this.requests[name] ||= [];
|
2020-05-28 15:15:55 +00:00
|
|
|
|
|
|
|
// Used to create sockets for pending requests from different origin
|
|
|
|
req[kRequestOptions] = options;
|
2020-07-20 13:05:33 +00:00
|
|
|
// Used to capture the original async context.
|
|
|
|
req[kRequestAsyncResource] = new AsyncResource('QueuedRequest');
|
2020-05-28 15:15:55 +00:00
|
|
|
|
2024-07-06 08:33:16 +00:00
|
|
|
this.requests[name].push(req);
|
2013-04-11 21:47:15 +00:00
|
|
|
}
|
|
|
|
};
|
2013-05-15 21:24:08 +00:00
|
|
|
|
2016-10-12 11:03:13 +00:00
|
|
|
Agent.prototype.createSocket = function createSocket(req, options, cb) {
|
2020-12-06 13:52:08 +00:00
|
|
|
options = { __proto__: null, ...options, ...this.options };
|
2017-05-25 07:32:04 +00:00
|
|
|
if (options.socketPath)
|
|
|
|
options.path = options.socketPath;
|
2013-04-11 21:47:15 +00:00
|
|
|
|
2023-10-06 13:31:05 +00:00
|
|
|
normalizeServerName(options, req);
|
2013-04-11 21:47:15 +00:00
|
|
|
|
2019-03-26 04:21:27 +00:00
|
|
|
const name = this.getName(options);
|
2015-07-23 04:18:38 +00:00
|
|
|
options._agentKey = name;
|
2013-05-23 01:44:24 +00:00
|
|
|
|
|
|
|
debug('createConnection', name, options);
|
2013-11-03 22:32:03 +00:00
|
|
|
options.encoding = null;
|
2017-07-03 03:52:41 +00:00
|
|
|
|
2020-04-12 20:40:58 +00:00
|
|
|
const oncreate = once((err, s) => {
|
2016-01-12 04:41:01 +00:00
|
|
|
if (err)
|
|
|
|
return cb(err);
|
2024-10-09 06:42:16 +00:00
|
|
|
this.sockets[name] ||= [];
|
2024-07-06 08:33:16 +00:00
|
|
|
this.sockets[name].push(s);
|
2020-05-28 15:15:55 +00:00
|
|
|
this.totalSocketCount++;
|
|
|
|
debug('sockets', name, this.sockets[name].length, this.totalSocketCount);
|
2017-10-25 06:34:43 +00:00
|
|
|
installListeners(this, s, options);
|
2016-01-12 04:41:01 +00:00
|
|
|
cb(null, s);
|
2020-04-12 20:40:58 +00:00
|
|
|
});
|
2022-06-18 09:30:04 +00:00
|
|
|
// When keepAlive is true, pass the related options to createConnection
|
|
|
|
if (this.keepAlive) {
|
|
|
|
options.keepAlive = this.keepAlive;
|
|
|
|
options.keepAliveInitialDelay = this.keepAliveMsecs;
|
|
|
|
}
|
2017-10-25 06:34:43 +00:00
|
|
|
const newSocket = this.createConnection(options, oncreate);
|
|
|
|
if (newSocket)
|
|
|
|
oncreate(null, newSocket);
|
2013-04-11 21:47:15 +00:00
|
|
|
};
|
2013-05-15 21:24:08 +00:00
|
|
|
|
2023-10-06 13:31:05 +00:00
|
|
|
function normalizeServerName(options, req) {
|
|
|
|
if (!options.servername && options.servername !== '')
|
|
|
|
options.servername = calculateServerName(options, req);
|
|
|
|
}
|
|
|
|
|
2017-08-10 13:11:19 +00:00
|
|
|
function calculateServerName(options, req) {
|
|
|
|
let servername = options.host;
|
|
|
|
const hostHeader = req.getHeader('host');
|
|
|
|
if (hostHeader) {
|
2021-05-16 11:22:48 +00:00
|
|
|
validateString(hostHeader, 'options.headers.host');
|
2019-09-12 07:22:05 +00:00
|
|
|
|
2017-08-10 13:11:19 +00:00
|
|
|
// abc => abc
|
|
|
|
// abc:123 => abc
|
|
|
|
// [::1] => ::1
|
|
|
|
// [::1]:123 => ::1
|
2024-10-19 10:18:10 +00:00
|
|
|
if (hostHeader[0] === '[') {
|
2024-07-06 08:33:16 +00:00
|
|
|
const index = hostHeader.indexOf(']');
|
2017-08-10 13:11:19 +00:00
|
|
|
if (index === -1) {
|
|
|
|
// Leading '[', but no ']'. Need to do something...
|
|
|
|
servername = hostHeader;
|
|
|
|
} else {
|
2024-07-06 08:33:16 +00:00
|
|
|
servername = hostHeader.substring(1, index);
|
2017-08-10 13:11:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-07-06 08:33:16 +00:00
|
|
|
servername = hostHeader.split(':', 1)[0];
|
2017-08-10 13:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-02 16:11:48 +00:00
|
|
|
// Don't implicitly set invalid (IP) servernames.
|
|
|
|
if (net.isIP(servername))
|
|
|
|
servername = '';
|
2017-08-10 13:11:19 +00:00
|
|
|
return servername;
|
|
|
|
}
|
|
|
|
|
2016-12-05 01:51:51 +00: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.
|
2021-10-24 11:34:45 +00:00
|
|
|
agent.totalSocketCount--;
|
2016-12-05 01:51:51 +00:00
|
|
|
agent.removeSocket(s, options);
|
|
|
|
}
|
|
|
|
s.on('close', onClose);
|
|
|
|
|
2020-02-28 21:27:39 +00: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;
|
2024-07-06 08:33:16 +00:00
|
|
|
if (ObjectKeys(sockets).some((name) => sockets[name].includes(s))) {
|
2021-02-12 11:10:57 +00:00
|
|
|
return s.destroy();
|
2020-02-28 21:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s.on('timeout', onTimeout);
|
|
|
|
|
2016-12-05 01:51:51 +00: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');
|
2021-10-24 11:34:45 +00:00
|
|
|
agent.totalSocketCount--;
|
2016-12-05 01:51:51 +00:00
|
|
|
agent.removeSocket(s, options);
|
|
|
|
s.removeListener('close', onClose);
|
|
|
|
s.removeListener('free', onFree);
|
2020-02-28 21:27:39 +00:00
|
|
|
s.removeListener('timeout', onTimeout);
|
2016-12-05 01:51:51 +00:00
|
|
|
s.removeListener('agentRemove', onRemove);
|
|
|
|
}
|
|
|
|
s.on('agentRemove', onRemove);
|
2019-10-21 21:27:50 +00:00
|
|
|
|
|
|
|
if (agent[kOnKeylog]) {
|
|
|
|
s.on('keylog', agent[kOnKeylog]);
|
|
|
|
}
|
2016-12-05 01:51:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 11:03:13 +00:00
|
|
|
Agent.prototype.removeSocket = function removeSocket(s, options) {
|
2019-03-26 04:21:27 +00:00
|
|
|
const name = this.getName(options);
|
2016-01-12 04:41:01 +00:00
|
|
|
debug('removeSocket', name, 'writable:', s.writable);
|
2019-03-26 04:21:27 +00:00
|
|
|
const sets = [this.sockets];
|
2013-11-06 11:58:03 +00:00
|
|
|
|
|
|
|
// If the socket was destroyed, remove it from the free buffers too.
|
2016-01-12 04:41:01 +00:00
|
|
|
if (!s.writable)
|
2024-07-06 08:33:16 +00:00
|
|
|
sets.push(this.freeSockets);
|
2013-11-06 11:58:03 +00:00
|
|
|
|
2021-01-29 13:53:16 +00:00
|
|
|
for (let sk = 0; sk < sets.length; sk++) {
|
|
|
|
const sockets = sets[sk];
|
|
|
|
|
2013-11-06 11:58:03 +00:00
|
|
|
if (sockets[name]) {
|
2024-07-06 08:33:16 +00:00
|
|
|
const index = sockets[name].indexOf(s);
|
2013-11-06 11:58:03 +00:00
|
|
|
if (index !== -1) {
|
2024-07-06 08:33:16 +00:00
|
|
|
sockets[name].splice(index, 1);
|
2013-11-06 11:58:03 +00:00
|
|
|
// Don't leak
|
|
|
|
if (sockets[name].length === 0)
|
|
|
|
delete sockets[name];
|
2013-04-11 21:47:15 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-14 03:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 15:15:55 +00:00
|
|
|
let req;
|
2024-09-24 19:48:15 +00:00
|
|
|
if (this.requests[name]?.length) {
|
2013-05-23 01:44:24 +00:00
|
|
|
debug('removeSocket, have a request, make a socket');
|
2020-05-28 15:15:55 +00:00
|
|
|
req = this.requests[name][0];
|
|
|
|
} else {
|
|
|
|
// TODO(rickyes): this logic will not be FIFO across origins.
|
|
|
|
// There might be older requests in a different origin, but
|
|
|
|
// if the origin which releases the socket has pending requests
|
|
|
|
// that will be prioritized.
|
2021-02-12 11:10:57 +00:00
|
|
|
const keys = ObjectKeys(this.requests);
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const prop = keys[i];
|
2020-05-28 15:15:55 +00:00
|
|
|
// Check whether this specific origin is already at maxSockets
|
2024-09-24 19:48:15 +00:00
|
|
|
if (this.sockets[prop]?.length) break;
|
2020-05-28 15:15:55 +00:00
|
|
|
debug('removeSocket, have a request with different origin,' +
|
|
|
|
' make a socket');
|
|
|
|
req = this.requests[prop][0];
|
|
|
|
options = req[kRequestOptions];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req && options) {
|
|
|
|
req[kRequestOptions] = undefined;
|
2013-05-23 01:44:24 +00:00
|
|
|
// If we have pending requests and a socket gets closed make a new one
|
2020-04-30 21:36:19 +00:00
|
|
|
this.createSocket(req, options, (err, socket) => {
|
|
|
|
if (err)
|
|
|
|
req.onSocket(socket, err);
|
|
|
|
else
|
|
|
|
socket.emit('free');
|
|
|
|
});
|
2013-04-11 21:47:15 +00:00
|
|
|
}
|
2020-05-28 15:15:55 +00:00
|
|
|
|
2013-04-11 21:47:15 +00:00
|
|
|
};
|
|
|
|
|
2017-05-12 20:53:15 +00:00
|
|
|
Agent.prototype.keepSocketAlive = function keepSocketAlive(socket) {
|
|
|
|
socket.setKeepAlive(true, this.keepAliveMsecs);
|
|
|
|
socket.unref();
|
|
|
|
|
2022-06-21 12:50:55 +00:00
|
|
|
let agentTimeout = this.options.timeout || 0;
|
2024-09-09 13:17:10 +00:00
|
|
|
let canKeepSocketAlive = true;
|
2022-06-21 12:50:55 +00:00
|
|
|
|
|
|
|
if (socket._httpMessage?.res) {
|
|
|
|
const keepAliveHint = socket._httpMessage.res.headers['keep-alive'];
|
|
|
|
|
|
|
|
if (keepAliveHint) {
|
2024-07-06 08:33:16 +00:00
|
|
|
const hint = /^timeout=(\d+)/.exec(keepAliveHint)?.[1];
|
2022-06-21 12:50:55 +00:00
|
|
|
|
|
|
|
if (hint) {
|
2024-09-09 13:17:10 +00:00
|
|
|
// Let the timer expire before the announced timeout to reduce
|
|
|
|
// the likelihood of ECONNRESET errors
|
|
|
|
let serverHintTimeout = (NumberParseInt(hint) * 1000) - HTTP_AGENT_KEEP_ALIVE_TIMEOUT_BUFFER;
|
|
|
|
serverHintTimeout = serverHintTimeout > 0 ? serverHintTimeout : 0;
|
|
|
|
if (serverHintTimeout === 0) {
|
|
|
|
// Cannot safely reuse the socket because the server timeout is
|
|
|
|
// too short
|
|
|
|
canKeepSocketAlive = false;
|
|
|
|
} else if (serverHintTimeout < agentTimeout) {
|
2022-06-21 12:50:55 +00:00
|
|
|
agentTimeout = serverHintTimeout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 15:42:34 +00:00
|
|
|
if (socket.timeout !== agentTimeout) {
|
|
|
|
socket.setTimeout(agentTimeout);
|
|
|
|
}
|
|
|
|
|
2024-09-09 13:17:10 +00:00
|
|
|
return canKeepSocketAlive;
|
2017-05-12 20:53:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Agent.prototype.reuseSocket = function reuseSocket(socket, req) {
|
|
|
|
debug('have free socket');
|
2020-02-28 11:29:45 +00:00
|
|
|
socket.removeListener('error', freeSocketErrorListener);
|
2019-09-26 03:56:59 +00:00
|
|
|
req.reusedSocket = true;
|
2017-05-12 20:53:15 +00:00
|
|
|
socket.ref();
|
|
|
|
};
|
|
|
|
|
2016-10-12 11:03:13 +00:00
|
|
|
Agent.prototype.destroy = function destroy() {
|
2021-01-29 13:53:16 +00:00
|
|
|
const sets = [this.freeSockets, this.sockets];
|
|
|
|
for (let s = 0; s < sets.length; s++) {
|
|
|
|
const set = sets[s];
|
|
|
|
const keys = ObjectKeys(set);
|
|
|
|
for (let v = 0; v < keys.length; v++) {
|
|
|
|
const setName = set[keys[v]];
|
|
|
|
for (let n = 0; n < setName.length; n++) {
|
|
|
|
setName[n].destroy();
|
2014-08-14 03:15:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-15 22:10:53 +00:00
|
|
|
};
|
|
|
|
|
2018-06-08 04:42:27 +00: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);
|
|
|
|
}
|
|
|
|
|
2020-07-20 13:05:33 +00:00
|
|
|
function asyncResetHandle(socket) {
|
|
|
|
// Guard against an uninitialized or user supplied Socket.
|
|
|
|
const handle = socket._handle;
|
|
|
|
if (handle && typeof handle.asyncReset === 'function') {
|
|
|
|
// Assign the handle a new asyncId and run any destroy()/init() hooks.
|
2021-11-13 07:21:42 +00:00
|
|
|
handle.asyncReset(new ReusedHandle(handle.getProviderType(), handle));
|
2020-07-20 13:05:33 +00:00
|
|
|
socket[async_id_symbol] = handle.getAsyncId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 00:09:32 +00:00
|
|
|
module.exports = {
|
|
|
|
Agent,
|
2023-02-26 10:34:02 +00:00
|
|
|
globalAgent: new Agent({ keepAlive: true, scheduling: 'lifo', timeout: 5000 }),
|
2017-02-28 00:09:32 +00:00
|
|
|
};
|