Nodejs:http服务端keep-alive超时设置

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);
});

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值