2 const FixedThreadPool
= require('./fixed')
3 const { randomWorker
} = require('./util')
4 const EventEmitter
= require('events')
5 class MyEmitter
extends EventEmitter
{}
8 * A thread pool with a min/max number of threads , is possible to execute tasks in sync or async mode as you prefer. <br>
9 * This thread pool will create new workers when the other ones are busy, until the max number of threads,
10 * when the max number of threads is reached, an event will be emitted , if you want to listen this event use the emitter method.
11 * @author Alessandro Pio Ardizio
14 class DynamicThreadPool
extends FixedThreadPool
{
17 * @param {Number} min Min number of threads that will be always active
18 * @param {Number} max Max number of threads that will be active
19 * @param {Object} an object with possible options for example maxConcurrency
21 constructor (min
, max
, filename
, opts
) {
22 super(min
, filename
, opts
)
24 this.emitter
= new MyEmitter()
28 * Return an event emitter that will send some messages, for example
29 * a message will be sent when max number of threads is reached and all threads are busy
30 * in this case it will emit a message
38 for (const entry
of this.tasks
) {
46 // a worker is free, use it
49 if (this.workers
.length
=== this.max
) {
50 this.emitter
.emit('FullPool')
51 return randomWorker(this.tasks
)
53 // all workers are busy create a new worker
54 const worker
= this._newWorker()
55 worker
.port2
.on('message', (message
) => {
57 worker
.postMessage({ kill
: 1 })
66 module
.exports
= DynamicThreadPool