Skip to content
Advertisement

Async Concurrent Queue with max concurrency

I’m running across a bug with a custom asynchronous queue that calls 10 async functions at a time.

I’m initiating the queue with 50 jobs, once first 10 jobs are finished the queue moves to the subsequent 10 until it finishes all.

The bug I’m coming across is that once it finishes 50, it restarts with first 5 jobs with 2 or 3 or 1 job at a time. It also takes fewer than 10 jobs towards the end of the queue.

Please create these two files and test with mocha and see the output yourself.

Note: Set the timeout in mocha to 0 to keep the test running for prolonged period of time.

Queue.js

JavaScript

QueueTest.js

JavaScript

Advertisement

Answer

The problem lies in the for loop in Queue.prototype.run.

I can’t immediately see why it should misbehave the way it does but a fix is to replace the for loop with self.jobs.splice() to create the tasks array.

JavaScript

Nothing else needs to change.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement