Sync gateway connection Error: connect EMFILE - Local (undefined:undefined)

Hi all:

I am trying to get document revision identifier via sync gatetway API GET /{db}/{doc} within Node server:
Here is the code block:

function _getRev(docIdUrl, gateway, callback) {
   
    let options = {
        host: gateway.host,
        path: gateway.path + docIdUrl,
        port: gateway.port,
        method: 'GET',
        headers: {
            'Content-Type': 'application/json'
        }
    };

    // Node HTTP requests
    let syncGatewayRequest = http.request(options, (response) => {
           // ...unrelevant codes
    });

    syncGatewayRequest.on('error', (error) => {
        logger.error('syncGateway connection error for ' + docIdUrl);
        callback(error, null); // here is the error happening!!!!!
    });

    syncGatewayRequest.write(JSON.stringify({}));
    syncGatewayRequest.end();
}

Then I got error:

[2017-11-03 11:07:51.961] { [Error: connect EMFILE 10.0.1.53:4985 - Local (undefined:undefined)]
  code: 'EMFILE',
  errno: 'EMFILE',
  syscall: 'connect',
  address: '10.0.1.53',
  port: 4985 }
Error: connect EMFILE 10.0.1.53:4985 - Local (undefined:undefined)
    at Object.exports._errnoException (util.js:870:11)
    at exports._exceptionWithHostPort (util.js:893:20)
    at connect (net.js:849:16)
    at net.js:937:9
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickCallback (node.js:349:13)

There is a context that the above function are executed asynchronously by a significant number of services, say 10,000+

I noticed the post here node.js - Nodejs connect EMFILE - How to reuse connections? - Stack Overflow

But I tried to unlimit the default connections by doing:

var http = require(‘http’)
http.globalAgent.maxSockets = Infinity

But does not seem to work, error still …

Anyone can let me know what’s wrong here?

This is a Node.js issue, not a Couchbase issue, so you might have more luck asking on a Node forum.

Just from reading that Stack Overflow thread, I think that setting maxSockets to infinity is the opposite of what you should do. You want to limit the maximum number of sockets, because your process is running out of file descriptors or TCP ports. (And likewise, you’re using up the same number of FDs/ports on the Sync Gateway server.) Try setting it to 8 or so.