JSPM

  • Created
  • Published
  • Downloads 105
  • Score
    100M100P100Q46354F
  • 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

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 implimentation
  },
  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: ' ',
    }
  }
});

Versioning

We use SemVer for versioning.