build(deps-dev): apply updates
[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
6ec11f90
JB
163### `pool = new FixedThreadPool/FixedClusterPool(numberOfThreads/numberOfWorkers, filePath, opts)`
164
165`numberOfThreads/numberOfWorkers` (mandatory) Number of workers for this pool
166`filePath` (mandatory) Path to a file with a worker implementation
167`opts` (optional) An object with the pool options properties described below
168
169### `pool = new DynamicThreadPool/DynamicClusterPool(min, max, filePath, opts)`
170
171`min` (mandatory) Same as _FixedThreadPool_/_FixedClusterPool_ numberOfThreads/numberOfWorkers, this number of workers will be always active
172`max` (mandatory) Max number of workers that this pool can contain, the new created workers will die after a threshold (default is 1 minute, you can override it in your worker implementation).
173`filePath` (mandatory) Path to a file with a worker implementation
174`opts` (optional) An object with the pool options properties described below
175
176### `pool.execute(data, name)`
177
178`data` (optional) An object that you want to pass to your worker implementation
179`name` (optional) A string with the task function name that you want to execute on the worker. Default: `'default'`
180
181This method is available on both pool implementations and returns a promise with the task function execution response.
182
183### `pool.destroy()`
184
185This method is available on both pool implementations and will call the terminate method on each worker.
186
5e5ea237 187### `PoolOptions`
50aa7901 188
70e9dd65 189An object with these properties:
50aa7901 190
46e857ca 191- `messageHandler` (optional) - A function that will listen for message event on each worker
f42ca86f
JB
192- `errorHandler` (optional) - A function that will listen for error event on each worker
193- `onlineHandler` (optional) - A function that will listen for online event on each worker
194- `exitHandler` (optional) - A function that will listen for exit event on each worker
46e857ca 195- `workerChoiceStrategy` (optional) - The worker choice strategy to use in this pool:
5519f8f3 196
b4db5969 197 - `WorkerChoiceStrategies.ROUND_ROBIN`: Submit tasks to worker in a round robin fashion
1c6fe997
JB
198 - `WorkerChoiceStrategies.LEAST_USED`: Submit tasks to the worker with the minimum number of executed, executing and queued tasks
199 - `WorkerChoiceStrategies.LEAST_BUSY`: Submit tasks to the worker with the minimum tasks total execution and wait time
a7bbf44a 200 - `WorkerChoiceStrategies.LEAST_ELU`: Submit tasks to the worker with the minimum event loop utilization (ELU) (experimental)
9e2ab226 201 - `WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN`: Submit tasks to worker by using a [weighted round robin scheduling algorithm](./src/pools/selection-strategies/README.md#weighted-round-robin) based on tasks execution time
cb954063 202 - `WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN`: Submit tasks to worker by using an [interleaved weighted round robin scheduling algorithm](./src/pools/selection-strategies/README.md#interleaved-weighted-round-robin) based on tasks execution time (experimental)
129ab848 203 - `WorkerChoiceStrategies.FAIR_SHARE`: Submit tasks to worker by using a [fair share scheduling algorithm](./src/pools/selection-strategies/README.md#fair-share) based on tasks execution time (the default) or ELU active time
5519f8f3 204
9b2bdf94 205 `WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN`, `WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN` and `WorkerChoiceStrategies.FAIR_SHARE` strategies are targeted to heavy and long tasks.
f42ca86f 206 Default: `WorkerChoiceStrategies.ROUND_ROBIN`
5519f8f3 207
da309861
JB
208- `workerChoiceStrategyOptions` (optional) - The worker choice strategy options object to use in this pool.
209 Properties:
210
9adcefab 211 - `measurement` (optional) - The measurement to use in worker choice strategies: `runTime`, `waitTime` or `elu`.
9e2ab226
JB
212 - `runTime` (optional) - Use the tasks [median](./src/pools/selection-strategies/README.md#median) runtime instead of the tasks average runtime in worker choice strategies.
213 - `waitTime` (optional) - Use the tasks [median](./src/pools/selection-strategies/README.md#median) wait time instead of the tasks average wait time in worker choice strategies.
214 - `elu` (optional) - Use the tasks [median](./src/pools/selection-strategies/README.md#median) ELU instead of the tasks average ELU in worker choice strategies.
129ab848 215 - `weights` (optional) - The worker weights to use in weighted round robin worker choice strategies: `{ 0: 200, 1: 300, ..., n: 100 }`.
7171d33f 216
9adcefab 217 Default: `{ runTime: { median: false }, waitTime: { median: false }, elu: { median: false } }`
da309861 218
1f68cede 219- `restartWorkerOnError` (optional) - Restart worker on uncaught error in this pool.
609154c5 220 Default: `true`
dfd00839 221- `enableEvents` (optional) - Events emission enablement in this pool.
609154c5 222 Default: `true`
dfd00839 223- `enableTasksQueue` (optional) - Tasks queue per worker enablement in this pool.
609154c5 224 Default: `false`
34a572eb 225
83d127b9 226- `tasksQueueOptions` (optional) - The worker tasks queue options object to use in this pool.
7171d33f
JB
227 Properties:
228
229 - `concurrency` (optional) - The maximum number of tasks that can be executed concurrently on a worker.
230
dfd00839 231 Default: `{ concurrency: 1 }`
7171d33f 232
92e7183f 233#### `ThreadPoolOptions extends PoolOptions`
70e9dd65 234
129ab848 235- `workerOptions` (optional) - An object with the worker options to pass to worker. See [worker_threads](https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options) for more details.
70e9dd65 236
92e7183f 237#### `ClusterPoolOptions extends PoolOptions`
70e9dd65 238
129ab848 239- `env` (optional) - An object with the environment variables to pass to worker. See [cluster](https://nodejs.org/api/cluster.html#cluster_cluster_fork_env) for more details.
70e9dd65 240
430745c9 241- `settings` (optional) - An object with the cluster settings. See [cluster](https://nodejs.org/api/cluster.html#cluster_cluster_settings) for more details.
70e9dd65 242
31b90205 243### `class YourWorker extends ThreadWorker/ClusterWorker`
50aa7901 244
ef9f489c 245`taskFunctions` (mandatory) The task function or task functions object `{ name_1: fn_1, ..., name_n: fn_n }` that you want to execute on the worker
50aa7901
S
246`opts` (optional) An object with these properties:
247
b0a4db63
JB
248- `maxInactiveTime` (optional) - Maximum waiting time in milliseconds for tasks on newly created workers. After this time newly created workers will die.
249 The last active time of your worker will be updated when it terminates a task.
920278a2 250 If `killBehavior` is set to `KillBehaviors.HARD` this value represents also the timeout for the tasks that you submit to the pool, when this timeout expires your tasks is interrupted before completion and removed. The worker is killed if is not part of the minimum size of the pool.
3832ad95 251 If `killBehavior` is set to `KillBehaviors.SOFT` your tasks have no timeout and your workers will not be terminated until your task is completed.
609154c5 252 Default: `60000`
15eacd5d 253
b0a4db63 254- `killBehavior` (optional) - Dictates if your worker will be deleted in case a task is active on it.
a6a2dc4c
JB
255 **KillBehaviors.SOFT**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker **won't** be deleted.
256 **KillBehaviors.HARD**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker will be deleted.
3832ad95 257 This option only apply to the newly created workers.
f42ca86f 258 Default: `KillBehaviors.SOFT`
50aa7901 259
968a2e8c
JB
260#### `YourWorker.hasTaskFunction(name)`
261
262`name` (mandatory) The task function name
263
264This method is available on both worker implementations and returns a boolean.
265
266#### `YourWorker.addTaskFunction(name, fn)`
267
3c0c44a0 268`name` (mandatory) The task function name
968a2e8c
JB
269`fn` (mandatory) The task function
270
271This method is available on both worker implementations and returns a boolean.
272
273#### `YourWorker.removeTaskFunction(name)`
274
275`name` (mandatory) The task function name
276
277This method is available on both worker implementations and returns a boolean.
278
c50b93fb
JB
279#### `YourWorker.listTaskFunctions()`
280
281This method is available on both worker implementations and returns an array of the task function names.
282
968a2e8c
JB
283#### `YourWorker.setDefaultTaskFunction(name)`
284
285`name` (mandatory) The task function name
286
287This method is available on both worker implementations and returns a boolean.
288
e36cfb13 289## General guidance
34a572eb 290
6ec11f90
JB
291Performance is one of the main target of these worker pool implementations, poolifier team wants to have a strong focus on this.
292Poolifier already has a [benchmarks](./benchmarks/) folder where you can find some comparisons.
e36cfb13
APA
293
294### Internal Node.js thread pool
295
296Before to jump into each poolifier pool type, let highlight that **Node.js comes with a thread pool already**, the libuv thread pool where some particular tasks already run by default.
297Please take a look at [which tasks run on the libuv thread pool](https://nodejs.org/en/docs/guides/dont-block-the-event-loop/#what-code-runs-on-the-worker-pool).
298
e843b904 299**If your task runs on libuv thread pool**, you can try to:
e36cfb13 300
0feeec4a 301- Tune the libuv thread pool size setting the [UV_THREADPOOL_SIZE](https://nodejs.org/api/cli.html#cli_uv_threadpool_size_size).
e36cfb13
APA
302
303and/or
304
6ec11f90 305- Use poolifier cluster pools that are spawning child processes, they will also increase the number of libuv threads since that any new child process comes with a separated libuv thread pool. **More threads does not mean more fast, so please tune your application**.
e36cfb13
APA
306
307### Cluster vs Threads worker pools
308
6ec11f90 309**If your task does not run into libuv thread pool** and is CPU intensive then poolifier **thread pools** (_FixedThreadPool_ and _DynamicThreadPool_) are suggested to run CPU intensive tasks, you can still run I/O intensive tasks into thread pools, but performance enhancement is expected to be minimal.
7aead695 310Thread pools are built on top of Node.js [worker_threads](https://nodejs.org/api/worker_threads.html) module.
063f8c5b 311
6ec11f90 312**If your task does not run into libuv thread pool** and is I/O intensive then poolifier **cluster pools** (_FixedClusterPool_ and _DynamicClusterPool_) are suggested to run I/O intensive tasks, again you can still run CPU intensive tasks into cluster pools, but performance enhancement is expected to be minimal.
e36cfb13 313Consider that by default Node.js already has great performance for I/O tasks (asynchronous I/O).
4a6952ff 314Cluster pools are built on top of Node.js [cluster](https://nodejs.org/api/cluster.html) module.
75876e4c 315
e36cfb13 316If your task contains code that runs on libuv plus code that is CPU intensive or I/O intensive you either split it either combine more strategies (i.e. tune the number of libuv threads and use cluster/thread pools).
0feeec4a 317But in general, **always profile your application**.
e36cfb13
APA
318
319### Fixed vs Dynamic pools
063f8c5b 320
6ec11f90
JB
321To choose your pool consider first that with a _FixedThreadPool_/_FixedClusterPool_ or a _DynamicThreadPool_/_DynamicClusterPool_ your application memory footprint will increase.
322By doing so, your application will be ready to execute in parallel more tasks, but during idle time your application will consume more memory.
03babf6f 323One good choice from poolifier team point of view is to profile your application using a fixed or dynamic worker pool, and analyze your application metrics when you increase/decrease the number of workers.
6ec11f90 324For example you could keep the memory footprint low by choosing a _DynamicThreadPool_/_DynamicClusterPool_ with a minimum of 5 workers, and allowing it to create new workers until a maximum of 50 workers if needed. This is the advantage of using a _DynamicThreadPool_/_DynamicClusterPool_.
0feeec4a 325But in general, **always profile your application**.
48211d04 326
50aa7901 327## Contribute
34a572eb 328
91633990 329Choose your task here [2.6.x](https://github.com/orgs/poolifier/projects/1), propose an idea, a fix, an improvement.
1a4ec243 330
0feeec4a
JB
331See [CONTRIBUTING](CONTRIBUTING.md) guidelines.
332
50aa7901 333## Team
1a4ec243 334
39deb558 335**Creator/Owner:**
39deb558 336
50aa7901
S
337- [**Alessandro Pio Ardizio**](https://github.com/pioardi)
338
339**_Contributors_**
340
341- [**Shinigami92**](https://github.com/Shinigami92)
342- [**Jérôme Benoit**](https://github.com/jerome-benoit)
39deb558 343
50aa7901 344## License
1a4ec243 345
9507c1d4 346[MIT](./LICENSE)