简体   繁体   中英

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.

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. 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: 在此处输入图片说明

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.

So, it seems that the listener on "data" event has taken some effect on the http connection.

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. 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 . right now, it pools 5 connections, which is why you're seeing a limit of 5 connections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM