How do I know I've hit the threads limit defined in Node?

Viewed 1566

I have limited the size of the thread pool to 25.

process.env.UV_THREADPOOL_SIZE = 25;

How can one know that all the threads are exhausted at run time?

Is there any way to find that all the define threads are exhausted during a new request?

I'm using Native Abstractions for Node.js (NAN) to call C++ functions. For every request to C++ Nan::AsyncQueueWorker is created. Here I want to find if the thread limit is exhausted and then add a safety factor.

3 Answers

Are you looking implementation in nan or js?

In Nan impl:

You have to do it manually. Maintain a map where key is int and value as workAsyn. Push at every call and delete when workAsyn complete. Do this for every request.

Compare the size of map with your thread limit defined.

I analyzed nan and libuv sources. Unfortunately, now there's no way to get the number of used threads. If only to add this feature yourself.

It looks like this cluster module might be able to help...

 var cluster = require('../')
    ,http = require('http');

 var server = http.createServer(function(req, res){
   res.writeHead(200);
   res.end('Hello World');
 });

 var workerCount = 0;

 cluster(server)
   .listen(3000)
   .on('worker',()=> {
     workerCount++;
     console.log('workerCount',workerCount)
   })
   .on('worker killed',()=> { 
     workerCount--; 
     console.log('workerCount',workerCount)
   })

Also appears to be able to access the worker count directly from master with the REPL "telnet" plugin...

http://learnboost.github.io/cluster/docs/stats.html

Docs

Related