Untangle pool abstract class from worker strategy selection (#234)
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 24 Feb 2021 16:47:05 +0000 (17:47 +0100)
committerGitHub <noreply@github.com>
Wed, 24 Feb 2021 16:47:05 +0000 (17:47 +0100)
Co-authored-by: Shinigami <chrissi92@hotmail.de>
README.md
benchmarks/versus-external-pools/threadjs.js
benchmarks/versus-external-pools/workers/threadjs/function-to-bench-worker.js
src/pools/abstract-pool.ts
src/pools/pool-internal.ts
src/pools/selection-strategies.ts

index ce026b148412eb6e445db1b6d6f53775af7bed2d..fe74335db7104991983bf100d06ff90001ae8d5f 100644 (file)
--- a/README.md
+++ b/README.md
@@ -203,7 +203,7 @@ We already have a bench folder where you can find some comparisons.
 Thread pools are built on top of Node.js [worker-threads](https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads) module.
 
 **Cluster pools** (FixedClusterPool and DynamicClusterPool) are suggested to run I/O intensive tasks, again you can still run CPU intensive tasks into cluster pools, but performance enhancement is expected to be minimal.  
-Cluster pools are built on top of Node.js [cluster](https://nodejs.org/api/cluster.html) module.  
+Cluster pools are built on top of Node.js [cluster](https://nodejs.org/api/cluster.html) module.
 
 **Remember** that some Node.js tasks are execute by Node.js into the libuv worker pool at process level as explained [here](https://nodejs.org/en/docs/guides/dont-block-the-event-loop/#what-code-runs-on-the-worker-pool).
 
index a70692bb6e3b3a39eb452063ce9cca716b5d8673..df5c087f547d1f2c922d661af0297f7019351392 100644 (file)
@@ -11,18 +11,19 @@ const data = {
 // Threads.js is not really a pool so we need to write few additional code
 const workers = []
 async function poolify () {
-  for (let i = 0; i < size ; i++ ){
-    const worker = await spawn(new Worker("./workers/threadjs/function-to-bench-worker.js"))
+  for (let i = 0; i < size; i++) {
+    const worker = await spawn(
+      new Worker('./workers/threadjs/function-to-bench-worker.js')
+    )
     workers.push(worker)
   }
 }
 
-
 async function run () {
   await poolify()
   const promises = []
   for (let i = 0; i < iterations; i++) {
-    const worker = workers[(i % size)]
+    const worker = workers[i % size]
     promises.push(worker.exposedFunction(data))
   }
   await Promise.all(promises)
index 81f2c58e91dfd7de3bd146f9b5095edcd0efbc5b..71e4838818664642bc88204af38d841859ad35e5 100644 (file)
@@ -1,5 +1,5 @@
 'use strict'
-const { expose } = require("threads/worker")
+const { expose } = require('threads/worker')
 const functionToBench = require('../../functions/function-to-bench')
 
 expose({
index 7024f0998797936be1fe8d43710e1e2f1c827633..0c4b59c92fef071bbddd487d1490ed5d2f8db38b 100644 (file)
@@ -2,6 +2,7 @@ import type {
   MessageValue,
   PromiseWorkerResponseWrapper
 } from '../utility-types'
+import { isKillBehavior, KillBehaviors } from '../worker/worker-options'
 import type { IPoolInternal } from './pool-internal'
 import { PoolEmitter } from './pool-internal'
 import type { WorkerChoiceStrategy } from './selection-strategies'
@@ -100,6 +101,15 @@ export abstract class AbstractPool<
   Data = unknown,
   Response = unknown
 > implements IPoolInternal<Worker, Data, Response> {
+  /** @inheritdoc */
+  public readonly workers: Worker[] = []
+
+  /** @inheritdoc */
+  public readonly tasks: Map<Worker, number> = new Map<Worker, number>()
+
+  /** @inheritdoc */
+  public readonly emitter: PoolEmitter
+
   /**
    * The promise map.
    *
@@ -113,15 +123,6 @@ export abstract class AbstractPool<
     PromiseWorkerResponseWrapper<Worker, Response>
   > = new Map<number, PromiseWorkerResponseWrapper<Worker, Response>>()
 
-  /** @inheritdoc */
-  public readonly workers: Worker[] = []
-
-  /** @inheritdoc */
-  public readonly tasks: Map<Worker, number> = new Map<Worker, number>()
-
-  /** @inheritdoc */
-  public readonly emitter: PoolEmitter
-
   /**
    * ID of the next message.
    */
@@ -164,6 +165,20 @@ export abstract class AbstractPool<
     this.emitter = new PoolEmitter()
     this.workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       this,
+      () => {
+        const workerCreated = this.createAndSetupWorker()
+        this.registerWorkerMessageListener(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
+      },
       opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
     )
   }
@@ -223,8 +238,12 @@ export abstract class AbstractPool<
     await Promise.all(this.workers.map(worker => this.destroyWorker(worker)))
   }
 
-  /** @inheritdoc */
-  public abstract destroyWorker (worker: Worker): void | Promise<void>
+  /**
+   * Shut down given worker.
+   *
+   * @param worker A worker within `workers`.
+   */
+  protected abstract destroyWorker (worker: Worker): void | Promise<void>
 
   /**
    * Setup hook that can be overridden by a Poolifier pool implementation
@@ -306,8 +325,13 @@ export abstract class AbstractPool<
     message: MessageValue<Data>
   ): void
 
-  /** @inheritdoc */
-  public abstract registerWorkerMessageListener<
+  /**
+   * Register a listener callback on a given worker.
+   *
+   * @param worker A worker.
+   * @param listener A message listener callback.
+   */
+  protected abstract registerWorkerMessageListener<
     Message extends Data | Response
   > (worker: Worker, listener: (message: MessageValue<Message>) => void): void
 
@@ -334,8 +358,12 @@ export abstract class AbstractPool<
    */
   protected abstract afterWorkerSetup (worker: Worker): void
 
-  /** @inheritdoc */
-  public createAndSetupWorker (): Worker {
+  /**
+   * Creates a new worker for this pool and sets it up completely.
+   *
+   * @returns New, completely set up worker.
+   */
+  protected createAndSetupWorker (): Worker {
     const worker: Worker = this.createWorker()
 
     worker.on('error', this.opts.errorHandler ?? EMPTY_FUNCTION)
@@ -359,7 +387,7 @@ export abstract class AbstractPool<
    * @returns The listener function to execute when a message is sent from a worker.
    */
   protected workerListener (): (message: MessageValue<Response>) => void {
-    const listener: (message: MessageValue<Response>) => void = message => {
+    return message => {
       if (message.id) {
         const value = this.promiseMap.get(message.id)
         if (value) {
@@ -370,6 +398,5 @@ export abstract class AbstractPool<
         }
       }
     }
-    return listener
   }
 }
index d641fcf9d9fec246ee26a92a67a78d348b3dacf3..6152e3a421a60bf65d64804e491c35a663c7d1c1 100644 (file)
@@ -1,5 +1,4 @@
 import EventEmitter from 'events'
-import type { MessageValue } from '../utility-types'
 import type { IWorker } from './abstract-pool'
 import type { IPool } from './pool'
 
@@ -53,29 +52,4 @@ export interface IPoolInternal<
    * Maximum number of workers that can be created by this pool.
    */
   readonly max?: number
-
-  /**
-   * Creates a new worker for this pool and sets it up completely.
-   *
-   * @returns New, completely set up worker.
-   */
-  createAndSetupWorker(): Worker
-
-  /**
-   * Shut down given worker.
-   *
-   * @param worker A worker within `workers`.
-   */
-  destroyWorker(worker: Worker): void | Promise<void>
-
-  /**
-   * Register a listener callback on a given worker.
-   *
-   * @param worker A worker.
-   * @param listener A message listener callback.
-   */
-  registerWorkerMessageListener<Message extends Data | Response>(
-    worker: Worker,
-    listener: (message: MessageValue<Message>) => void
-  ): void
 }
index f86442b0f5fc836fd9cacf77333d1bab69f1fae2..53b129c9a4f1dd6cf1883b2d3157026b56b6ee4a 100644 (file)
@@ -1,4 +1,3 @@
-import { isKillBehavior, KillBehaviors } from '../worker/worker-options'
 import type { IWorker } from './abstract-pool'
 import type { IPoolInternal } from './pool-internal'
 
@@ -121,10 +120,12 @@ class DynamicPoolWorkerChoiceStrategy<Worker extends IWorker, Data, Response>
    * Constructs a worker choice strategy for dynamical pools.
    *
    * @param pool The pool instance.
+   * @param createDynamicallyWorkerCallback The worker creation callback for dynamic pool.
    * @param workerChoiceStrategy The worker choice strategy when the pull is full.
    */
   public constructor (
     private readonly pool: IPoolInternal<Worker, Data, Response>,
+    private createDynamicallyWorkerCallback: () => Worker,
     workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
   ) {
     this.workerChoiceStrategy = SelectionStrategiesUtils.getWorkerChoiceStrategy(
@@ -136,7 +137,7 @@ class DynamicPoolWorkerChoiceStrategy<Worker extends IWorker, Data, Response>
   /** @inheritdoc */
   public choose (): Worker {
     const freeWorker = SelectionStrategiesUtils.findFreeWorkerBasedOnTasks(
-      this.pool
+      this.pool.tasks
     )
     if (freeWorker) {
       return freeWorker
@@ -148,18 +149,7 @@ class DynamicPoolWorkerChoiceStrategy<Worker extends IWorker, Data, Response>
     }
 
     // All workers are busy, create a new worker
-    const workerCreated = this.pool.createAndSetupWorker()
-    this.pool.registerWorkerMessageListener(workerCreated, message => {
-      const tasksInProgress = this.pool.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.pool.destroyWorker(workerCreated)
-      }
-    })
-    return workerCreated
+    return this.createDynamicallyWorkerCallback()
   }
 }
 
@@ -182,10 +172,12 @@ export class WorkerChoiceStrategyContext<
    * Worker choice strategy context constructor.
    *
    * @param pool The pool instance.
+   * @param createDynamicallyWorkerCallback The worker creation callback for dynamic pool.
    * @param workerChoiceStrategy The worker choice strategy.
    */
   public constructor (
     private readonly pool: IPoolInternal<Worker, Data, Response>,
+    private createDynamicallyWorkerCallback: () => Worker,
     workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
   ) {
     this.setWorkerChoiceStrategy(workerChoiceStrategy)
@@ -203,6 +195,7 @@ export class WorkerChoiceStrategyContext<
     if (this.pool.dynamic) {
       return new DynamicPoolWorkerChoiceStrategy(
         this.pool,
+        this.createDynamicallyWorkerCallback,
         workerChoiceStrategy
       )
     }
@@ -246,15 +239,13 @@ class SelectionStrategiesUtils {
    *
    * If no free worker was found, `null` will be returned.
    *
-   * @param pool The pool instance.
+   * @param workerTasksMap The pool worker tasks map.
    * @returns A free worker if there was one, otherwise `null`.
    */
-  public static findFreeWorkerBasedOnTasks<
-    Worker extends IWorker,
-    Data,
-    Response
-  > (pool: IPoolInternal<Worker, Data, Response>): Worker | null {
-    for (const [worker, numberOfTasks] of pool.tasks) {
+  public static findFreeWorkerBasedOnTasks<Worker extends IWorker> (
+    workerTasksMap: Map<Worker, number>
+  ): Worker | null {
+    for (const [worker, numberOfTasks] of workerTasksMap) {
       if (numberOfTasks === 0) {
         // A worker is free, use it
         return worker