Giter Club home page Giter Club logo

zeroq's Introduction

Fast queue - ZeroQ

Greenkeeper badge

NPM Version Build Status Test Coverage

High performace in-memory tasks queue for Node.js. Perfect for executing in batches while utilizing resources to the maximum.

High perormance

ZeroQ was built to endure great tasks load, while preserving the order of the tasks. It surpassing other implementations by:

  • It's in-memory data structure is faster - Linked list instead of Array (I measured 200X latency with [].shift comparing to changing a single pointer of linked list)
  • Super-high test coverage
  • Out of the box support for TypeScript.
  • No dependencies.

I currently don't provide other important queue features such as priority queue as I found current popular implementation fast enough with the features these provide.

Speed was our greatest concern and not a full featured queue.

Simple to use

Using Javascript

  //Require all ZeroQ module
  const ZeroQ = require('zeroq');
  //Now you can use both `ZeroQ.DataQueue` and `ZeroQ.TasksQueue`

  //Or Directly load the desired class
  const {DataQueue ,TasksQueue} = require('zeroq');

Using TypeScript

  import {DataQueue , TasksQueue} from 'zeroq';
  //Now you can use both `ZeroQ.DataQueue` and `ZeroQ.TasksQueue`

API

Both Queues supports maxConcurrency number as first argument. This enables locking/releasing multiple resources with a single queue.

Tasks queue - TasksQueue(maxConcurrency : number , isSyncMode : boolean )

This class was created to distribute tasks in the order these were pushed. Tasks will be excuted once locked resource will be released.

const {TasksQueue} = require('zeroq');
//Create a sync queue with no task executing in parallel
const queue = new TasksQueue(1 , true);
//Push some task to queue
queue.push(()=>{
    console.log('task 1');
});
//=> task 1
queue.push(()=>{
    console.log('task 2');
});
queue.release();
//=> task 2

Data queue - DataQueue(maxConcurrency : number , onData : (data:T)=>void )

This class was created to distribute data in the order these were pushed. Data will be pushed once locked resource will be released.

const {DataQueue} = require('zeroq');
//Create a sync queue with no task executing in parallel
const queue = new DataQueue(1 , function onData(data){
    console.log('just recived ' , data);
});
//Any data can be pushed to the queue it will be release into your callback as is
queue.push({ eventName : 'click'});
//=>just recived { eventName : 'click'}
queue.push("https://www.example.com");
queue.push(200);

setTimeout(()=>{
    queue.release();
//=>just recived https://www.example.com
    queue.release();
//=>just recived 200
} , 2000);

##Examples

Making multiple parallel request

Usually when making requests with Noed.JS the best approch is to request I/O operation to execute in parallel and wait for the last one to return.

  const rp = require('request-promise');
  Promise.all([
      rp('https://www.example.com/resource/1'),
      rp('https://www.example.com/resource/2'),
      rp('https://www.example.com/resource/3')
  ])
  .then(function (results) {
      //All requests have finished
  })
  .catch(function (err) {
      //One of the requests have failed
  });

While it is in most cases the best approch when the amount of requests required is much higher, requesting from the OS to preform so many requests at once will probably result in random recurring timeouts.

The best approach is to lock a big number of requests. As long as the number of requests is lower than the maximum concurrency - all requests will execute together. Else it will be executed in bulks.

  const rp = require('request-promise');
  const requestQueue = new TasksQueue(30);

  const thousendsOfResources = [];
  //No matter the number in this case only 30 request will be exectured in parllel
  for (let i =0; i<50 ;i++){
      thousendsOfResources.push(`https://www.example.com/resource/${i}`);
  }

  Promise.all(
      thousendsOfResources.map((requestOptions) =>{
          return new Promise(function (reoslve , reject){
              requestQueue.push(function makeRequestTask(){
                  return rp(requestOptions)
                      .then((result)=>{
                          //This single request has finished excuting successfully
                          //Do something with this single result
                          //You must not forget to release the request resource
                          requestQueue.release(); 
                          return result
                      })
                      .catch((error)=>{
                          //This single request has failed excuting
                          //Do something with this error 
                          //You must not forget to release the request resource
                          requestQueue.release(); 
                          throw error;
                      })
              })
          })
      }
  ))
  .then((results)=>{
      //All request have finshed successfully
  })

Synchronous writing to an IPC socket

While Node.js doesn't preform any task in parallel, the OS do. Some of the following messages will be lost because the OS can't write two messages in parallel.

const net = require('net');
const client = net.createConnection({ path: '/tmp/app.sock' }, () => {
    for (let i=0;i<10000;i++){
        client.write(`check out ${i}`)
    }
});

With tasks queue it's super safe and simple to make sure all your messages would arrive

const net = require('net');
const writeQueue = new TasksQueue(1);
const client = net.createConnection({ path: '/tmp/app.sock' }, () => {
    for (let i=0;i<10000;i++){
        writeQueue.push(()=>{
            client.write(`check out ${i}` , writeQueue.release)
        })
    }
});

Asynchronous proccesing thousands of files

  const queue = new TasksQueue(1000);
  const glob = require('glob');
  const {readFile} = require('fs')

  const getFileNames = new Promise(()=>{
      glob('**/*.js', function (er, files) {
          // files is an array of filenames.
          if (err)
              reject(err);
          else //Assume that you have millions of file names in this array
              resolve(files);
      })
  });
  function readAndProcessSingleFile(filename){
      return new Promise((resolve , reject)=>{
          readFile(fileName , (err , data)=>{
              if (err)
                  reject(err);
              else{
                  resolve(data);
                  //doSomething(data);
              }
              queue.release();
          })
      })
  }

  getFileNames
      .then((fileNames)=> 
          Promise.all(
              fileNames.map((fileName)=>
                  new Promise((resolve , reject)=>{
                      queue.push(()=>{

                      });
                  })
              )
          )
      )

License

MIT

zeroq's People

Contributors

greenkeeper[bot] avatar hisco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.