Closed
Description
Why the http client does not support the keep alive feature?
TCP socket should be reused in multiple requests.
But the result is that the request and response are sent only once per connection.
- Version:
nodejs v14.2.0
- Platform:
Linux localhost 4.9.148 #1 SMP PREEMPT Tue Mar 10 02:27:59 CST 2020 aarch64 Android
- Subsystem:
What steps will reproduce the bug?
client.js
const http = require('http');
const agent = new http.Agent({
keepAlive: true,
keepAliveMsecs: 10000
});
function request() {
count++;
const req = http.request({
host: 'localhost',
port: 9000,
method: 'GET',
agent: agent
}, res => {
const socket = res.socket;
console.log(socket.localAddress, socket.localPort);
console.log(res.headers);
});
req.end();
if (count > 20) {
process.exit();
}
}
let count = 0;
setInterval(() => {
request();
request();
}, 500 * (1 + Math.random()));
setInterval(() => {
request();
}, 500 * (1 + Math.random()));
setInterval(() => {
request();
}, 500 * (1 + Math.random()));
server.js
const http = require('http');
http.createServer((req, res) => {
const socket = req.socket;
const info = socket.remoteAddress + ',' + req.socket.remotePort;
console.log(info);
console.log(req.headers);
res.write(JSON.stringify(req.headers));
res.end(info);
}).listen(9000, () => {
console.log('listening');
});
node server.js
node client.js
How often does it reproduce? Is there a required condition?
100%
What is the expected behavior?
TCP socket should be reused in multiple requests.
What do you see instead?
$ node /storage/emulated/0/test/server.js
listening
::ffff:127.0.0.1,44284
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44286
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44288
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44290
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44294
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44296
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44300
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44302
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44304
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44308
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44310
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44312
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44314
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44318
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44320
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44322
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44324
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44328
{ host: 'localhost:9000', connection: 'keep-alive' }
::ffff:127.0.0.1,44330
{ host: 'localhost:9000', connection: 'keep-alive' }
^C
$
$ node /storage/emulated/0/test/client.js
127.0.0.1 44284
{
date: 'Fri, 12 Jun 2020 04:22:52 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44286
{
date: 'Fri, 12 Jun 2020 04:22:52 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44288
{
date: 'Fri, 12 Jun 2020 04:22:52 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44290
{
date: 'Fri, 12 Jun 2020 04:22:52 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44294
{
date: 'Fri, 12 Jun 2020 04:22:52 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44296
{
date: 'Fri, 12 Jun 2020 04:22:53 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44300
{
date: 'Fri, 12 Jun 2020 04:22:53 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44302
{
date: 'Fri, 12 Jun 2020 04:22:53 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44304
{
date: 'Fri, 12 Jun 2020 04:22:53 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44308
{
date: 'Fri, 12 Jun 2020 04:22:53 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44310
{
date: 'Fri, 12 Jun 2020 04:22:53 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44312
{
date: 'Fri, 12 Jun 2020 04:22:54 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44314
{
date: 'Fri, 12 Jun 2020 04:22:54 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44318
{
date: 'Fri, 12 Jun 2020 04:22:54 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44320
{
date: 'Fri, 12 Jun 2020 04:22:54 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44322
{
date: 'Fri, 12 Jun 2020 04:22:54 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44324
{
date: 'Fri, 12 Jun 2020 04:22:54 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44328
{
date: 'Fri, 12 Jun 2020 04:22:54 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
127.0.0.1 44330
{
date: 'Fri, 12 Jun 2020 04:22:55 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
}
$