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