Fast and small Node.js Worker_Threads and Cluster Worker Pool
npm install poolifier
















- Code security  
- Overview
- Installation
- Usage
- Node.js versions
- API
- General guidelines
- Worker choice strategies
- Contribute
- Team
- License
Poolifier contains two worker_threads/cluster worker pool implementations, you don't have to deal with worker_threads/cluster complexity.
The first implementation is a fixed worker pool, with a defined number of workers that are started at creation time and will be reused.
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.
You have to implement your worker by extending the _ThreadWorker_ or _ClusterWorker_ class.
``shell`
npm install poolifier --save
`shell`
npx jsr add @poolifier/poolifier
You can implement a poolifier worker_threads worker in a simple way by extending the class _ThreadWorker_:
`js
import { ThreadWorker } from 'poolifier'
function yourFunction(data) {
// this will be executed in the worker thread,
// the data will be received by using the execute method
return { ok: 1 }
}
export default new ThreadWorker(yourFunction, {
maxInactiveTime: 60000,
})
`
Instantiate your pool based on your needs :
`js
import { DynamicThreadPool, FixedThreadPool, PoolEvents, availableParallelism } from 'poolifier'
// a fixed worker_threads pool
const pool = new FixedThreadPool(availableParallelism(), './yourWorker.js', {
onlineHandler: () => console.info('worker is online'),
errorHandler: e => console.error(e),
})
pool.emitter?.on(PoolEvents.ready, () => console.info('Pool is ready'))
pool.emitter?.on(PoolEvents.busy, () => console.info('Pool is busy'))
// or a dynamic worker_threads pool
const pool = new DynamicThreadPool(Math.floor(availableParallelism() / 2), availableParallelism(), './yourWorker.js', {
onlineHandler: () => console.info('worker is online'),
errorHandler: e => console.error(e),
})
pool.emitter?.on(PoolEvents.full, () => console.info('Pool is full'))
pool.emitter?.on(PoolEvents.ready, () => console.info('Pool is ready'))
pool.emitter?.on(PoolEvents.busy, () => console.info('Pool is busy'))
// the execute method signature is the same for both implementations,
// so you can easily switch from one to another
try {
const res = await pool.execute()
console.info(res)
} catch (err) {
console.error(err)
}
``
You can do the same with the classes _ClusterWorker_, _FixedClusterPool_ and _DynamicClusterPool_.
See examples for more details:
- Javascript
- Typescript
- HTTP client pool
- SMTP client pool
- HTTP server pool
- Express worker_threads pool
- Express cluster pool
- Express hybrid pool
- Fastify worker_threads pool
- Fastify cluster pool
- Fastify hybrid pool
- WebSocket server pool
- ws worker_threads pool
- ws cluster pool
- ws hybrid pool
Remember that workers can only send and receive structured-cloneable data.
Node.js versions >= 20.x.x are supported.
Choose your task here, propose an idea, a fix, an improvement.
See CONTRIBUTING guidelines.
Creator/Owner:
Maintainers:
Contributors: