2 const FixedThreadPool
= require('./fixed')
5 * A thread pool with a min/max number of threads , is possible to execute tasks in sync or async mode as you prefer. <br>
6 * This thread pool will create new workers when the other ones are busy, until the max number of threads,
7 * when the max number of threads is reached, an exception will be thrown.
8 * This pool will select the worker thread in a round robin fashion. <br>
9 * @author Alessandro Pio Ardizio
12 class DynamicThreadPool
extends FixedThreadPool
{
15 * @param {Number} min Min number of threads that will be always active
16 * @param {Number} max Max number of threads that will be active
17 * @param {Object} an object with possible options for example maxConcurrency
19 constructor (min
, max
, filename
, opts
) {
20 super(min
, filename
, opts
)
26 for (const entry
of this.tasks
) {
34 // a worker is free, use it
37 if (this.workers
.length
=== this.max
) {
38 throw new Error('Max number of threads reached !!!')
40 // console.log('new thread is coming')
41 // all workers are busy create a new worker
42 const worker
= this._newWorker()
43 worker
.port2
.on('message', (message
) => {
45 worker
.postMessage({ kill
: 1 })
54 module
.exports
= DynamicThreadPool