JSPM

  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q46051F
  • License MIT

Implementation a thread pool pattern for node.js

Package Exports

  • @datapain/matte

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@datapain/matte) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Matte

Matte is an implementation a thread pool pattern for node.js

Thead pool description image

As we know Node.js is asynchronous but some features stay synchronous. The matte solution can help you to avoid blocking your main thread and compute sync things in another Event loop.

Built with

  • Node.js - Platform
  • Typescript - typed superset of JavaScript that compiles to plain JavaScript

Prerequisites

Installation

npm install @datapain/matte

Usage

Use Worker Pool by promise handling:

import { WorkerPool, QueueType } from '@datapain/matte';

const pool = new WorkerPool({
  queueType: QueueType.PRIORITY, // or QueueType.FIFO; or your custom QueueType which implement WorkerPoolQueueInterface; by default is QueueType.PRIORITY
  maxWorkers: 4, // by default used cpus length
  worker: {
    resourceLimits: { // by default used native worker limits
      maxOldGenerationSizeMb: 64,
      maxYoungGenerationSizeMb: 16,
      codeRangeSizeMb: 4,
    },
    timeout: 1000, // by default is 3000 ms
    executable: './path/to/your/Worker-file', // by default used matte implementation
  },
  persistentContextFn: () => {
    this.GLOBAL_CONTEXT = 'MATTE IS';
  },
});

new Promise((resolve, reject) => pool.add({
  resolve,
  reject,
  handler: (data) => GLOBAL_CONTEXT + MESSAGE_CONTEXT + data,
  config: {
    data: 'AWESOME',
    ctx: {
      MESSAGE_CONTEXT: ' ',
    }
  }
}));

Also, you can use it with the callback:

import { WorkerPool, QueueType } from '@datapain/matte';

const pool = new WorkerPool({
  queueType: QueueType.PRIORITY, // or QueueType.FIFO; or your custom QueueType which implement WorkerPoolQueueInterface; by default is QueueType.PRIORITY
  maxWorkers: 4, // by default used cpus length
  worker: {
    resourceLimits: { // by default used native worker limits
      maxOldGenerationSizeMb: 64,
      maxYoungGenerationSizeMb: 16,
      codeRangeSizeMb: 4,
    },
    timeout: 1000, // by default is 3000 ms
    executable: './path/to/your/Worker-file', // by default used matte implementation
  },
  persistentContextFn: () => {
    this.GLOBAL_CONTEXT = 'MATTE IS';
  },
});

new Promise((resolve, reject) => pool.add({
  callback: (err, data) => {
    if(err) reject(err);
    resolve(data);
  },
  handler: (data) => GLOBAL_CONTEXT + MESSAGE_CONTEXT + data,
  config: {
    data: 'AWESOME',
    ctx: {
      MESSAGE_CONTEXT: ' ',
    }
  }
}));

finally, the simple way:

import { WorkerPool, TaskPriority } from '@datapain/matte';
import fetch from 'node-fetch'

const pool = new WorkerPool();
pool.add({
  callback: (err, data) => {
    if(err) console.error(err);
    console.log(data);
  },
  handler: () => fetch('https://github.com/').then(res => res.text()), 
}, TaskPriority.CRITICAL);

Versioning

We use SemVer for versioning.