X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=lib%2Fdynamic.js;h=36db0c75673ed7a70d4fe5e6e634b2754c38529f;hb=ae2377d3db3c0d942a60770982ec0278cfe4cc3f;hp=fa8a3a521c96b0b01954dcf303a7264308e541b0;hpb=50811da2e375e78179b56fe78fd7674ad2f360fd;p=poolifier.git diff --git a/lib/dynamic.js b/lib/dynamic.js index fa8a3a52..36db0c75 100644 --- a/lib/dynamic.js +++ b/lib/dynamic.js @@ -1,11 +1,12 @@ 'use strict' const FixedThreadPool = require('./fixed') +const EventEmitter = require('events') +class MyEmitter extends EventEmitter {} /** * A thread pool with a min/max number of threads , is possible to execute tasks in sync or async mode as you prefer.
* This thread pool will create new workers when the other ones are busy, until the max number of threads, - * when the max number of threads is reached, an exception will be thrown. - * This pool will select the worker thread in a round robin fashion.
+ * when the max number of threads is reached, an event will be emitted , if you want to listen this event use the emitter method. * @author Alessandro Pio Ardizio * @since 0.0.1 */ @@ -14,11 +15,11 @@ class DynamicThreadPool extends FixedThreadPool { * * @param {Number} min Min number of threads that will be always active * @param {Number} max Max number of threads that will be active - * @param {Object} an object with possible options for example maxConcurrency */ constructor (min, max, filename, opts) { super(min, filename, opts) this.max = max + this.emitter = new MyEmitter() } _chooseWorker () { @@ -35,7 +36,8 @@ class DynamicThreadPool extends FixedThreadPool { return worker } else { if (this.workers.length === this.max) { - throw new Error('Max number of threads reached !!!') + this.emitter.emit('FullPool') + return super._chooseWorker() } // all workers are busy create a new worker const worker = this._newWorker()