Nodejs里用于控制http服务端的KeepAlive超时的函数setTimeout:
server.setTimeout([msecs][, callback])
示例代码server.js:
const http = require('http')
const port = 8889
const host = "127.0.0.1"
const print = function(msg){
t = new Date();
console.log(t + ' ' + msg);
};
const server = http.createServer(function(req, res) {
print("connection:" + req.headers["connection"])
var data = []
var len = 0
req.on('data', function(chunk) {
data.push(chunk)
len += chunk.length
})
req.on('end', function() {
print("body-length:" + len + "body:" + data.toString())
})
res.end("OK")
});
server.setTimeout(3 * 1000);
server.listen(port, host, function() {
print("listening on " + port)
});
server.on('error', function(e) {
print(e);
});
server.on('connection', function(socket) {
print('==> a new socket with a remote port:' + socket.remotePort + "<==");
});
用于验证的客户端代码client.js
var http = require('http')
const agent = new http.Agent({
keepAlive: true,
maxSockets: 3
});
var options = {
host: "127.0.0.1",
port: "8889",
path: '/',
method: 'POST',
agent: agent
};
post = function(options, content, callback) {
req = http.request(options, function(res) {
var data = '';
res.on('data', function(chunk) {
return data += chunk;
});
return res.on('end', function() {
return callback(null, data);
});
}).on('error', function(e) {
return callback(e, null);
});
req.write(content);
req.end();
};
var jsonData = {
"name": "sonoo",
"salary": 5600,
"married": true
};
var content = JSON.stringify(jsonData);
http.createServer(function(req, res) {
post(options, content, function(err, data) {
res.end(data);
})
}).listen(8888);
连续多次请求curl http://127.0.0.1:8888,可以得到如下输出,当请求间隔大于3秒时会重建新的socket,否则继续使用原有socket:
[root@dev keepalive]# node server.js
Tue Sep 15 2020 17:35:18 GMT+0800 (CST) listening on 8889
Tue Sep 15 2020 17:35:24 GMT+0800 (CST) ==> a new socket with a remote port:41040<==
Tue Sep 15 2020 17:35:24 GMT+0800 (CST) connection:keep-alive
Tue Sep 15 2020 17:35:24 GMT+0800 (CST) body-length:45body:{"name":"sonoo","salary":5600,"married":true}
Tue Sep 15 2020 17:35:25 GMT+0800 (CST) connection:keep-alive
Tue Sep 15 2020 17:35:25 GMT+0800 (CST) body-length:45body:{"name":"sonoo","salary":5600,"married":true}
Tue Sep 15 2020 17:35:26 GMT+0800 (CST) connection:keep-alive
Tue Sep 15 2020 17:35:26 GMT+0800 (CST) body-length:45body:{"name":"sonoo","salary":5600,"married":true}
Tue Sep 15 2020 17:35:32 GMT+0800 (CST) ==> a new socket with a remote port:42070<==
Tue Sep 15 2020 17:35:32 GMT+0800 (CST) connection:keep-alive
Tue Sep 15 2020 17:35:32 GMT+0800 (CST) body-length:45body:{"name":"sonoo","salary":5600,"married":true}
Tue Sep 15 2020 17:35:38 GMT+0800 (CST) ==> a new socket with a remote port:42940<==
Tue Sep 15 2020 17:35:38 GMT+0800 (CST) connection:keep-alive
Tue Sep 15 2020 17:35:38 GMT+0800 (CST) body-length:45body:{"name":"sonoo","salary":5600,"married":true}
如果把client.js里的options.agent注释掉即不适用keep-alive则可得到如下输出,每次请求都会重建socket:
Tue Sep 15 2020 17:40:15 GMT+0800 (CST) connection:close
Tue Sep 15 2020 17:40:15 GMT+0800 (CST) body-length:45body:{"name":"sonoo","salary":5600,"married":true}
Tue Sep 15 2020 17:40:16 GMT+0800 (CST) ==> a new socket with a remote port:53514<==
Tue Sep 15 2020 17:40:16 GMT+0800 (CST) connection:close
Tue Sep 15 2020 17:40:16 GMT+0800 (CST) body-length:45body:{"name":"sonoo","salary":5600,"married":true}
Tue Sep 15 2020 17:40:16 GMT+0800 (CST) ==> a new socket with a remote port:53580<==
Tue Sep 15 2020 17:40:16 GMT+0800 (CST) connection:close
Tue Sep 15 2020 17:40:16 GMT+0800 (CST) body-length:45body:{"name":"sonoo","salary":5600,"married":true}
Tue Sep 15 2020 17:40:18 GMT+0800 (CST) ==> a new socket with a remote port:53808<==
Tue Sep 15 2020 17:40:18 GMT+0800 (CST) connection:close
Tue Sep 15 2020 17:40:18 GMT+0800 (CST) body-length:45body:{"name":"sonoo","salary":5600,"married":true}
如果使用express框架的话,则服务端keepAliveTimeout的设置如下:
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
app.use(
bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json())
app.post('/', function(req, res) {
console.log(req.body);
res.send('OK')
});
const server = app.listen(8889);
server.keepAliveTimeout = 3 * 1000;
server.on('connection', function(socket) {
console.log("A new connection remote port:" + socket.remotePort);
});