feat: internal messaging strict worker id checking
[poolifier.git] / src / pools / thread / fixed.ts
index 6cf8b526baf51d479a73c82a1bff9444718d9ffa..c9145f571186a9d5783531236414aff7eadd463e 100644 (file)
@@ -1,82 +1,99 @@
-import { isMainThread, MessageChannel, SHARE_ENV, Worker } from 'worker_threads'
-import type { Draft, JSONValue, MessageValue } from '../../utility-types'
-import type { PoolOptions } from '../abstract-pool'
+import {
+  SHARE_ENV,
+  Worker,
+  type WorkerOptions,
+  isMainThread
+} from 'node:worker_threads'
+import type { MessageValue } from '../../utility-types'
 import { AbstractPool } from '../abstract-pool'
+import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
+import { type WorkerType, WorkerTypes } from '../worker'
 
-export type ThreadWorkerWithMessageChannel = Worker & Draft<MessageChannel>
+/**
+ * Options for a poolifier thread pool.
+ */
+export interface ThreadPoolOptions extends PoolOptions<Worker> {
+  /**
+   * Worker options.
+   *
+   * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
+   */
+  workerOptions?: WorkerOptions
+}
 
 /**
- * A thread pool with a static number of threads, is possible to execute tasks in sync or async mode as you prefer.
- *
- * This pool will select the worker thread in a round robin fashion.
+ * A thread pool with a fixed number of threads.
  *
+ * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
+ * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
  * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
  * @since 0.0.1
  */
 export class FixedThreadPool<
-  Data extends JSONValue = JSONValue,
-  Response extends JSONValue = JSONValue
-> extends AbstractPool<ThreadWorkerWithMessageChannel, Data, Response> {
+  Data = unknown,
+  Response = unknown
+> extends AbstractPool<Worker, Data, Response> {
   /**
-   * @param numThreads Num of threads for this worker pool.
-   * @param filePath 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 fixed thread pool.
+   *
+   * @param numberOfThreads - Number of threads for this pool.
+   * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
+   * @param opts - Options for this fixed thread pool.
    */
   public constructor (
-    numThreads: number,
+    numberOfThreads: number,
     filePath: string,
-    opts: PoolOptions<ThreadWorkerWithMessageChannel> = { maxTasks: 1000 }
+    protected readonly opts: ThreadPoolOptions = {}
   ) {
-    super(numThreads, filePath, opts)
+    super(numberOfThreads, filePath, opts)
   }
 
+  /** @inheritDoc */
   protected isMain (): boolean {
     return isMainThread
   }
 
-  protected async destroyWorker (
-    worker: ThreadWorkerWithMessageChannel
-  ): Promise<void> {
+  /** @inheritDoc */
+  protected async destroyWorker (worker: Worker): Promise<void> {
+    this.sendToWorker(worker, { kill: true, workerId: worker.threadId })
     await worker.terminate()
-    // FIXME: The tests are currently failing, so these must be changed first
   }
 
-  protected sendToWorker (
-    worker: ThreadWorkerWithMessageChannel,
-    message: MessageValue<Data>
-  ): void {
+  /** @inheritDoc */
+  protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
     worker.postMessage(message)
   }
 
-  protected registerWorkerMessageListener (
-    port: ThreadWorkerWithMessageChannel,
-    listener: (message: MessageValue<Response>) => void
-  ): void {
-    port.port2?.on('message', listener)
+  /** @inheritDoc */
+  protected createWorker (): Worker {
+    return new Worker(this.filePath, {
+      env: SHARE_ENV,
+      ...this.opts.workerOptions
+    })
+  }
+
+  /** @inheritDoc */
+  protected get type (): PoolType {
+    return PoolTypes.fixed
   }
 
-  protected unregisterWorkerMessageListener (
-    port: ThreadWorkerWithMessageChannel,
-    listener: (message: MessageValue<Response>) => void
-  ): void {
-    port.port2?.removeListener('message', listener)
+  /** @inheritDoc */
+  protected get worker (): WorkerType {
+    return WorkerTypes.thread
   }
 
-  protected newWorker (): ThreadWorkerWithMessageChannel {
-    return new Worker(this.filePath, {
-      env: SHARE_ENV
-    })
+  /** @inheritDoc */
+  protected get minSize (): number {
+    return this.numberOfWorkers
+  }
+
+  /** @inheritDoc */
+  protected get maxSize (): number {
+    return this.numberOfWorkers
   }
 
-  protected afterNewWorkerPushed (
-    worker: ThreadWorkerWithMessageChannel
-  ): void {
-    const { port1, port2 } = new MessageChannel()
-    worker.postMessage({ parent: port1 }, [port1])
-    worker.port1 = port1
-    worker.port2 = port2
-    // we will attach a listener for every task,
-    // when task is completed the listener will be removed but to avoid warnings we are increasing the max listeners size
-    worker.port2.setMaxListeners(this.opts.maxTasks ?? 1000)
+  /** @inheritDoc */
+  protected get busy (): boolean {
+    return this.internalBusy()
   }
 }