Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

@datapain/matte

1.5.1 • Public • Published

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.

Install

npm i @datapain/matte

DownloadsWeekly Downloads

0

Version

1.5.1

License

MIT

Unpacked Size

78.5 kB

Total Files

63

Last publish

Collaborators

  • avatar