简体   繁体   中英

node.js http server: how to get pending socket connections?

on a basic node http server like this:

var http = require('http');

var server = http.createServer(function(req, res){

  console.log("number of concurr. connections: " + this.getConnections());
  //var pendingConnections = ???

});

server.maxConnections = 500;

server.listen(4000);

if i send 500 requests at one time to the server, the number of concurr. connections is arround 350. with the hard limit set to 500 ( net.server.backlog too), i want to know, how to access the number of pending connections (max. 150 in this example) when ever a new request starts.

so i think i have to access the underlying socket listening on port 4000 to get this info, but until now i was not able to get it.

EDIT

looking at node-http there is an event called connection , so i think the roundtrip of an request is as follows:

  1. client connects to server socket --> 3-way-handshake, socket lasts in state CONNECTED (or ESTABLISHED ?!) then in node event connection is emitted.
  2. the node http server accepts this pending connection an starts processing the request by emitting request

so the number of connections has to be at least as big as the number of requests, but with following example i could not confirm this:

var http = require('http');

var activeRequets = 0;
var activeConnections = 0;

var server = http.createServer(function(req, res){

    activeRequests++;
    res.send("foo");

});

server.on('connection', function (socket) {

    socket.setKeepAlive(false);
    activeConnections++;

});

setInterval(function(){

    console.log("activeConns: " + activeConnections + " activeRequests: " + activeRequests);
    activeRequests = 0;
    activeConnections = 0;

}, 500);

server.maxConnections = 1024;
server.listen(4000, '127.0.0.1');

even if i stress the server with 1000 concurr connections and adding one delay in the response, activeRequests is mostly as high as activeConnections . even worse, the activeRequests are often higher then activeconnections, how could this be?

IIRC You can just count how many connections that have a status of SYN_RECV for that particular IP and port that you're listening on. Whether you use a child process to execute netstat and grep (or similar utilities) for that information, or write a binding to get this information using the *nix C API, is up to you.

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