X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=inline;f=lib%2Fdynamic.js;h=cedb8c35b93c0c230f62024bbd9facd04c6e1b7a;hb=973cde2683aa90c540748e68ed4f707c8ca33e40;hp=53ff28fadf06c0fde0536edceb9358fef839510e;hpb=a32e02baa991ae01b5d677e3fd34821965daab1e;p=poolifier.git
diff --git a/lib/dynamic.js b/lib/dynamic.js
index 53ff28fa..cedb8c35 100644
--- a/lib/dynamic.js
+++ b/lib/dynamic.js
@@ -1,11 +1,13 @@
'use strict'
const FixedThreadPool = require('./fixed')
+const { randomWorker } = require('./util')
+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
*/
@@ -19,6 +21,16 @@ class DynamicThreadPool extends FixedThreadPool {
constructor (min, max, filename, opts) {
super(min, filename, opts)
this.max = max
+ this.emitter = new MyEmitter()
+ }
+
+ /**
+ * Return an event emitter that will send some messages, for example
+ * a message will be sent when max number of threads is reached and all threads are busy
+ * in this case it will emit a message
+ */
+ emitter () {
+ return this.emitter
}
_chooseWorker () {
@@ -35,9 +47,9 @@ 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 randomWorker(this.tasks)
}
- // console.log('new thread is coming')
// all workers are busy create a new worker
const worker = this._newWorker()
worker.port2.on('message', (message) => {