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.
|
|
|
|
|
2015-05-19 11:00:06 +00:00
|
|
|
'use strict';
|
2016-01-04 02:53:27 +00:00
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const http = require('http');
|
|
|
|
const Agent = require('_http_agent').Agent;
|
2013-11-06 11:58:03 +00:00
|
|
|
|
2016-05-29 07:06:56 +00:00
|
|
|
let name;
|
|
|
|
|
2016-01-04 02:53:27 +00:00
|
|
|
const agent = new Agent({
|
2013-11-06 11:58:03 +00:00
|
|
|
keepAlive: true,
|
|
|
|
keepAliveMsecs: 1000,
|
|
|
|
maxSockets: 5,
|
2014-02-25 22:18:43 +00:00
|
|
|
maxFreeSockets: 5
|
2013-11-06 11:58:03 +00:00
|
|
|
});
|
|
|
|
|
2017-07-14 22:05:24 +00:00
|
|
|
const server = http.createServer(common.mustCall((req, res) => {
|
2013-11-06 11:58:03 +00:00
|
|
|
if (req.url === '/error') {
|
|
|
|
res.destroy();
|
|
|
|
return;
|
|
|
|
} else if (req.url === '/remote_close') {
|
2019-01-21 00:22:27 +00:00
|
|
|
// Cache the socket, close it after a short delay
|
2016-01-04 02:53:27 +00:00
|
|
|
const socket = res.connection;
|
2017-07-14 22:05:24 +00:00
|
|
|
setImmediate(common.mustCall(() => socket.end()));
|
2013-11-06 11:58:03 +00:00
|
|
|
}
|
|
|
|
res.end('hello world');
|
2017-07-14 22:05:24 +00:00
|
|
|
}, 4));
|
2013-11-06 11:58:03 +00:00
|
|
|
|
|
|
|
function get(path, callback) {
|
2014-02-25 22:18:43 +00:00
|
|
|
return http.get({
|
2013-11-06 11:58:03 +00:00
|
|
|
host: 'localhost',
|
2016-05-29 07:06:56 +00:00
|
|
|
port: server.address().port,
|
2014-02-25 22:18:43 +00:00
|
|
|
agent: agent,
|
2013-11-06 11:58:03 +00:00
|
|
|
path: path
|
2019-08-21 13:12:48 +00:00
|
|
|
}, callback).on('socket', common.mustCall(checkListeners));
|
2013-11-06 11:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkDataAndSockets(body) {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(body.toString(), 'hello world');
|
|
|
|
assert.strictEqual(agent.sockets[name].length, 1);
|
|
|
|
assert.strictEqual(agent.freeSockets[name], undefined);
|
2021-10-24 11:34:45 +00:00
|
|
|
assert.strictEqual(agent.totalSocketCount, 1);
|
2013-11-06 11:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function second() {
|
2019-03-07 00:03:53 +00:00
|
|
|
// Request second, use the same socket
|
2019-09-26 03:56:59 +00:00
|
|
|
const req = get('/second', common.mustCall((res) => {
|
|
|
|
assert.strictEqual(req.reusedSocket, true);
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(res.statusCode, 200);
|
2013-11-06 11:58:03 +00:00
|
|
|
res.on('data', checkDataAndSockets);
|
2017-07-14 22:05:24 +00:00
|
|
|
res.on('end', common.mustCall(() => {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(agent.sockets[name].length, 1);
|
|
|
|
assert.strictEqual(agent.freeSockets[name], undefined);
|
2017-07-14 22:05:24 +00:00
|
|
|
process.nextTick(common.mustCall(() => {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(agent.sockets[name], undefined);
|
|
|
|
assert.strictEqual(agent.freeSockets[name].length, 1);
|
2021-10-24 11:34:45 +00:00
|
|
|
assert.strictEqual(agent.totalSocketCount, 1);
|
2013-11-06 11:58:03 +00:00
|
|
|
remoteClose();
|
2017-07-14 22:05:24 +00:00
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}));
|
2013-11-06 11:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function remoteClose() {
|
2019-03-07 00:03:53 +00:00
|
|
|
// Mock remote server close the socket
|
2019-09-26 03:56:59 +00:00
|
|
|
const req = get('/remote_close', common.mustCall((res) => {
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(req.reusedSocket, true);
|
|
|
|
assert.strictEqual(res.statusCode, 200);
|
2013-11-06 11:58:03 +00:00
|
|
|
res.on('data', checkDataAndSockets);
|
2017-07-14 22:05:24 +00:00
|
|
|
res.on('end', common.mustCall(() => {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(agent.sockets[name].length, 1);
|
|
|
|
assert.strictEqual(agent.freeSockets[name], undefined);
|
2017-07-14 22:05:24 +00:00
|
|
|
process.nextTick(common.mustCall(() => {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(agent.sockets[name], undefined);
|
|
|
|
assert.strictEqual(agent.freeSockets[name].length, 1);
|
2021-10-24 11:34:45 +00:00
|
|
|
assert.strictEqual(agent.totalSocketCount, 1);
|
2019-03-07 00:03:53 +00:00
|
|
|
// Waiting remote server close the socket
|
2017-07-14 22:05:24 +00:00
|
|
|
setTimeout(common.mustCall(() => {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(agent.sockets[name], undefined);
|
2018-04-26 21:09:56 +00:00
|
|
|
assert.strictEqual(agent.freeSockets[name], undefined);
|
2021-10-24 11:34:45 +00:00
|
|
|
assert.strictEqual(agent.totalSocketCount, 0);
|
2013-11-06 11:58:03 +00:00
|
|
|
remoteError();
|
2017-07-14 22:05:24 +00:00
|
|
|
}), common.platformTimeout(200));
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}));
|
2013-11-06 11:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function remoteError() {
|
2019-03-07 00:03:53 +00:00
|
|
|
// Remote server will destroy the socket
|
2017-07-14 22:05:24 +00:00
|
|
|
const req = get('/error', common.mustNotCall());
|
|
|
|
req.on('error', common.mustCall((err) => {
|
|
|
|
assert(err);
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(err.message, 'socket hang up');
|
2021-03-29 09:20:57 +00:00
|
|
|
assert.strictEqual(agent.sockets[name].length, 1);
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(agent.freeSockets[name], undefined);
|
2021-10-24 11:34:45 +00:00
|
|
|
assert.strictEqual(agent.totalSocketCount, 1);
|
2021-03-29 09:20:57 +00:00
|
|
|
// Wait socket 'close' event emit
|
|
|
|
setTimeout(common.mustCall(() => {
|
|
|
|
assert.strictEqual(agent.sockets[name], undefined);
|
|
|
|
assert.strictEqual(agent.freeSockets[name], undefined);
|
2021-10-24 11:34:45 +00:00
|
|
|
assert.strictEqual(agent.totalSocketCount, 0);
|
2021-03-29 09:20:57 +00:00
|
|
|
server.close();
|
|
|
|
}), common.platformTimeout(1));
|
2017-07-14 22:05:24 +00:00
|
|
|
}));
|
2013-11-06 11:58:03 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 22:05:24 +00:00
|
|
|
server.listen(0, common.mustCall(() => {
|
2016-05-29 07:06:56 +00:00
|
|
|
name = `localhost:${server.address().port}:`;
|
2019-03-22 02:44:26 +00:00
|
|
|
// Request first, and keep alive
|
2019-09-26 03:56:59 +00:00
|
|
|
const req = get('/first', common.mustCall((res) => {
|
|
|
|
assert.strictEqual(req.reusedSocket, false);
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(res.statusCode, 200);
|
2013-11-06 11:58:03 +00:00
|
|
|
res.on('data', checkDataAndSockets);
|
2017-07-14 22:05:24 +00:00
|
|
|
res.on('end', common.mustCall(() => {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(agent.sockets[name].length, 1);
|
|
|
|
assert.strictEqual(agent.freeSockets[name], undefined);
|
2017-07-14 22:05:24 +00:00
|
|
|
process.nextTick(common.mustCall(() => {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(agent.sockets[name], undefined);
|
|
|
|
assert.strictEqual(agent.freeSockets[name].length, 1);
|
2021-10-24 11:34:45 +00:00
|
|
|
assert.strictEqual(agent.totalSocketCount, 1);
|
2013-11-06 11:58:03 +00:00
|
|
|
second();
|
2017-07-14 22:05:24 +00:00
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}));
|
2019-08-21 13:12:48 +00:00
|
|
|
|
|
|
|
// Check for listener leaks when reusing sockets.
|
|
|
|
function checkListeners(socket) {
|
2019-08-21 20:03:11 +00:00
|
|
|
const callback = common.mustCall(() => {
|
|
|
|
if (!socket.destroyed) {
|
|
|
|
assert.strictEqual(socket.listenerCount('data'), 0);
|
|
|
|
assert.strictEqual(socket.listenerCount('drain'), 0);
|
|
|
|
// Sockets have freeSocketErrorListener.
|
|
|
|
assert.strictEqual(socket.listenerCount('error'), 1);
|
|
|
|
// Sockets have onReadableStreamEnd.
|
|
|
|
assert.strictEqual(socket.listenerCount('end'), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.off('free', callback);
|
|
|
|
socket.off('close', callback);
|
|
|
|
});
|
2019-08-21 13:12:48 +00:00
|
|
|
assert.strictEqual(socket.listenerCount('error'), 1);
|
|
|
|
assert.strictEqual(socket.listenerCount('end'), 2);
|
2019-08-21 20:03:11 +00:00
|
|
|
socket.once('free', callback);
|
|
|
|
socket.once('close', callback);
|
2019-08-21 13:12:48 +00:00
|
|
|
}
|