简体   繁体   English

为什么不听数据事件我只能发送5个请求?

[英]why I can only send 5 request if I don't listen the data event?

I've found some questions that seems has some relationship with this question(eg: Why is node.js only processing six requests at a time? ), but I still can not understand the details. 我发现一些问题似乎与此问题相关(例如: 为什么node.js一次只能处理六个请求? ),但我仍然无法理解细节。

the following is my case. 以下是我的情况。

first, server's code: 首先,服务器的代码:

var express = require ('express'),
    methodOverride = require('method-override');

var app = express();

app.use(methodOverride());
app.use(function(err, req, res, next) {
    console.error(err.stack);
    res.status(500);
    res.send({ error: err });
});

app.use('/', function(req, res, next) {
    res.header('Cache-control', 'no-cache');
    res.header('Access-Control-Allow-Origin', '*');
    next();
});

app.get('/OK/', function (req, res) {
    console.log(getTimestamp() + ' OK ');
    res.status(200).send("200");
});

app.listen(22222);

function getTimestamp(){
    var timestamp = new Date();
    return timestamp.toString() + " " + timestamp.getMilliseconds();
}

console.log(getTimestamp() + ' Sever started!');

and then, the client's code: 然后是客户的代码:

var http = require('http');

var opt = {
    method: "GET",
    host: "localhost",
    port: 22222,
    path: "/OK/",
    headers: {
        'Cache-control': 'no-cache'
    }
};

count = 10;
function request(){
    var req = http.request(opt, function (serverFeedback) {
        var body = "";
        serverFeedback
            .on('data',function(){})
            .on('end', function () {
                console.log(getTimestamp(), "response END", serverFeedback.statusCode, body);
        });
    });
    req.end();

    console.log(getTimestamp(), "resuest START");
    count--;
    if(count > 0){
        setTimeout(request, 500);
    }
}

request();

function getTimestamp(){
    var timestamp = new Date();
    return timestamp.toString() + " " + timestamp.getMilliseconds();
}

run both of them in node, of course run server first, the client will send 10 request in about 5s, and everything is alright. 在节点中运行它们,当然首先运行服务器,客户端将在5秒钟左右发送10个请求,一切都很好。 like this: 像这样: 在此处输入图片说明

But, if I remove the code about listening the "data" event in client, like this: 但是,如果我删除了有关在客户端中监听“数据”事件的代码,如下所示:

var req = http.request(opt, function (serverFeedback) {
    var body = "";
    serverFeedback
        //.on('data',function(){})      /* remove the listener*/
        .on('end', function () {
            console.log(getTimestamp(), "response END", serverFeedback.statusCode, body);
    });
});

run again, the client seems has send 10 request in 5s, but in fact, the server only received 5 request: 再次运行,客户端似乎在5秒钟内发送了10个请求,但实际上,服务器仅收到5个请求: 在此处输入图片说明

and as you see, the "end" event seems doesn't triggered. 如您所见,“结束”事件似乎没有触发。

at last, I add a header in the response via the server's code: 最后,我通过服务器代码在响应中添加标头:

app.use('/', function(req, res, next) {
    res.header('Cache-control', 'no-cache');
    res.header('Access-Control-Allow-Origin', '*');
    res.header('connection', 'close');      /* add a header about connection */
    next();
});

restart the server and run client again: 重新启动服务器并再次运行客户端: 在此处输入图片说明

now the server can received all the 10 request immediately, but the "end" event seems still doesn't triggered. 现在服务器可以立即收到所有10个请求,但是“ end”事件似乎仍然没有触发。

So, it seems that the listener on "data" event has taken some effect on the http connection. 因此,似乎“数据”事件上的侦听器已对http连接产生了一些影响。

can anyone explain the details for everyone? 谁能为每个人解释细节?

two things are happening. 两件事正在发生。

first, when you don't "listen to the data event", the response stream is never completed, so the http request you've made is never completed. 首先,当您不“收听数据事件”时,响应流将永远不会完成,因此您发出的http请求也永远不会完成。 this is essentially a leak. 这本质上是泄漏。 always consume your streams!!! 总是消耗你的流!!! otherwise, your streams will be in a "paused" state indefinitely. 否则,您的流将无限期处于“已暂停”状态。

the second is that node.js by default pools connections using the default agent http://nodejs.org/api/http.html#http_class_http_agent . 第二个是默认情况下,node.js使用默认代理http://nodejs.org/api/http.html#http_class_http_agent来池化连接。 right now, it pools 5 connections, which is why you're seeing a limit of 5 connections. 现在,它汇集了5个连接,这就是为什么您看到5个连接的限制的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据添加到firestore的时候,想用onSnapshot监听,这样就可以自动调用数据了,但是不知道怎么用 - When data is added to the firestore, I want to listen with onSnapshot so that the data can be called automatically, but I don't know how to use 为什么我无法将GET请求发送到google.com? - Why can't I send a GET request to google.com? 为什么我不能使用 Cordova 发送 XHR 请求? - Why can't I send an XHR request with Cordova? 当我没有 jQuery 时,如何发送 Ajax 请求以检查注册用户字段? - How can I send an Ajax request to check a register user field when I don't have jQuery? 我不知道为什么JSON响应无法预言ajax的回叫,而是由浏览器显示并停留在我发送请求的页面上 - I don't know why JSON response can't forword ajax's call back but showing by browser and stay on the page where I send request 如果变量的 classList 为“x”,我该如何编写监听事件,否则在变量具有 classList“x”之前不要监听事件 - How do I write if a variable has a classList of “x” listen for an event, otherwise don't listen for the event until the variable has the classList “x” 为什么我无法从 axios 发送表单数据? - Why can't I send a form data from axios? 为什么我不在 POST 读取数据 - Why i don't read data at POST 我的数组中没有任何数据,只有一个,就是这样,我不明白为什么? - My array isn't taking any data in it, only one and that's it, and I don't understand why? 如果提到最外层的选择器,为什么事件委托不起作用? - Why don't event delegation work if I mention the outermost selector?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM