How to count concurrent requests to my server?

1,265 views
Skip to first unread message

Domenic Denicola

unread,
Jul 10, 2012, 5:25:15 PM7/10/12
to nod...@googlegroups.com
I am trying to create a mini dashboard for my server that shows number of "unfinished requests." (BTW if there's a better term for that let me know.)

Here is what I have, but I don't think it's catching all the response finishes:

server.server.on("request", function (request, response) {
    updateRequestCount(+1);

    response.on("finish", function () {
        updateRequestCount(-1);
    });
});

If I log the request count to the console, it always increases, sometimes going down by one, but usually going up much more than it goes down.

What event can I listen to to properly count requests?

Roly Fentanes

unread,
Jul 10, 2012, 5:40:15 PM7/10/12
to nod...@googlegroups.com
Response is a stream, should emit the `end` event. Are you sure it emits `finish`? And if there is an error it might emit `error` or `close` instead of `end`.

mscdex

unread,
Jul 10, 2012, 6:48:00 PM7/10/12
to nodejs
On Jul 10, 5:25 pm, Domenic Denicola <dome...@domenicdenicola.com>
wrote:
> I am trying to create a mini dashboard for my server that shows number of
> "unfinished requests." (BTW if there's a better term for that let me know.)

I would listen for response's 'close' event. As Roly mentioned, this
should fire for connections that are either closed naturally or closed
due to some error.

Domenic Denicola

unread,
Jul 11, 2012, 10:15:34 AM7/11/12
to nod...@googlegroups.com
Neither "end" nor "close" are ever emitted.

* HttpServerResponse is a WritableStream which does not emit "end" events.
* WritableStreams emit "close" events "when the underlying file descriptor has been closed," which doesn't seem applicable.
* HttpServerResponses emit "close" events to indicate "that the underlaying connection was terminated before response.end() was called or able to flush," which is not the case.

That's as far as documentation goes.

Arnout Kazemier

unread,
Jul 11, 2012, 10:19:59 AM7/11/12
to nod...@googlegroups.com
Every server has an internal connections count.. See http://nodejs.org/api/net.html#net_server_connections
--
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group at

Domenic Denicola

unread,
Jul 11, 2012, 10:54:03 AM7/11/12
to nod...@googlegroups.com
Thank you! This is perfect. Still not sure exactly which events would be most accurate (maybe "connection" and the resulting socket's "close"?), but with this in hand I can just check the number of connections in a setInterval, which is good enough.

mscdex

unread,
Jul 11, 2012, 12:06:41 PM7/11/12
to nodejs
On Jul 11, 10:15 am, Domenic Denicola <dome...@domenicdenicola.com>
wrote:
> Neither "end" nor "close" are ever emitted.

Ok, then `request.connection.on('close', ....);`

Ted Young

unread,
Jul 11, 2012, 12:36:38 PM7/11/12
to nod...@googlegroups.com
just to double check, are you actually calling response.end() in the example you are running?

Ted

Reply all
Reply to author
Forward
0 new messages