How to check the number of open connections in node.js?
Asked Answered
K

3

12

I have a machine running node.js (v0.1.32) with a tcp server (tcp.createServer) and a http server (http.createServer). The http server is hit by long polling requests (lasting 50 sec each) from a comet based application on port 80. And there are tcp socket connections on port 8080 from an iphone application for the same purpose.

It was found that the server was not able to handle more connections (especially the tcp connections while the http connections appeared fine!!??) for a while and was normal only after a restart.

For load testing the connections I have created a tcp server and spawned 2000 requests and figured that the connections starting to fail after the max file descriptor limit on machine is reached (default 1024). Which is a really very small number.

So, a novice question here: How do I scale my application to handle more number of connections on node.js and how I handle this issue.

Is there a way to find out how many active connections are there at the moment?

Thanks Sharief

Kursk answered 9/7, 2010 at 16:5 Comment(0)
K
4

Hey Joey! I was looking for a unix solution that would help me figure out how many open connections at a given moment anytime on my machine. The reason was my server was not able to handle requests after a certain number of connections. And figured that my machine can handle only 1024 open connections at a time i.e., the ulimit file descriptor value which defaults to 1024. I have modified this value by setting ulimit -n that suits my requirement.

So to check the open connections I used lsof that gives me the list of open files and figured how many connections are open via each port I was using.

Kursk answered 15/7, 2010 at 6:11 Comment(0)
I
4

You can get the count of connections by using below:

var server = http.createServer(app);     
server.getConnections(function(error, count) {
    console.log(count); 
});

Using this you keep check on connection and when it cross a threshold then close the previous connections. Hope it helps.

Indigent answered 6/1, 2020 at 4:46 Comment(0)
A
2

I don't know if there's a built-in way to get the number of active connections with Node, but it's pretty easy to rig something up.

For my comet-style Node app I keep an object that I add connections to as a property. Every X seconds I iterate over that object and see if there are any connections that should be closed (in your case, anything past your 50 second limit).

When you close a connection, just delete that property from your connections object. Then you can see how many connections are open at any time with Object.size(connections)

Amboceptor answered 14/7, 2010 at 14:24 Comment(1)
for this solution i'm imagining the client has to be sending a heartbeat of some kind to update whether or not they are still active?Extravaganza

© 2022 - 2024 — McMap. All rights reserved.