docs: generate documentation
[poolifier.git] / README.md
1 <div align="center">
2 <img src="./images/logo.png" width="340px" height="266px"/>
3 </div>
4
5 <h1 align="center">Node.js Worker_Threads and Cluster Worker Pool</h1>
6
7 <div 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://discord.gg/85VrP8m8">
21 <img alt="Discord" src="https://badgen.net/discord/online-members/85VrP8m8?icon=discord&label=discord&color=green"></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 </div>
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-guidelines).
36
37 - Easy to use :white_check_mark:
38 - Fixed and dynamic pool size :white_check_mark:
39 - Easy switch from a pool type to another :white_check_mark:
40 - Performance [benchmarks](./benchmarks/README.md) :white_check_mark:
41 - No runtime dependencies :white_check_mark:
42 - Proper integration with Node.js [async_hooks](https://nodejs.org/api/async_hooks.html) :white_check_mark:
43 - Support for 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 for multiple task functions :white_check_mark:
46 - Support for task functions CRUD operations at runtime :white_check_mark:
47 - Support for sync and async task functions :white_check_mark:
48 - Tasks distribution strategies :white_check_mark:
49 - Lockless tasks queueing :white_check_mark:
50 - Queued tasks rescheduling:
51 - Task stealing :white_check_mark:
52 - Tasks stealing under back pressure :white_check_mark:
53 - Tasks redistribution on worker error :white_check_mark:
54 - General guidelines on pool choice :white_check_mark:
55 - Error handling out of the box :white_check_mark:
56 - Widely tested :white_check_mark:
57 - Active community :white_check_mark:
58 - Code quality [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=bugs)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
59 [![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=code_smells)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
60 [![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=duplicated_lines_density)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
61 [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
62 [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
63 [![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=pioardi_poolifier&metric=sqale_index)](https://sonarcloud.io/dashboard?id=pioardi_poolifier)
64 - 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)
65
66 ## Table of contents
67
68 - [Overview](#overview)
69 - [Installation](#installation)
70 - [Usage](#usage)
71 - [Node.js versions](#nodejs-versions)
72 - [API](#api)
73 - [General guidelines](#general-guidelines)
74 - [Worker choice strategies](#worker-choice-strategies)
75 - [Contribute](#contribute)
76 - [Team](#team)
77 - [License](#license)
78
79 ## Overview
80
81 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.
82 The first implementation is a fixed worker pool, with a defined number of workers that are started at creation time and will be reused.
83 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.
84 You have to implement your worker by extending the _ThreadWorker_ or _ClusterWorker_ class.
85
86 ## Installation
87
88 ```shell
89 npm install poolifier --save
90 ```
91
92 ## Usage
93
94 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_:
95
96 ```js
97 'use strict'
98 const { ThreadWorker } = require('poolifier')
99
100 function yourFunction(data) {
101 // this will be executed in the worker thread,
102 // the data will be received by using the execute method
103 return { ok: 1 }
104 }
105
106 module.exports = new ThreadWorker(yourFunction, {
107 maxInactiveTime: 60000
108 })
109 ```
110
111 Instantiate your pool based on your needs :
112
113 ```js
114 'use strict'
115 const { DynamicThreadPool, FixedThreadPool, PoolEvents, availableParallelism } = require('poolifier')
116
117 // a fixed worker_threads pool
118 const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', {
119 errorHandler: (e) => console.error(e),
120 onlineHandler: () => console.info('worker is online')
121 })
122
123 pool.emitter.on(PoolEvents.ready, () => console.info('Pool is ready'))
124 pool.emitter.on(PoolEvents.busy, () => console.info('Pool is busy'))
125
126 // or a dynamic worker_threads pool
127 const pool = new DynamicThreadPool(Math.floor(availableParallelism() / 2), availableParallelism(), './yourWorker.js', {
128 errorHandler: (e) => console.error(e),
129 onlineHandler: () => console.info('worker is online')
130 })
131
132 pool.emitter.on(PoolEvents.full, () => console.info('Pool is full'))
133 pool.emitter.on(PoolEvents.ready, () => console.info('Pool is ready'))
134 pool.emitter.on(PoolEvents.busy, () => console.info('Pool is busy'))
135
136 // the execute method signature is the same for both implementations,
137 // so you can easily switch from one to another
138 pool
139 .execute()
140 .then((res) => {
141 console.info(res)
142 })
143 .catch((err) => {
144 console.error(err)
145 })
146 ```
147
148 You can do the same with the classes _ClusterWorker_, _FixedClusterPool_ and _DynamicClusterPool_.
149
150 **See [examples](./examples/) for more details**:
151
152 - [Javascript](./examples/javascript/)
153 - [Typescript](./examples/typescript/)
154 - [HTTP client pool](./examples/typescript/http-client-pool/)
155 - [SMTP client pool](./examples/typescript/smtp-client-pool/)
156 - [HTTP server pool](./examples/typescript/http-server-pool/)
157 - [Express worker_threads pool](./examples/typescript/http-server-pool/express-worker_threads/)
158 - [Express cluster pool](./examples/typescript/http-server-pool/express-cluster/)
159 - [Express hybrid pool](./examples/typescript/http-server-pool/express-hybrid/)
160 - [Fastify worker_threads pool](./examples/typescript/http-server-pool/fastify-worker_threads/)
161 - [Fastify cluster pool](./examples/typescript/http-server-pool/fastify-cluster/)
162 - [Fastify hybrid pool](./examples/typescript/http-server-pool/fastify-hybrid/)
163 - [WebSocket server pool](./examples/typescript/websocket-server-pool/)
164 - [ws worker_threads pool](./examples/typescript/websocket-server-pool/ws-worker_threads/)
165 - [ws cluster pool](./examples/typescript/websocket-server-pool/ws-cluster/)
166 - [ws hybrid pool](./examples/typescript/websocket-server-pool/ws-hybrid/)
167
168 Remember that workers can only send and receive structured-cloneable data.
169
170 ## Node.js versions
171
172 Node.js versions >= 16.14.x are supported.
173
174 ## [API](./docs/api.md)
175
176 ## [General guidelines](./docs/general-guidelines.md)
177
178 ## [Worker choice strategies](./docs/worker-choice-strategies.md)
179
180 ## Contribute
181
182 Choose your task [here](https://github.com/orgs/poolifier/projects/1), propose an idea, a fix, an improvement.
183
184 See [CONTRIBUTING](./CONTRIBUTING.md) guidelines.
185
186 ## Team
187
188 **Creator/Owner:**
189
190 - [**Alessandro Pio Ardizio**](https://github.com/pioardi)
191
192 **Maintainers:**
193
194 - [**Jérôme Benoit**](https://github.com/jerome-benoit)
195
196 **Contributors:**
197
198 - [**Shinigami92**](https://github.com/Shinigami92)
199
200 ## License
201
202 [MIT](./LICENSE)