refactor: move message channel property from worker info to node
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 17 Aug 2023 22:16:27 +0000 (00:16 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 17 Aug 2023 22:16:27 +0000 (00:16 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/thread/fixed.ts
src/pools/worker-node.ts
src/pools/worker.ts
tests/pools/abstract/abstract-pool.test.js

index 13e4a11a50028364c055b6b60aca5cb03b87fcfd..8dd4e7e33e422b4467a76931b201c45c0ad80712 100644 (file)
@@ -80,7 +80,7 @@ export class FixedThreadPool<
     transferList?: TransferListItem[]
   ): void {
     (
-      this.getWorkerInfo(workerNodeKey).messageChannel as MessageChannel
+      this.workerNodes[workerNodeKey].messageChannel as MessageChannel
     ).port1.postMessage(message, transferList)
   }
 
@@ -88,7 +88,7 @@ export class FixedThreadPool<
   protected sendStartupMessageToWorker (workerNodeKey: number): void {
     const worker = this.workerNodes[workerNodeKey].worker
     const port2: MessagePort = (
-      this.getWorkerInfo(workerNodeKey).messageChannel as MessageChannel
+      this.workerNodes[workerNodeKey].messageChannel as MessageChannel
     ).port2
     worker.postMessage(
       {
@@ -106,7 +106,7 @@ export class FixedThreadPool<
     listener: (message: MessageValue<Message>) => void
   ): void {
     (
-      this.getWorkerInfo(workerNodeKey).messageChannel as MessageChannel
+      this.workerNodes[workerNodeKey].messageChannel as MessageChannel
     ).port1.on('message', listener)
   }
 
index 292b39dfa15352e9cf113919a2dc1485226faa73..a5482dcd29f6778b69c70bf322c2370be13918b3 100644 (file)
@@ -22,6 +22,7 @@ export class WorkerNode<Worker extends IWorker, Data = unknown>
 implements IWorkerNode<Worker, Data> {
   public readonly worker: Worker
   public readonly info: WorkerInfo
+  public messageChannel?: MessageChannel
   public usage: WorkerUsage
   private readonly tasksUsage: Map<string, WorkerUsage>
   private readonly tasksQueue: Queue<Task<Data>>
@@ -35,6 +36,9 @@ implements IWorkerNode<Worker, Data> {
   constructor (worker: Worker, workerType: WorkerType) {
     this.worker = worker
     this.info = this.initWorkerInfo(worker, workerType)
+    if (workerType === WorkerTypes.thread) {
+      this.messageChannel = new MessageChannel()
+    }
     this.usage = this.initWorkerUsage()
     this.tasksUsage = new Map<string, WorkerUsage>()
     this.tasksQueue = new Queue<Task<Data>>()
@@ -77,12 +81,12 @@ implements IWorkerNode<Worker, Data> {
 
   /** @inheritdoc */
   public closeChannel (): void {
-    if (this.info.messageChannel != null) {
-      this.info.messageChannel?.port1.unref()
-      this.info.messageChannel?.port2.unref()
-      this.info.messageChannel?.port1.close()
-      this.info.messageChannel?.port2.close()
-      delete this.info.messageChannel
+    if (this.messageChannel != null) {
+      this.messageChannel?.port1.unref()
+      this.messageChannel?.port2.unref()
+      this.messageChannel?.port1.close()
+      this.messageChannel?.port2.close()
+      delete this.messageChannel
     }
   }
 
@@ -111,10 +115,7 @@ implements IWorkerNode<Worker, Data> {
       id: this.getWorkerId(worker, workerType),
       type: workerType,
       dynamic: false,
-      ready: false,
-      ...(workerType === WorkerTypes.thread && {
-        messageChannel: new MessageChannel()
-      })
+      ready: false
     }
   }
 
index 1de3cf09b6932053605efd6cc4719bfbe8bcf14c..09253242f7d0af88204ba9989fb37a7fa12cc287 100644 (file)
@@ -141,10 +141,6 @@ export interface WorkerInfo {
    * Task function names.
    */
   taskFunctions?: string[]
-  /**
-   * Message channel.
-   */
-  messageChannel?: MessageChannel
 }
 
 /**
@@ -215,6 +211,10 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
    * Worker info.
    */
   readonly info: WorkerInfo
+  /**
+   * Message channel.
+   */
+  readonly messageChannel?: MessageChannel
   /**
    * Worker usage statistics.
    */
index 5af1591207feca5269881d640db718b351aa66f3..5c367aa2c0ebf52be17b51dc8f961d70cfabc564 100644 (file)
@@ -1,4 +1,3 @@
-const { MessageChannel } = require('worker_threads')
 const { expect } = require('expect')
 const {
   DynamicClusterPool,
@@ -588,8 +587,7 @@ describe('Abstract pool test suite', () => {
         id: expect.any(Number),
         type: WorkerTypes.thread,
         dynamic: false,
-        ready: true,
-        messageChannel: expect.any(MessageChannel)
+        ready: true
       })
     }
   })