2

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: enter image description here

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: enter image description here

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: enter image description here

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?

Community
  • 1
  • 1

1 Answers1

0

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.

Jonathan Ong
  • 19,927
  • 17
  • 79
  • 118
  • I know that nodejs pools only 5 connections, but if I've listened the "data" event, and set the response header "connection" values "keep-alive", the server can received all 10 request immediately. Is that means the connection is kept or not only dependents on the "data" event is listened or not, and independent on the "connection" header in the response? You said the http request is never completed without a listener on the "data" event, and this is essentially a leak. can you offer some basis for your opinion? Thank your so much for your reply! – SilentTiger Jan 13 '15 at 01:41