| 1 | <div align="center"> |
| 2 | <img src="./images/logo.png" width="340px" height="266px"/> |
| 3 | </div> |
| 4 | |
| 5 | <div align="center"> |
| 6 | |
| 7 | # Node.js Worker_Threads and Cluster Worker Pool |
| 8 | |
| 9 | </div> |
| 10 | |
| 11 | <div align="center"> |
| 12 | |
| 13 | [![GitHub commit activity (master)](https://img.shields.io/github/commit-activity/m/poolifier/poolifier/master?color=brightgreen&logo=github)](https://github.com/poolifier/poolifier/graphs/commit-activity) |
| 14 | [![Weekly Downloads](https://badgen.net/npm/dw/poolifier?icon=npm)](https://www.npmjs.com/package/poolifier) |
| 15 | [![CI Workflow](https://github.com/poolifier/poolifier/actions/workflows/ci.yml/badge.svg)](https://github.com/poolifier/poolifier/actions/workflows/ci.yml) |
| 16 | [![Code Coverage](https://sonarcloud.io/api/project_badges/measure?project=poolifier_poolifier&metric=coverage)](https://sonarcloud.io/dashboard?id=poolifier_poolifier) |
| 17 | [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=poolifier_poolifier&metric=alert_status)](https://sonarcloud.io/dashboard?id=poolifier_poolifier) |
| 18 | [![Javascript Standard Style Guide](<https://badgen.net/static/code style/standard/green>)](https://standardjs.com) |
| 19 | [![Discord](https://badgen.net/discord/online-members/vXxZhyb3b6?icon=discord&label=discord&color=green)](https://discord.gg/vXxZhyb3b6) |
| 20 | [![Open Collective](https://opencollective.com/poolifier/tiers/badge.svg)](https://opencollective.com/poolifier) |
| 21 | [![PRs Welcome](https://badgen.net/static/PRs/welcome/green)](https://makeapullrequest.com) |
| 22 | [![No Dependencies](<https://badgen.net/static/dependencies/no dependencies/green>)](<https://badgen.net/static/dependencies/no dependencies/green>) |
| 23 | |
| 24 | </div> |
| 25 | |
| 26 | ## Why Poolifier? |
| 27 | |
| 28 | 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. |
| 29 | With poolifier you can improve your **performance** and resolve problems related to the event loop. |
| 30 | Moreover you can execute your tasks using an API designed to improve the **developer experience**. |
| 31 | Please consult our [general guidelines](#general-guidelines). |
| 32 | |
| 33 | - Easy to use :white_check_mark: |
| 34 | - Fixed and dynamic pool size :white_check_mark: |
| 35 | - Easy switch from a pool type to another :white_check_mark: |
| 36 | - Performance [benchmarks](./benchmarks/README.md) :white_check_mark: |
| 37 | - No runtime dependencies :white_check_mark: |
| 38 | - Proper integration with Node.js [async_hooks](https://nodejs.org/api/async_hooks.html) :white_check_mark: |
| 39 | - Support for CommonJS, ESM and TypeScript :white_check_mark: |
| 40 | - 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: |
| 41 | - Support for multiple task functions :white_check_mark: |
| 42 | - Support for task functions [CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) operations at runtime :white_check_mark: |
| 43 | - Support for sync and async task functions :white_check_mark: |
| 44 | - Tasks distribution strategies :white_check_mark: |
| 45 | - Lockless tasks queueing :white_check_mark: |
| 46 | - Queued tasks rescheduling: |
| 47 | - Task stealing on empty queue :white_check_mark: |
| 48 | - Tasks stealing under back pressure :white_check_mark: |
| 49 | - Tasks redistribution on worker error :white_check_mark: |
| 50 | - General guidelines on pool choice :white_check_mark: |
| 51 | - Error handling out of the box :white_check_mark: |
| 52 | - Widely tested :white_check_mark: |
| 53 | - Active community :white_check_mark: |
| 54 | - Code quality [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=poolifier_poolifier&metric=bugs)](https://sonarcloud.io/dashboard?id=poolifier_poolifier) |
| 55 | [![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=poolifier_poolifier&metric=code_smells)](https://sonarcloud.io/dashboard?id=poolifier_poolifier) |
| 56 | [![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=poolifier_poolifier&metric=duplicated_lines_density)](https://sonarcloud.io/dashboard?id=poolifier_poolifier) |
| 57 | [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=poolifier_poolifier&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=poolifier_poolifier) |
| 58 | [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=poolifier_poolifier&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=poolifier_poolifier) |
| 59 | [![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=poolifier_poolifier&metric=sqale_index)](https://sonarcloud.io/dashboard?id=poolifier_poolifier) |
| 60 | - Code security [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=poolifier_poolifier&metric=security_rating)](https://sonarcloud.io/dashboard?id=poolifier_poolifier) [![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=poolifier_poolifier&metric=vulnerabilities)](https://sonarcloud.io/dashboard?id=poolifier_poolifier) |
| 61 | |
| 62 | ## Table of contents |
| 63 | |
| 64 | - [Overview](#overview) |
| 65 | - [Installation](#installation) |
| 66 | - [Usage](#usage) |
| 67 | - [Node.js versions](#nodejs-versions) |
| 68 | - [API](#api) |
| 69 | - [General guidelines](#general-guidelines) |
| 70 | - [Worker choice strategies](#worker-choice-strategies) |
| 71 | - [Contribute](#contribute) |
| 72 | - [Team](#team) |
| 73 | - [License](#license) |
| 74 | |
| 75 | ## Overview |
| 76 | |
| 77 | 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. |
| 78 | The first implementation is a fixed worker pool, with a defined number of workers that are started at creation time and will be reused. |
| 79 | 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 newly created workers will be stopped after a configurable period of inactivity. |
| 80 | You have to implement your worker by extending the _ThreadWorker_ or _ClusterWorker_ class. |
| 81 | |
| 82 | ## Installation |
| 83 | |
| 84 | ```shell |
| 85 | npm install poolifier --save |
| 86 | ``` |
| 87 | |
| 88 | ## Usage |
| 89 | |
| 90 | 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_: |
| 91 | |
| 92 | ```js |
| 93 | 'use strict' |
| 94 | const { ThreadWorker } = require('poolifier') |
| 95 | |
| 96 | function yourFunction(data) { |
| 97 | // this will be executed in the worker thread, |
| 98 | // the data will be received by using the execute method |
| 99 | return { ok: 1 } |
| 100 | } |
| 101 | |
| 102 | module.exports = new ThreadWorker(yourFunction, { |
| 103 | maxInactiveTime: 60000 |
| 104 | }) |
| 105 | ``` |
| 106 | |
| 107 | Instantiate your pool based on your needs : |
| 108 | |
| 109 | ```js |
| 110 | 'use strict' |
| 111 | const { DynamicThreadPool, FixedThreadPool, PoolEvents, availableParallelism } = require('poolifier') |
| 112 | |
| 113 | // a fixed worker_threads pool |
| 114 | const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', { |
| 115 | errorHandler: (e) => console.error(e), |
| 116 | onlineHandler: () => console.info('worker is online') |
| 117 | }) |
| 118 | |
| 119 | pool.emitter?.on(PoolEvents.ready, () => console.info('Pool is ready')) |
| 120 | pool.emitter?.on(PoolEvents.busy, () => console.info('Pool is busy')) |
| 121 | |
| 122 | // or a dynamic worker_threads pool |
| 123 | const pool = new DynamicThreadPool(Math.floor(availableParallelism() / 2), availableParallelism(), './yourWorker.js', { |
| 124 | errorHandler: (e) => console.error(e), |
| 125 | onlineHandler: () => console.info('worker is online') |
| 126 | }) |
| 127 | |
| 128 | pool.emitter?.on(PoolEvents.full, () => console.info('Pool is full')) |
| 129 | pool.emitter?.on(PoolEvents.ready, () => console.info('Pool is ready')) |
| 130 | pool.emitter?.on(PoolEvents.busy, () => console.info('Pool is busy')) |
| 131 | |
| 132 | // the execute method signature is the same for both implementations, |
| 133 | // so you can easily switch from one to another |
| 134 | pool |
| 135 | .execute() |
| 136 | .then((res) => { |
| 137 | console.info(res) |
| 138 | }) |
| 139 | .catch((err) => { |
| 140 | console.error(err) |
| 141 | }) |
| 142 | ``` |
| 143 | |
| 144 | You can do the same with the classes _ClusterWorker_, _FixedClusterPool_ and _DynamicClusterPool_. |
| 145 | |
| 146 | **See [examples](./examples/) for more details**: |
| 147 | |
| 148 | - [Javascript](./examples/javascript/) |
| 149 | - [Typescript](./examples/typescript/) |
| 150 | - [HTTP client pool](./examples/typescript/http-client-pool/) |
| 151 | - [SMTP client pool](./examples/typescript/smtp-client-pool/) |
| 152 | - [HTTP server pool](./examples/typescript/http-server-pool/) |
| 153 | - [Express worker_threads pool](./examples/typescript/http-server-pool/express-worker_threads/) |
| 154 | - [Express cluster pool](./examples/typescript/http-server-pool/express-cluster/) |
| 155 | - [Express hybrid pool](./examples/typescript/http-server-pool/express-hybrid/) |
| 156 | - [Fastify worker_threads pool](./examples/typescript/http-server-pool/fastify-worker_threads/) |
| 157 | - [Fastify cluster pool](./examples/typescript/http-server-pool/fastify-cluster/) |
| 158 | - [Fastify hybrid pool](./examples/typescript/http-server-pool/fastify-hybrid/) |
| 159 | - [WebSocket server pool](./examples/typescript/websocket-server-pool/) |
| 160 | - [ws worker_threads pool](./examples/typescript/websocket-server-pool/ws-worker_threads/) |
| 161 | - [ws cluster pool](./examples/typescript/websocket-server-pool/ws-cluster/) |
| 162 | - [ws hybrid pool](./examples/typescript/websocket-server-pool/ws-hybrid/) |
| 163 | |
| 164 | Remember that workers can only send and receive structured-cloneable data. |
| 165 | |
| 166 | ## Node.js versions |
| 167 | |
| 168 | Node.js versions >= 18.x.x are supported. |
| 169 | |
| 170 | ## [API](./docs/api.md) |
| 171 | |
| 172 | ## [General guidelines](./docs/general-guidelines.md) |
| 173 | |
| 174 | ## [Worker choice strategies](./docs/worker-choice-strategies.md) |
| 175 | |
| 176 | ## Contribute |
| 177 | |
| 178 | Choose your task [here](https://github.com/orgs/poolifier/projects/1), propose an idea, a fix, an improvement. |
| 179 | |
| 180 | See [CONTRIBUTING](./CONTRIBUTING.md) guidelines. |
| 181 | |
| 182 | ## Team |
| 183 | |
| 184 | **Creator/Owner:** |
| 185 | |
| 186 | - [**Alessandro Pio Ardizio**](https://github.com/pioardi) |
| 187 | |
| 188 | **Maintainers:** |
| 189 | |
| 190 | - [**Jérôme Benoit**](https://github.com/jerome-benoit) |
| 191 | |
| 192 | **Contributors:** |
| 193 | |
| 194 | - [**Shinigami92**](https://github.com/Shinigami92) |
| 195 | |
| 196 | ## License |
| 197 | |
| 198 | [MIT](./LICENSE) |