| 1 | <div align="center"> |
| 2 | <img src="./images/logo.png" width="340px" height="266px"/> |
| 3 | </div> |
| 4 | |
| 5 | <h2 align="center">Node Thread Pool and Cluster Pool :arrow_double_up: :on:</h2> |
| 6 | |
| 7 | <p align="center"> |
| 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> |
| 10 | <a href="https://www.npmjs.com/package/poolifier"> |
| 11 | <img alt="Weekly Downloads" src="https://img.shields.io/npm/dw/poolifier"></a> |
| 12 | <a href="https://github.com/poolifier/poolifier/actions/workflows/ci.yml"> |
| 13 | <img alt="Actions Status" src="https://github.com/poolifier/poolifier/actions/workflows/ci.yml/badge.svg"></a> |
| 14 | <a href="https://sonarcloud.io/dashboard?id=pioardi_poolifier"> |
| 15 | <img alt="Code Coverage" src="https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=coverage"></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://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> |
| 22 | <a href="https://opencollective.com/poolifier"> |
| 23 | <img alt="Open Collective" src="https://opencollective.com/poolifier/tiers/badge.svg"></a> |
| 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> |
| 28 | </p> |
| 29 | |
| 30 | ## Why Poolifier? |
| 31 | |
| 32 | Poolifier 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. |
| 33 | With poolifier you can improve your **performance** and resolve problems related to the event loop. |
| 34 | Moreover you can execute your tasks using an API designed to improve the **developer experience**. |
| 35 | Please consult our [general guidelines](#general-guidance). |
| 36 | |
| 37 | - Easy to use :white_check_mark: |
| 38 | - Performance [benchmarks](./benchmarks/README.md) :white_check_mark: |
| 39 | - Static and dynamic pool size :white_check_mark: |
| 40 | - Easy switch from a pool type to another :white_check_mark: |
| 41 | - No runtime dependencies :white_check_mark: |
| 42 | - Proper async integration with node [async_hooks](https://nodejs.org/api/async_hooks.html) :white_check_mark: |
| 43 | - Support CommonJS, ESM, and TypeScript :white_check_mark: |
| 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: |
| 45 | - Support sync and async tasks :white_check_mark: |
| 46 | - Tasks distribution strategies :white_check_mark: |
| 47 | - General guidance on pool choice :white_check_mark: |
| 48 | - Error handling out of the box :white_check_mark: |
| 49 | - Widely tested :white_check_mark: |
| 50 | - Active community :white_check_mark: |
| 51 | - Code quality [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=bugs)](https://sonarcloud.io/dashboard?id=pioardi_poolifier) |
| 52 | [![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=code_smells)](https://sonarcloud.io/dashboard?id=pioardi_poolifier) |
| 53 | [![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=duplicated_lines_density)](https://sonarcloud.io/dashboard?id=pioardi_poolifier) |
| 54 | [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=pioardi_poolifier) |
| 55 | [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=pioardi_poolifier) |
| 56 | [![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=sqale_index)](https://sonarcloud.io/dashboard?id=pioardi_poolifier) |
| 57 | - 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) |
| 58 | |
| 59 | ## Contents |
| 60 | |
| 61 | <h3 align="center"> |
| 62 | <a href="#overview">Overview</a> |
| 63 | <span> · </span> |
| 64 | <a href="#installation">Installation</a> |
| 65 | <span> · </span> |
| 66 | <a href="#usage">Usage</a> |
| 67 | <span> · </span> |
| 68 | <a href="#node-versions">Node versions</a> |
| 69 | <span> · </span> |
| 70 | <a href="#api">API</a> |
| 71 | <span> · </span> |
| 72 | <a href="#general-guidance">General guidance</a> |
| 73 | <span> · </span> |
| 74 | <a href="#contribute">Contribute</a> |
| 75 | <span> · </span> |
| 76 | <a href="#team">Team</a> |
| 77 | <span> · </span> |
| 78 | <a href="#license">License</a> |
| 79 | </h3> |
| 80 | |
| 81 | ## Overview |
| 82 | |
| 83 | Poolifier 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. |
| 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 by extending the _ThreadWorker_ or _ClusterWorker_ class. |
| 87 | |
| 88 | ## Installation |
| 89 | |
| 90 | ```shell |
| 91 | npm install poolifier --save |
| 92 | ``` |
| 93 | |
| 94 | ## Usage |
| 95 | |
| 96 | You can implement a [worker_threads](https://nodejs.org/api/worker_threads.html#class-worker) worker in a simple way by extending the class _ThreadWorker_: |
| 97 | |
| 98 | ```js |
| 99 | 'use strict' |
| 100 | const { ThreadWorker } = require('poolifier') |
| 101 | |
| 102 | function yourFunction(data) { |
| 103 | // this will be executed in the worker thread, |
| 104 | // the data will be received by using the execute method |
| 105 | return { ok: 1 } |
| 106 | } |
| 107 | |
| 108 | module.exports = new ThreadWorker(yourFunction, { |
| 109 | maxInactiveTime: 60000 |
| 110 | }) |
| 111 | ``` |
| 112 | |
| 113 | Instantiate your pool based on your needs : |
| 114 | |
| 115 | ```js |
| 116 | 'use strict' |
| 117 | const { DynamicThreadPool, FixedThreadPool, PoolEvents, availableParallelism } = require('poolifier') |
| 118 | |
| 119 | // a fixed worker_threads pool |
| 120 | const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', { |
| 121 | errorHandler: e => console.error(e), |
| 122 | onlineHandler: () => console.info('worker is online') |
| 123 | }) |
| 124 | |
| 125 | pool.emitter.on(PoolEvents.ready, () => console.info('Pool is ready')) |
| 126 | pool.emitter.on(PoolEvents.busy, () => console.info('Pool is busy')) |
| 127 | |
| 128 | // or a dynamic worker_threads pool |
| 129 | const pool = new DynamicThreadPool(Math.floor(availableParallelism() / 2), availableParallelism(), './yourWorker.js', { |
| 130 | errorHandler: e => console.error(e), |
| 131 | onlineHandler: () => console.info('worker is online') |
| 132 | }) |
| 133 | |
| 134 | pool.emitter.on(PoolEvents.full, () => console.info('Pool is full')) |
| 135 | pool.emitter.on(PoolEvents.ready, () => console.info('Pool is ready')) |
| 136 | pool.emitter.on(PoolEvents.busy, () => console.info('Pool is busy')) |
| 137 | |
| 138 | // the execute method signature is the same for both implementations, |
| 139 | // so you can easy switch from one to another |
| 140 | pool |
| 141 | .execute({}) |
| 142 | .then(res => { |
| 143 | console.info(res) |
| 144 | }) |
| 145 | .catch(err => { |
| 146 | console.error(err) |
| 147 | }) |
| 148 | ``` |
| 149 | |
| 150 | You can do the same with the classes _ClusterWorker_, _FixedClusterPool_ and _DynamicClusterPool_. |
| 151 | |
| 152 | **See [examples](./examples/) folder for more details (in particular if you want to use a pool with [multiple worker functions](./examples/multiFunctionExample.js))**. |
| 153 | |
| 154 | Remember that workers can only send and receive structured-cloneable data. |
| 155 | |
| 156 | ## Node versions |
| 157 | |
| 158 | Node versions >= 16.14.x are supported. |
| 159 | |
| 160 | ## [API](https://poolifier.github.io/poolifier/) |
| 161 | |
| 162 | ### `pool = new FixedThreadPool/FixedClusterPool(numberOfThreads/numberOfWorkers, filePath, opts)` |
| 163 | |
| 164 | `numberOfThreads/numberOfWorkers` (mandatory) Number of workers for this pool |
| 165 | `filePath` (mandatory) Path to a file with a worker implementation |
| 166 | `opts` (optional) An object with the pool options properties described below |
| 167 | |
| 168 | ### `pool = new DynamicThreadPool/DynamicClusterPool(min, max, filePath, opts)` |
| 169 | |
| 170 | `min` (mandatory) Same as _FixedThreadPool_/_FixedClusterPool_ numberOfThreads/numberOfWorkers, this number of workers will be always active |
| 171 | `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). |
| 172 | `filePath` (mandatory) Path to a file with a worker implementation |
| 173 | `opts` (optional) An object with the pool options properties described below |
| 174 | |
| 175 | ### `pool.execute(data, name)` |
| 176 | |
| 177 | `data` (optional) An object that you want to pass to your worker implementation |
| 178 | `name` (optional) A string with the task function name that you want to execute on the worker. Default: `'default'` |
| 179 | |
| 180 | This method is available on both pool implementations and returns a promise with the task function execution response. |
| 181 | |
| 182 | ### `pool.destroy()` |
| 183 | |
| 184 | This method is available on both pool implementations and will call the terminate method on each worker. |
| 185 | |
| 186 | ### `PoolOptions` |
| 187 | |
| 188 | An object with these properties: |
| 189 | |
| 190 | - `messageHandler` (optional) - A function that will listen for message event on each worker |
| 191 | - `errorHandler` (optional) - A function that will listen for error event on each worker |
| 192 | - `onlineHandler` (optional) - A function that will listen for online event on each worker |
| 193 | - `exitHandler` (optional) - A function that will listen for exit event on each worker |
| 194 | - `workerChoiceStrategy` (optional) - The worker choice strategy to use in this pool: |
| 195 | |
| 196 | - `WorkerChoiceStrategies.ROUND_ROBIN`: Submit tasks to worker in a round robin fashion |
| 197 | - `WorkerChoiceStrategies.LEAST_USED`: Submit tasks to the worker with the minimum number of executed, executing and queued tasks |
| 198 | - `WorkerChoiceStrategies.LEAST_BUSY`: Submit tasks to the worker with the minimum tasks total execution and wait time |
| 199 | - `WorkerChoiceStrategies.LEAST_ELU`: Submit tasks to the worker with the minimum event loop utilization (ELU) (experimental) |
| 200 | - `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 |
| 201 | - `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) |
| 202 | - `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 |
| 203 | |
| 204 | `WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN`, `WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN` and `WorkerChoiceStrategies.FAIR_SHARE` strategies are targeted to heavy and long tasks. |
| 205 | Default: `WorkerChoiceStrategies.ROUND_ROBIN` |
| 206 | |
| 207 | - `workerChoiceStrategyOptions` (optional) - The worker choice strategy options object to use in this pool. |
| 208 | Properties: |
| 209 | |
| 210 | - `measurement` (optional) - The measurement to use in worker choice strategies: `runTime`, `waitTime` or `elu`. |
| 211 | - `runTime` (optional) - Use the tasks [median](./src/pools/selection-strategies/README.md#median) runtime instead of the tasks average runtime in worker choice strategies. |
| 212 | - `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. |
| 213 | - `elu` (optional) - Use the tasks [median](./src/pools/selection-strategies/README.md#median) ELU instead of the tasks average ELU in worker choice strategies. |
| 214 | - `weights` (optional) - The worker weights to use in weighted round robin worker choice strategies: `{ 0: 200, 1: 300, ..., n: 100 }`. |
| 215 | |
| 216 | Default: `{ runTime: { median: false }, waitTime: { median: false }, elu: { median: false } }` |
| 217 | |
| 218 | - `restartWorkerOnError` (optional) - Restart worker on uncaught error in this pool. |
| 219 | Default: `true` |
| 220 | - `enableEvents` (optional) - Events emission enablement in this pool. |
| 221 | Default: `true` |
| 222 | - `enableTasksQueue` (optional) - Tasks queue per worker enablement in this pool. |
| 223 | Default: `false` |
| 224 | |
| 225 | - `tasksQueueOptions` (optional) - The worker tasks queue options object to use in this pool. |
| 226 | Properties: |
| 227 | |
| 228 | - `concurrency` (optional) - The maximum number of tasks that can be executed concurrently on a worker. |
| 229 | |
| 230 | Default: `{ concurrency: 1 }` |
| 231 | |
| 232 | #### `ThreadPoolOptions extends PoolOptions` |
| 233 | |
| 234 | - `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. |
| 235 | |
| 236 | #### `ClusterPoolOptions extends PoolOptions` |
| 237 | |
| 238 | - `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. |
| 239 | |
| 240 | - `settings` (optional) - An object with the cluster settings. See [cluster](https://nodejs.org/api/cluster.html#cluster_cluster_settings) for more details. |
| 241 | |
| 242 | ### `class YourWorker extends ThreadWorker/ClusterWorker` |
| 243 | |
| 244 | `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 |
| 245 | `opts` (optional) An object with these properties: |
| 246 | |
| 247 | - `maxInactiveTime` (optional) - Maximum waiting time in milliseconds for tasks on newly created workers. After this time newly created workers will die. |
| 248 | The last active time of your worker will be updated when it terminates a task. |
| 249 | 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. |
| 250 | If `killBehavior` is set to `KillBehaviors.SOFT` your tasks have no timeout and your workers will not be terminated until your task is completed. |
| 251 | Default: `60000` |
| 252 | |
| 253 | - `killBehavior` (optional) - Dictates if your worker will be deleted in case a task is active on it. |
| 254 | **KillBehaviors.SOFT**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker **won't** be deleted. |
| 255 | **KillBehaviors.HARD**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker will be deleted. |
| 256 | This option only apply to the newly created workers. |
| 257 | Default: `KillBehaviors.SOFT` |
| 258 | |
| 259 | #### `YourWorker.hasTaskFunction(name)` |
| 260 | |
| 261 | `name` (mandatory) The task function name |
| 262 | |
| 263 | This method is available on both worker implementations and returns a boolean. |
| 264 | |
| 265 | #### `YourWorker.addTaskFunction(name, fn)` |
| 266 | |
| 267 | `name` (mandatory) The task function name |
| 268 | `fn` (mandatory) The task function |
| 269 | |
| 270 | This method is available on both worker implementations and returns a boolean. |
| 271 | |
| 272 | #### `YourWorker.removeTaskFunction(name)` |
| 273 | |
| 274 | `name` (mandatory) The task function name |
| 275 | |
| 276 | This method is available on both worker implementations and returns a boolean. |
| 277 | |
| 278 | #### `YourWorker.listTaskFunctions()` |
| 279 | |
| 280 | This method is available on both worker implementations and returns an array of the task function names. |
| 281 | |
| 282 | #### `YourWorker.setDefaultTaskFunction(name)` |
| 283 | |
| 284 | `name` (mandatory) The task function name |
| 285 | |
| 286 | This method is available on both worker implementations and returns a boolean. |
| 287 | |
| 288 | ## General guidance |
| 289 | |
| 290 | Performance is one of the main target of these worker pool implementations, poolifier team wants to have a strong focus on this. |
| 291 | Poolifier already has a [benchmarks](./benchmarks/) folder where you can find some comparisons. |
| 292 | |
| 293 | ### Internal Node.js thread pool |
| 294 | |
| 295 | 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. |
| 296 | 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). |
| 297 | |
| 298 | **If your task runs on libuv thread pool**, you can try to: |
| 299 | |
| 300 | - Tune the libuv thread pool size setting the [UV_THREADPOOL_SIZE](https://nodejs.org/api/cli.html#cli_uv_threadpool_size_size). |
| 301 | |
| 302 | and/or |
| 303 | |
| 304 | - 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**. |
| 305 | |
| 306 | ### Cluster vs Threads worker pools |
| 307 | |
| 308 | **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. |
| 309 | Thread pools are built on top of Node.js [worker_threads](https://nodejs.org/api/worker_threads.html) module. |
| 310 | |
| 311 | **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. |
| 312 | Consider that by default Node.js already has great performance for I/O tasks (asynchronous I/O). |
| 313 | Cluster pools are built on top of Node.js [cluster](https://nodejs.org/api/cluster.html) module. |
| 314 | |
| 315 | 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). |
| 316 | But in general, **always profile your application**. |
| 317 | |
| 318 | ### Fixed vs Dynamic pools |
| 319 | |
| 320 | To choose your pool consider first that with a _FixedThreadPool_/_FixedClusterPool_ or a _DynamicThreadPool_/_DynamicClusterPool_ your application memory footprint will increase. |
| 321 | By doing so, your application will be ready to execute in parallel more tasks, but during idle time your application will consume more memory. |
| 322 | One good choice from poolifier team point of view is to profile your application using a static or dynamic worker pool, and analyze your application metrics when you increase/decrease the number of workers. |
| 323 | For 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_. |
| 324 | But in general, **always profile your application**. |
| 325 | |
| 326 | ## Contribute |
| 327 | |
| 328 | Choose your task here [2.6.x](https://github.com/orgs/poolifier/projects/1), propose an idea, a fix, an improvement. |
| 329 | |
| 330 | See [CONTRIBUTING](CONTRIBUTING.md) guidelines. |
| 331 | |
| 332 | ## Team |
| 333 | |
| 334 | **Creator/Owner:** |
| 335 | |
| 336 | - [**Alessandro Pio Ardizio**](https://github.com/pioardi) |
| 337 | |
| 338 | **_Contributors_** |
| 339 | |
| 340 | - [**Shinigami92**](https://github.com/Shinigami92) |
| 341 | - [**Jérôme Benoit**](https://github.com/jerome-benoit) |
| 342 | |
| 343 | ## License |
| 344 | |
| 345 | [MIT](./LICENSE) |