Merge pull request #811 from namandeep-123/move-readme-content-to-typedoc
[poolifier.git] / README.md
CommitLineData
f2b5671a 1<div align="center">
0feeec4a 2 <img src="./images/logo.png" width="340px" height="266px"/>
ddaa3a76 3</div>
4
063f8c5b 5<h2 align="center">Node Thread Pool and Cluster Pool :arrow_double_up: :on:</h2>
1d4f79e7
APA
6
7<p align="center">
23fc6cf8
JB
8 <a href="https://github.com/poolifier/poolifier/graphs/commit-activity">
9 <img alt="GitHub commit activity (master)" src="https://img.shields.io/github/commit-activity/m/poolifier/poolifier/master"></a>
1d4f79e7
APA
10 <a href="https://www.npmjs.com/package/poolifier">
11 <img alt="Weekly Downloads" src="https://img.shields.io/npm/dw/poolifier"></a>
d7757502 12 <a href="https://github.com/poolifier/poolifier/actions/workflows/ci.yml">
86f8562e 13 <img alt="Actions Status" src="https://github.com/poolifier/poolifier/actions/workflows/ci.yml/badge.svg"></a>
609edfec 14 <a href="https://sonarcloud.io/dashboard?id=pioardi_poolifier">
22147b67 15 <img alt="Code Coverage" src="https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=coverage"></a>
8076b106
JB
16 <a href="https://sonarcloud.io/dashboard?id=pioardi_poolifier">
17 <img alt="Quality Gate Status" src="https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=alert_status"></a>
1d4f79e7
APA
18 <a href="https://standardjs.com">
19 <img alt="Javascript Standard Style Guide" src="https://img.shields.io/badge/code_style-standard-brightgreen.svg"></a>
20 <a href="https://gitter.im/poolifier/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge">
21 <img alt="Gitter chat" src="https://badges.gitter.im/poolifier/community.svg"></a>
e8a862ca
JB
22 <a href="https://opencollective.com/poolifier">
23 <img alt="Open Collective" src="https://opencollective.com/poolifier/tiers/badge.svg"></a>
1d4f79e7
APA
24 <a href="http://makeapullrequest.com">
25 <img alt="PR Welcome" src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square"></a>
26 <a href="https://img.shields.io/static/v1?label=dependencies&message=no%20dependencies&color=brightgreen">
27 <img alt="No dependencies" src="https://img.shields.io/static/v1?label=dependencies&message=no%20dependencies&color=brightgreen"></a>
1d4f79e7
APA
28</p>
29
74750c7f 30## Why Poolifier?
e76f5485 31
6ec11f90 32Poolifier is used to perform CPU and/or I/O intensive tasks on Node.js servers, it implements worker pools using [worker_threads](https://nodejs.org/api/worker_threads.html) and [cluster](https://nodejs.org/api/cluster.html) Node.js modules.
50aa7901 33With poolifier you can improve your **performance** and resolve problems related to the event loop.
063f8c5b 34Moreover you can execute your tasks using an API designed to improve the **developer experience**.
0feeec4a 35Please consult our [general guidelines](#general-guidance).
e76f5485 36
bd2ff6e0
JB
37- Easy to use :white_check_mark:
38- Performance [benchmarks](./benchmarks/README.md) :white_check_mark:
03babf6f 39- Fixed and dynamic pool size :white_check_mark:
6ec11f90 40- Easy switch from a pool type to another :white_check_mark:
82be29c0 41- No runtime dependencies :white_check_mark:
cb954063 42- Proper integration with node [async_hooks](https://nodejs.org/api/async_hooks.html) :white_check_mark:
bd2ff6e0 43- Support CommonJS, ESM, and TypeScript :white_check_mark:
6ec11f90 44- Support for [worker_threads](https://nodejs.org/api/worker_threads.html) and [cluster](https://nodejs.org/api/cluster.html) Node.js modules :white_check_mark:
82ea6492
JB
45- Support multiple task functions :white_check_mark:
46- Support sync and async task functions :white_check_mark:
89a4abfd 47- Tasks distribution strategies :white_check_mark:
bd2ff6e0 48- General guidance on pool choice :white_check_mark:
82be29c0 49- Error handling out of the box :white_check_mark:
6ec11f90 50- Widely tested :white_check_mark:
82be29c0 51- Active community :white_check_mark:
bd2ff6e0 52- Code quality [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=bugs)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
d28c7996
JB
53 [![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=code_smells)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
54 [![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=duplicated_lines_density)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
55 [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
56 [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
57 [![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=sqale_index)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
bd2ff6e0 58- Code security [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=security_rating)](https://sonarcloud.io/dashboard?id=pioardi_poolifier) [![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=vulnerabilities)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
ddaa3a76 59
50aa7901 60## Contents
e76f5485 61
34a572eb 62<h3 align="center">
50aa7901
S
63 <a href="#overview">Overview</a>
64 <span> · </span>
34a572eb 65 <a href="#installation">Installation</a>
66 <span> · </span>
67 <a href="#usage">Usage</a>
68 <span> · </span>
4f4ae1cb 69 <a href="#node-versions">Node versions</a>
50aa7901 70 <span> · </span>
34a572eb 71 <a href="#api">API</a>
72 <span> · </span>
e36cfb13 73 <a href="#general-guidance">General guidance</a>
48211d04 74 <span> · </span>
34a572eb 75 <a href="#contribute">Contribute</a>
76 <span> · </span>
50aa7901 77 <a href="#team">Team</a>
34a572eb 78 <span> · </span>
79 <a href="#license">License</a>
80</h3>
81
50aa7901 82## Overview
13031992 83
22c0d9a2 84Poolifier contains two [worker_threads](https://nodejs.org/api/worker_threads.html#class-worker)/[cluster](https://nodejs.org/api/cluster.html#cluster_class_worker) worker pool implementations, you don't have to deal with [worker_threads](https://nodejs.org/api/worker_threads.html)/[cluster](https://nodejs.org/api/cluster.html) complexity.
03babf6f 85The first implementation is a fixed worker pool, with a defined number of workers that are started at creation time and will be reused.
56bbcb6b 86The second implementation is a dynamic worker pool, with a number of worker started at creation time (these workers will be always active and reused) and other workers created when the load will increase (with an upper limit, these workers will be reused when active), the new created workers will be stopped after a configurable period of inactivity.
22c0d9a2 87You have to implement your worker by extending the _ThreadWorker_ or _ClusterWorker_ class.
50aa7901
S
88
89## Installation
90
91```shell
27c5fb75 92npm install poolifier --save
1a4ec243 93```
1a4ec243 94
50aa7901
S
95## Usage
96
6ec11f90 97You can implement a [worker_threads](https://nodejs.org/api/worker_threads.html#class-worker) worker in a simple way by extending the class _ThreadWorker_:
1a4ec243
APA
98
99```js
100'use strict'
d2eb4964 101const { ThreadWorker } = require('poolifier')
1a4ec243 102
78cea37e 103function yourFunction(data) {
106744f7 104 // this will be executed in the worker thread,
105 // the data will be received by using the execute method
106 return { ok: 1 }
107}
108
50aa7901 109module.exports = new ThreadWorker(yourFunction, {
124119cf 110 maxInactiveTime: 60000
50aa7901 111})
1a4ec243
APA
112```
113
0feeec4a 114Instantiate your pool based on your needs :
1a4ec243
APA
115
116```js
117'use strict'
6961ca9a 118const { DynamicThreadPool, FixedThreadPool, PoolEvents, availableParallelism } = require('poolifier')
1a4ec243 119
2fbe1783 120// a fixed worker_threads pool
6961ca9a 121const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', {
53795b86
JB
122 errorHandler: e => console.error(e),
123 onlineHandler: () => console.info('worker is online')
124})
1a4ec243 125
2431bdb4 126pool.emitter.on(PoolEvents.ready, () => console.info('Pool is ready'))
53795b86 127pool.emitter.on(PoolEvents.busy, () => console.info('Pool is busy'))
164d950a 128
2fbe1783 129// or a dynamic worker_threads pool
31a7d5be 130const pool = new DynamicThreadPool(Math.floor(availableParallelism() / 2), availableParallelism(), './yourWorker.js', {
53795b86
JB
131 errorHandler: e => console.error(e),
132 onlineHandler: () => console.info('worker is online')
133})
106744f7 134
53795b86 135pool.emitter.on(PoolEvents.full, () => console.info('Pool is full'))
2431bdb4 136pool.emitter.on(PoolEvents.ready, () => console.info('Pool is ready'))
53795b86 137pool.emitter.on(PoolEvents.busy, () => console.info('Pool is busy'))
1a4ec243
APA
138
139// the execute method signature is the same for both implementations,
140// so you can easy switch from one to another
8351c9af 141pool
8923de44 142 .execute()
8351c9af
JB
143 .then(res => {
144 console.info(res)
145 })
146 .catch(err => {
147 console.error(err)
148 })
1a4ec243
APA
149```
150
6ec11f90 151You can do the same with the classes _ClusterWorker_, _FixedClusterPool_ and _DynamicClusterPool_.
31b90205 152
82ea6492 153**See [examples](./examples/) folder for more details (in particular if you want to use a pool with [multiple task functions](./examples/multiFunctionExample.js))**.
1a4ec243 154
6677a3d3 155Remember that workers can only send and receive structured-cloneable data.
deb85c12 156
50aa7901 157## Node versions
34a572eb 158
9e45c2c4 159Node versions >= 16.14.x are supported.
1a4ec243 160
059ea823 161## [API](https://poolifier.github.io/poolifier/)
4680c1b2 162
cea8e6b1 163[**API Details**](./docs/api-details.md)
6ec11f90 164
cea8e6b1 165## General Guideline
6ec11f90 166
cea8e6b1 167For general guidelines, please refer to [this document](./docs/general-guidelines.md)
48211d04 168
50aa7901 169## Contribute
34a572eb 170
91633990 171Choose your task here [2.6.x](https://github.com/orgs/poolifier/projects/1), propose an idea, a fix, an improvement.
1a4ec243 172
0feeec4a
JB
173See [CONTRIBUTING](CONTRIBUTING.md) guidelines.
174
50aa7901 175## Team
1a4ec243 176
39deb558 177**Creator/Owner:**
39deb558 178
50aa7901
S
179- [**Alessandro Pio Ardizio**](https://github.com/pioardi)
180
181**_Contributors_**
182
183- [**Shinigami92**](https://github.com/Shinigami92)
184- [**Jérôme Benoit**](https://github.com/jerome-benoit)
39deb558 185
50aa7901 186## License
1a4ec243 187
9507c1d4 188[MIT](./LICENSE)