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