2 const FixedThreadPool
= require('./fixed')
3 const EventEmitter
= require('events')
4 class MyEmitter
extends EventEmitter
{}
7 * A thread pool with a min/max number of threads , is possible to execute tasks in sync or async mode as you prefer. <br>
8 * This thread pool will create new workers when the other ones are busy, until the max number of threads,
9 * when the max number of threads is reached, an event will be emitted , if you want to listen this event use the emitter method.
10 * @author Alessandro Pio Ardizio
13 class DynamicThreadPool
extends FixedThreadPool
{
16 * @param {Number} min Min number of threads that will be always active
17 * @param {Number} max Max number of threads that will be active
19 constructor (min
, max
, filename
, opts
) {
20 super(min
, filename
, opts
)
22 this.emitter
= new MyEmitter()
27 for (const entry
of this.tasks
) {
35 // a worker is free, use it
38 if (this.workers
.length
=== this.max
) {
39 this.emitter
.emit('FullPool')
40 return super._chooseWorker()
42 // all workers are busy create a new worker
43 const worker
= this._newWorker()
44 worker
.port2
.on('message', (message
) => {
46 worker
.postMessage({ kill
: 1 })
55 module
.exports
= DynamicThreadPool