node.jsmessage-queuesleepnode-worker-threads

NodeJS sleep with promise takes too long


I'm trying to improve performance of processing my worker's incoming messages using a queue.

However, the sleep function takes anywhere between 16 to 30 milliseconds to complete instead of the 1 millisecond. Is there a better way to process the queue without this large delay, and without keeping my app at 100% cpu?

I am doing something like this:

var actions = new Queue();
parentPort.on('message', (msg) => actions.enqueue(msg));

loopy();

async function loopy() {
    while (true) {
        if (actions.size() > 0) {
            let action = actions.dequeue();
            //do work
            continue; 
        }

        await sleep(1);
    }
}

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

Any help would be appreciated. Thanks!


Solution

  • while(true) is (usually) not a good idea. You should call the dequeue function after you enqueue a message. The dequeue function should end when 1. there is already a dequeue function running 2. no more message in the queue.

    var isProcessing = false;
    var actions = new Queue();
    parentPort.on('message', (msg) => {
      actions.enqueue(msg)
      tryDequeue();
    });
    
    
    async function tryDequeue() {
        if(isProcessing || actions.size() == 0)
        {
          return;
        }
        isProcessing = true;
    
        let action = actions.dequeue();
        //do work
        
        isProcessing = false;
        tryDequeue();
    }