如何在nodejs服务器中设置HTTP Keep-Alive超时 [英] How to set the HTTP Keep-Alive timeout in a nodejs server

查看:3719
本文介绍了如何在nodejs服务器中设置HTTP Keep-Alive超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上正在对ExpressJS服务器进行一些负载测试,我注意到服务器发送的响应包括一个Connection:Keep-Alive标头。据我所知,连接将保持打开,直到服务器或客户端发送Connection:Close标头。



在某些实现中,连接: Keep-Alive标头提供了一个Keep-Alive标题,设置连接超时和通过此连接发送的最大连续请求数。



例如: Keep-Alive:timeout = 15,max = 100



在Express服务器上设置这些参数有什么办法(和它是否相关)?



如果没有,你知道ExpressJS如何处理这个吗?



编辑:
经过一番调查,我发现默认超时是在节点标准http库

  socket.setTimeout(2 * 60 * 1000); // 2分钟超时

为了更改:

  var http = require('http'); 

http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text / plain'});
res。 end(Hello World);
})on('connection',function(socket){
socket.setTimeout(10000);
})listen(3000);

无论如何,我仍然看起来有点奇怪,服务器没有发送任何提示客户关于其超时。



Edit2:
感谢josh3736发表评论。



setSocketKeepAlive与HTTP保持活动无关。它是一个TCP级别选项,允许您检测到连接的另一端已经消失。

解决方案

对于Express 3 :

  var express = require('express'); 
var app = express();
var server = app.listen(5001);

server.listen(3000);

server.on('connection',function(socket){
console.log(一个新的连接是由客户端创建的);
socket.setTimeout 30 * 1000);
// 30秒超时,根据您的需要更改此项
})


I'm actually doing some load testing against an ExpressJS server, and I noticed that the response send by the server includes a "Connection: Keep-Alive" header. As far as I understand it, the connection will remain opened until the server or the client sends a "Connection: Close" header.

In some implementations, the "Connection: Keep-Alive" header comes up with a "Keep-Alive" header setting the connection timeout and the maximum number of consecutive requests send via this connection.

For example : "Keep-Alive: timeout=15, max=100"

Is there a way (and is it relevant) to set these parameters on an Express server ?

If not, do you know how ExpressJS handles this ?

Edit: After some investigations, I found out that the default timeout is set in the node standard http library:

socket.setTimeout(2 * 60 * 1000); // 2 minute timeout

In order to change this:

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end("Hello World");
}).on('connection', function(socket) {
  socket.setTimeout(10000);
}).listen(3000);

Anyway it still looks a little bit weird to me that the server doesn't send any hint to the client concerning its timeout.

Edit2: Thanks to josh3736 for his comment.

setSocketKeepAlive is not related to HTTP keep-alive. It is a TCP-level option that allows you to detect that the other end of the connection has disappeared.

解决方案

For Express 3:

var express = require('express');
var app = express();
var server = app.listen(5001);

server.listen(3000);

server.on('connection', function(socket) {
  console.log("A new connection was made by a client.");
  socket.setTimeout(30 * 1000); 
  // 30 second timeout. Change this as you see fit.
})

这篇关于如何在nodejs服务器中设置HTTP Keep-Alive超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆