Fix the nextTick(cb) causing event loop starvation

If you run more than 1000 lookups (like processing a big directory of
files), you will get lots of warnings from node.  Solution is to use
setImmediate(cb) instead.
pull/2/head
Tom V 11 years ago
parent 7b5171f6bd
commit e7f8209c90
  1. 6
      lib/executor.js
  2. 19
      test/executor.test.js

@ -22,7 +22,11 @@ function Executor () {
// Always tell the queue task is complete. Execute callback if any was given. // Always tell the queue task is complete. Execute callback if any was given.
if (typeof lastArg === 'function') { if (typeof lastArg === 'function') {
callback = function () { callback = function () {
process.nextTick(cb); if (typeof setImmediate === 'function') {
setImmediate(cb);
} else {
process.nextTick(cb);
}
lastArg.apply(null, arguments); lastArg.apply(null, arguments);
}; };

@ -76,6 +76,21 @@ function testRightOrder (d, done) {
// Note: The following test does not have any assertion because it
// is meant to address the deprecation warning:
// (node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.
// see
var testEventLoopStarvation = function(d, done){
var times = 1001;
var i = 0;
while ( i <times) {
i++;
d.find({"bogus": "search"}, function (err, docs) {
});
}
done();
};
describe('Executor', function () { describe('Executor', function () {
describe('With persistent database', function () { describe('With persistent database', function () {
@ -114,6 +129,10 @@ describe('Executor', function () {
testRightOrder(d, done); testRightOrder(d, done);
}); });
it('Does not starve event loop and raise warning when more than 1000 callbacks are in queue', function(done){
testEventLoopStarvation(d, done);
});
}); // ==== End of 'With persistent database' ==== }); // ==== End of 'With persistent database' ====

Loading…
Cancel
Save