docs: switch from gitter to discord
[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
3eed0bf4 5<h1 align="center">Node.js Worker_Threads and Cluster Worker Pool</h1>
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>
0d04d8bc
JB
20 <a href="https://discord.gg/85VrP8m8">
21 <img alt="Discord" src="https://badgen.net/discord/online-members/85VrP8m8?icon=discord&label=discord&color=green"></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**.
b492f20e 35Please consult our [general guidelines](#general-guidelines).
e76f5485 36
bd2ff6e0 37- Easy to use :white_check_mark:
03babf6f 38- Fixed and dynamic pool size :white_check_mark:
6ec11f90 39- Easy switch from a pool type to another :white_check_mark:
5bc19860 40- Performance [benchmarks](./benchmarks/README.md) :white_check_mark:
82be29c0 41- No runtime dependencies :white_check_mark:
d0ed34c9 42- Proper integration with Node.js [async_hooks](https://nodejs.org/api/async_hooks.html) :white_check_mark:
aad54ad2 43- Support for 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:
aad54ad2 45- Support for multiple task functions :white_check_mark:
9e2f405b 46- Support for task functions CRUD operations at runtime :white_check_mark:
aad54ad2 47- Support for sync and async task functions :white_check_mark:
89a4abfd 48- Tasks distribution strategies :white_check_mark:
84de2e11 49- Lockless tasks queueing :white_check_mark:
72695f86 50- Queued tasks rescheduling:
a6b3272b 51 - Task stealing :white_check_mark:
72695f86 52 - Tasks stealing under back pressure :white_check_mark:
dd951876 53 - Tasks redistribution on worker error :white_check_mark:
b492f20e 54- General guidelines on pool choice :white_check_mark:
82be29c0 55- Error handling out of the box :white_check_mark:
6ec11f90 56- Widely tested :white_check_mark:
82be29c0 57- Active community :white_check_mark:
bd2ff6e0 58- Code quality [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=bugs)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
d28c7996
JB
59 [![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=code_smells)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
60 [![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=duplicated_lines_density)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
61 [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
62 [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
63 [![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=sqale_index)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
bd2ff6e0 64- 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 65
4f8e444d
JB
66## Table of contents
67
68- [Overview](#overview)
69- [Installation](#installation)
70- [Usage](#usage)
8ea47589 71- [Node.js versions](#nodejs-versions)
4f8e444d
JB
72- [API](#api)
73- [General guidelines](#general-guidelines)
74- [Worker choice strategies](#worker-choice-strategies)
75- [Contribute](#contribute)
76- [Team](#team)
77- [License](#license)
34a572eb 78
50aa7901 79## Overview
13031992 80
22c0d9a2 81Poolifier 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 82The first implementation is a fixed worker pool, with a defined number of workers that are started at creation time and will be reused.
a36b9e1f 83The 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 newly created workers will be stopped after a configurable period of inactivity.
22c0d9a2 84You have to implement your worker by extending the _ThreadWorker_ or _ClusterWorker_ class.
50aa7901
S
85
86## Installation
87
88```shell
27c5fb75 89npm install poolifier --save
1a4ec243 90```
1a4ec243 91
50aa7901
S
92## Usage
93
6ec11f90 94You 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
95
96```js
97'use strict'
d2eb4964 98const { ThreadWorker } = require('poolifier')
1a4ec243 99
78cea37e 100function yourFunction(data) {
106744f7 101 // this will be executed in the worker thread,
102 // the data will be received by using the execute method
103 return { ok: 1 }
104}
105
50aa7901 106module.exports = new ThreadWorker(yourFunction, {
124119cf 107 maxInactiveTime: 60000
50aa7901 108})
1a4ec243
APA
109```
110
0feeec4a 111Instantiate your pool based on your needs :
1a4ec243
APA
112
113```js
114'use strict'
6961ca9a 115const { DynamicThreadPool, FixedThreadPool, PoolEvents, availableParallelism } = require('poolifier')
1a4ec243 116
2fbe1783 117// a fixed worker_threads pool
6961ca9a 118const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', {
0eee77cb 119 errorHandler: (e) => console.error(e),
53795b86
JB
120 onlineHandler: () => console.info('worker is online')
121})
1a4ec243 122
2431bdb4 123pool.emitter.on(PoolEvents.ready, () => console.info('Pool is ready'))
53795b86 124pool.emitter.on(PoolEvents.busy, () => console.info('Pool is busy'))
164d950a 125
2fbe1783 126// or a dynamic worker_threads pool
31a7d5be 127const pool = new DynamicThreadPool(Math.floor(availableParallelism() / 2), availableParallelism(), './yourWorker.js', {
0eee77cb 128 errorHandler: (e) => console.error(e),
53795b86
JB
129 onlineHandler: () => console.info('worker is online')
130})
106744f7 131
53795b86 132pool.emitter.on(PoolEvents.full, () => console.info('Pool is full'))
2431bdb4 133pool.emitter.on(PoolEvents.ready, () => console.info('Pool is ready'))
53795b86 134pool.emitter.on(PoolEvents.busy, () => console.info('Pool is busy'))
1a4ec243
APA
135
136// the execute method signature is the same for both implementations,
74ecb519 137// so you can easily switch from one to another
8351c9af 138pool
8923de44 139 .execute()
0eee77cb 140 .then((res) => {
8351c9af
JB
141 console.info(res)
142 })
0eee77cb 143 .catch((err) => {
8351c9af
JB
144 console.error(err)
145 })
1a4ec243
APA
146```
147
6ec11f90 148You can do the same with the classes _ClusterWorker_, _FixedClusterPool_ and _DynamicClusterPool_.
31b90205 149
b0834e9d 150**See [examples](./examples/) for more details**:
b2b02b3d 151
7a923e5b 152- [Javascript](./examples/javascript/)
b2b02b3d 153- [Typescript](./examples/typescript/)
5ba0cd3c 154 - [HTTP client pool](./examples/typescript/http-client-pool/)
9aef1431 155 - [SMTP client pool](./examples/typescript/smtp-client-pool/)
fac9ce96 156 - [HTTP server pool](./examples/typescript/http-server-pool/)
d2bc8d80 157 - [Express worker_threads pool](./examples/typescript/http-server-pool/express-worker_threads/)
15a9c195 158 - [Express cluster pool](./examples/typescript/http-server-pool/express-cluster/)
d0ed34c9 159 - [Express hybrid pool](./examples/typescript/http-server-pool/express-hybrid/)
d2bc8d80
JB
160 - [Fastify worker_threads pool](./examples/typescript/http-server-pool/fastify-worker_threads/)
161 - [Fastify cluster pool](./examples/typescript/http-server-pool/fastify-cluster/)
3b311539 162 - [Fastify hybrid pool](./examples/typescript/http-server-pool/fastify-hybrid/)
9e623684 163 - [WebSocket server pool](./examples/typescript/websocket-server-pool/)
d2bc8d80 164 - [ws worker_threads pool](./examples/typescript/websocket-server-pool/ws-worker_threads/)
72855e92 165 - [ws cluster pool](./examples/typescript/websocket-server-pool/ws-cluster/)
02999424 166 - [ws hybrid pool](./examples/typescript/websocket-server-pool/ws-hybrid/)
1a4ec243 167
6677a3d3 168Remember that workers can only send and receive structured-cloneable data.
deb85c12 169
8ea47589 170## Node.js versions
34a572eb 171
8ea47589 172Node.js versions >= 16.14.x are supported.
1a4ec243 173
b492f20e 174## [API](./docs/api.md)
4680c1b2 175
4f8e444d
JB
176## [General guidelines](./docs/general-guidelines.md)
177
178## [Worker choice strategies](./docs/worker-choice-strategies.md)
48211d04 179
50aa7901 180## Contribute
34a572eb 181
f097c79b 182Choose your task [here](https://github.com/orgs/poolifier/projects/1), propose an idea, a fix, an improvement.
1a4ec243 183
e6973c72 184See [CONTRIBUTING](./CONTRIBUTING.md) guidelines.
0feeec4a 185
50aa7901 186## Team
1a4ec243 187
39deb558 188**Creator/Owner:**
39deb558 189
50aa7901
S
190- [**Alessandro Pio Ardizio**](https://github.com/pioardi)
191
5c47fa7c 192**Maintainers:**
50aa7901 193
50aa7901 194- [**Jérôme Benoit**](https://github.com/jerome-benoit)
39deb558 195
5c47fa7c
JB
196**Contributors:**
197
198- [**Shinigami92**](https://github.com/Shinigami92)
199
50aa7901 200## License
1a4ec243 201
9507c1d4 202[MIT](./LICENSE)