2 <img src="./images/logo.png" width="340px" height="266px"/>
5 <h2 align="center">Node Thread Pool and Cluster Pool :arrow_double_up: :on:</h2>
7 <a href="https://ko-fi.com/Q5Q31D6QY">
8 <img alt="Ko-fi" src="https://ko-fi.com/img/githubbutton_sm.svg"></a>
12 <a href="https://www.npmjs.com/package/poolifier">
13 <img alt="Weekly Downloads" src="https://img.shields.io/npm/dw/poolifier"></a>
14 <a href="https://github.com/pioardi/node-pool/actions">
15 <img alt="Actions Status" src="https://github.com/pioardi/node-pool/workflows/NodeCI/badge.svg"></a>
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>
18 <a href="https://sonarcloud.io/component_measures/metric/coverage/list?id=pioardi_poolifier">
19 <img alt="Code coverage" src="https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=coverage"></a>
20 <a href="https://standardjs.com">
21 <img alt="Javascript Standard Style Guide" src="https://img.shields.io/badge/code_style-standard-brightgreen.svg"></a>
22 <a href="https://gitter.im/poolifier/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge">
23 <img alt="Gitter chat" src="https://badges.gitter.im/poolifier/community.svg"></a>
24 <a href="https://badgen.net/dependabot/dependabot/dependabot-core/?icon=dependabot">
25 <img alt="Dependabot" src="https://badgen.net/dependabot/dependabot/dependabot-core/?icon=dependabot"></a>
26 <a href="http://makeapullrequest.com">
27 <img alt="PR Welcome" src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square"></a>
28 <a href="https://img.shields.io/static/v1?label=dependencies&message=no%20dependencies&color=brightgreen">
29 <img alt="No dependencies" src="https://img.shields.io/static/v1?label=dependencies&message=no%20dependencies&color=brightgreen"></a>
34 Poolifier is used to perform CPU intensive and I/O intensive tasks on nodejs servers, it implements worker pools (yes, more worker pool implementations, so you can choose which one fit better for you) using [worker-threads](https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads) and cluster pools using [Node.js cluster](https://nodejs.org/api/cluster.html) modules.
35 With poolifier you can improve your **performance** and resolve problems related to the event loop.
36 Moreover you can execute your tasks using an API designed to improve the **developer experience**.
37 Please consult our <a href="#general-guidance">general guidelines</a>
39 - Performance :racehorse: [benchmarks](./benchmarks/README.md)
40 - Security :bank: :cop: [![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)
41 - Easy to use :couple:
42 - Easy switch from a pool to another, easy to tune :heavy_check_mark:
43 - Dynamic pool size :heavy_check_mark:
44 - No runtime dependencies :heavy_check_mark:
45 - Proper async integration with node async hooks :heavy_check_mark:
46 - Support for worker threads and cluster node modules :heavy_check_mark:
47 - Support sync and async tasks :heavy_check_mark:
48 - General guidance on pools to use :heavy_check_mark:
49 - Widely tested :heavy_check_mark:
50 - Error handling out of the box :heavy_check_mark:
51 - Active community :heavy_check_mark:
52 - Code quality :octocat: [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=bugs)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
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)
62 <a href="#overview">Overview</a>
64 <a href="#installation">Installation</a>
66 <a href="#usage">Usage</a>
68 <a href="#node-versions"> Node versions</a>
70 <a href="#api">API</a>
72 <a href="#general-guidance">General guidance</a>
74 <a href="#contribute">Contribute</a>
76 <a href="#team">Team</a>
78 <a href="#license">License</a>
83 Node pool contains two [worker-threads](https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads)/[cluster worker](https://nodejs.org/api/cluster.html#cluster_class_worker) pool implementations, you don't have to deal with worker-threads/cluster worker complexity.
84 The first implementation is a static worker pool, with a defined number of workers that are started at creation time and will be reused.
85 The 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.
86 You have to implement your worker extending the ThreadWorker or ClusterWorker class
91 npm install poolifier --save
96 You can implement a worker-threads worker in a simple way by extending the class ThreadWorker:
100 const { ThreadWorker } = require('poolifier')
102 function yourFunction (data) {
103 // this will be executed in the worker thread,
104 // the data will be received by using the execute method
108 module.exports = new ThreadWorker(yourFunction, {
109 maxInactiveTime: 60000,
114 Instantiate your pool based on your needed :
118 const { FixedThreadPool, DynamicThreadPool } = require('poolifier')
120 // a fixed worker-threads pool
121 const pool = new FixedThreadPool(15,
123 { errorHandler: (e) => console.error(e), onlineHandler: () => console.log('worker is online') })
125 // or a dynamic worker-threads pool
126 const pool = new DynamicThreadPool(10, 100,
128 { errorHandler: (e) => console.error(e), onlineHandler: () => console.log('worker is online') })
130 pool.emitter.on('busy', () => console.log('Pool is busy'))
132 // the execute method signature is the same for both implementations,
133 // so you can easy switch from one to another
134 pool.execute({}).then(res => {
140 You can do the same with the classes ClusterWorker, FixedClusterPool and DynamicClusterPool.
142 **See examples folder for more details (in particular if you want to use a pool for [multiple functions](./examples/multiFunctionExample.js)).**
143 **Now TypeScript is also supported, find how to use it into the example folder**.
145 Remember that workers can only send and receive serializable data.
149 You can use node versions 12.x, 13.x, 14.x
153 ### `pool = new FixedThreadPool/FixedClusterPool(numberOfThreads/numberOfWorkers, filePath, opts)`
155 `numberOfThreads/numberOfWorkers` (mandatory) Num of workers for this worker pool
156 `filePath` (mandatory) Path to a file with a worker implementation
157 `opts` (optional) An object with these properties :
159 - `errorHandler` - A function that will listen for error event on each worker
160 - `onlineHandler` - A function that will listen for online event on each worker
161 - `exitHandler` - A function that will listen for exit event on each worker
163 ### `pool = new DynamicThreadPool/DynamicClusterPool(min, max, filePath, opts)`
165 `min` (mandatory) Same as FixedThreadPool/FixedClusterPool numberOfThreads/numberOfWorkers, this number of workers will be always active
166 `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).
167 `filePath` (mandatory) Same as FixedThreadPool/FixedClusterPool
168 `opts` (optional) Same as FixedThreadPool/FixedClusterPool
170 ### `pool.execute(data)`
172 Execute method is available on both pool implementations (return type : Promise):
173 `data` (mandatory) An object that you want to pass to your worker implementation
177 Destroy method is available on both pool implementations.
178 This method will call the terminate method on each worker.
180 ### `class YourWorker extends ThreadWorker/ClusterWorker`
182 `fn` (mandatory) The function that you want to execute on the worker
183 `opts` (optional) An object with these properties:
185 - `maxInactiveTime` - Max time to wait tasks to work on (in ms), after this period the new worker will die.
186 The last active time of your worker unit will be updated when a task is submitted to a worker or when a worker terminate a task.
187 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 and the worker is killed if is not part of the minimum size of the pool.
188 If `killBehavior` is set to `KillBehaviors.SOFT` your tasks have no timeout and your workers will not be terminated until your task is completed.
191 - `async` - true/false, true if your function contains async pieces else false
192 - `killBehavior` - Dictates if your async unit (worker/process) will be deleted in case that a task is active on it.
193 **SOFT**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker **won't** be deleted.
194 **HARD**: If `lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted.
195 This option only apply to the newly created workers.
200 Performance is one of the main target of these worker pool implementations, we want to have a strong focus on this.
201 We already have a bench folder where you can find some comparisons.
203 ### Internal Node.js thread pool
205 Before 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.
206 Please 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).
208 Now **if your task runs on libuv thread pool**, you can try to:
210 - Tune the libuv thread pool size setting the [UV_THREADPOOL_SIZE](https://nodejs.org/api/cli.html#cli_uv_threadpool_size_size)
214 - Use poolifier cluster pool that spawning child processes 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.**
216 ### Cluster vs Threads worker pools
218 **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.
219 Thread pools are built on top of Node.js [worker-threads](https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads) module.
221 **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.
222 Consider that by default Node.js already has great performance for I/O tasks (asynchronous I/O).
223 Cluster pools are built on top of Node.js [cluster](https://nodejs.org/api/cluster.html) module.
225 If 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).
226 But in general, **always profile your application**
228 ### Fixed vs Dynamic pools
230 To choose your pool consider that with a FixedThreadPool/FixedClusterPool or a DynamicThreadPool/DynamicClusterPool (in this case is important the min parameter passed to the constructor) your application memory footprint will increase.
231 Increasing the memory footprint, your application will be ready to accept more tasks, but during idle time your application will consume more memory.
232 One good choose from my point of view is to profile your application using Fixed/Dynamic worker pool, and to see your application metrics when you increase/decrease the num of workers.
233 For example you could keep the memory footprint low choosing a DynamicThreadPool/DynamicClusterPool with 5 workers, and allow to create new workers until 50/100 when needed, this is the advantage to use the DynamicThreadPool/DynamicClusterPool.
234 But in general, **always profile your application**
238 See guidelines [CONTRIBUTING](CONTRIBUTING.md)
239 Choose your task here [2.0.0](https://github.com/pioardi/poolifier/projects/1), propose an idea, a fix, an improvement.
243 <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
244 <!-- prettier-ignore-start -->
245 <!-- markdownlint-disable -->
249 - [**Alessandro Pio Ardizio**](https://github.com/pioardi)
253 - [**Shinigami92**](https://github.com/Shinigami92)
254 - [**Jérôme Benoit**](https://github.com/jerome-benoit)