JSONValue can not be used by custom defined interfaces (#201)
[poolifier.git] / src / pools / thread / dynamic.ts
index d6a6e1ba870f9d1938e42f0dc0f70bf844243448..3e74a4ea4b7fe053445f3c204104b13f28e43e65 100644 (file)
@@ -1,68 +1,75 @@
-import type { MessageValue } from '../../utility-types'
+import { isKillBehavior, KillBehaviors } from '../../worker/worker-options'
 import type { PoolOptions } from '../abstract-pool'
 import type { ThreadWorkerWithMessageChannel } from './fixed'
 import { FixedThreadPool } from './fixed'
 
 /**
- * A thread pool with a min/max number of threads, is possible to execute tasks in sync or async mode as you prefer.
+ * A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads.
  *
- * 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 event will be emitted, if you want to listen this event use the emitter method.
+ * This thread pool creates new threads when the others are busy, up to the maximum number of threads.
+ * When the maximum number of threads is reached, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
+ *
+ * @template Data Type of data sent to the worker. This can only be serializable data.
+ * @template Response Type of response of execution. This can only be serializable data.
  *
  * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
  * @since 0.0.1
  */
 export class DynamicThreadPool<
-  // eslint-disable-next-line @typescript-eslint/no-explicit-any
-  Data = any,
-  // eslint-disable-next-line @typescript-eslint/no-explicit-any
-  Response = any
+  Data = unknown,
+  Response = unknown
 > extends FixedThreadPool<Data, Response> {
   /**
-   * @param min Min number of threads that will be always active
-   * @param max Max number of threads that will be active
-   * @param filename A file path with implementation of `ThreadWorker` class, relative path is fine.
-   * @param opts An object with possible options for example `errorHandler`, `onlineHandler`. Default: `{ maxTasks: 1000 }`
+   * Constructs a new poolifier dynamic thread pool.
+   *
+   * @param min Minimum number of threads which are always active.
+   * @param max Maximum number of threads that can be created by this pool.
+   * @param filePath Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
+   * @param opts Options for this dynamic thread pool. Default: `{ maxTasks: 1000 }`
    */
   public constructor (
     min: number,
     public readonly max: number,
-    filename: string,
+    filePath: string,
     opts: PoolOptions<ThreadWorkerWithMessageChannel> = { maxTasks: 1000 }
   ) {
-    super(min, filename, opts)
+    super(min, filePath, opts)
   }
 
+  /**
+   * Choose a thread for the next task.
+   *
+   * It will first check for and return an idle thread.
+   * If all threads are busy, then it will try to create a new one up to the `max` thread count.
+   * If the max thread count is reached, the emitter will emit a `FullPool` event and it will fall back to using a round robin algorithm to distribute the load.
+   *
+   * @returns Thread worker.
+   */
   protected chooseWorker (): ThreadWorkerWithMessageChannel {
-    let worker: ThreadWorkerWithMessageChannel | undefined
-    for (const entry of this.tasks) {
-      if (entry[1] === 0) {
-        worker = entry[0]
-        break
+    for (const [worker, numberOfTasks] of this.tasks) {
+      if (numberOfTasks === 0) {
+        // A worker is free, use it
+        return worker
       }
     }
 
-    if (worker) {
-      // a worker is free, use it
-      return worker
-    } else {
-      if (this.workers.length === this.max) {
-        this.emitter.emit('FullPool')
-        return super.chooseWorker()
-      }
-      // all workers are busy create a new worker
-      const worker = this.internalNewWorker()
-      worker.port2?.on('message', (message: MessageValue<Data>) => {
-        if (message.kill) {
-          this.sendToWorker(worker, { kill: 1 })
-          void this.destroyWorker(worker)
-          // clean workers from data structures
-          const workerIndex = this.workers.indexOf(worker)
-          this.workers.splice(workerIndex, 1)
-          this.tasks.delete(worker)
-        }
-      })
-      return worker
+    if (this.workers.length === this.max) {
+      this.emitter.emit('FullPool')
+      return super.chooseWorker()
     }
+
+    // All workers are busy, create a new worker
+    const workerCreated = this.createAndSetupWorker()
+    this.registerWorkerMessageListener<Data>(workerCreated, message => {
+      const tasksInProgress = this.tasks.get(workerCreated)
+      if (
+        isKillBehavior(KillBehaviors.HARD, message.kill) ||
+        tasksInProgress === 0
+      ) {
+        // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
+        void this.destroyWorker(workerCreated)
+      }
+    })
+    return workerCreated
   }
 }