fix: add missing worker options to thread pool options
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 7 Jun 2023 17:49:16 +0000 (19:49 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 7 Jun 2023 17:49:16 +0000 (19:49 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
CHANGELOG.md
src/pools/thread/dynamic.ts
src/pools/thread/fixed.ts
tests/pools/thread/fixed.test.js

index cb62e716c66205533efeee5f60ddd4ac1b3a74ed..33593d1ca79c2df3f627fa90b629c0766961d9a8 100644 (file)
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Changed
 
 - Compute statistics at the worker level only if needed.
+- Add `worker-threads` options to thread pool options.
 
 ### Fixed
 
index 90889c9b2572728ba7145da8e3a5c04bfa3c3378..b6d41e9ef9f7ff92cecf89e068c6493bafe696e3 100644 (file)
@@ -1,5 +1,5 @@
-import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
-import { FixedThreadPool, type ThreadWorkerWithMessageChannel } from './fixed'
+import { type PoolType, PoolTypes } from '../pool'
+import { FixedThreadPool, type ThreadPoolOptions } from './fixed'
 
 /**
  * A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads.
@@ -28,7 +28,7 @@ export class DynamicThreadPool<
     min: number,
     protected readonly max: number,
     filePath: string,
-    opts: PoolOptions<ThreadWorkerWithMessageChannel> = {}
+    opts: ThreadPoolOptions = {}
   ) {
     super(min, filePath, opts)
   }
index f0a49056bd3ef9c937abc10e7fc1dadc1bc3f50f..a9d8f685fec3432db4fc6829dabcfd84f146babe 100644 (file)
@@ -2,6 +2,7 @@ import {
   MessageChannel,
   SHARE_ENV,
   Worker,
+  type WorkerOptions,
   isMainThread
 } from 'node:worker_threads'
 import type { Draft, MessageValue } from '../../utility-types'
@@ -14,6 +15,18 @@ import {
   WorkerTypes
 } from '../pool'
 
+/**
+ * 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 worker with message channels for communication between main thread and thread worker.
  */
@@ -45,7 +58,7 @@ export class FixedThreadPool<
   public constructor (
     numberOfThreads: number,
     filePath: string,
-    opts: PoolOptions<ThreadWorkerWithMessageChannel> = {}
+    protected readonly opts: ThreadPoolOptions = {}
   ) {
     super(numberOfThreads, filePath, opts)
   }
@@ -82,7 +95,8 @@ export class FixedThreadPool<
   /** @inheritDoc */
   protected createWorker (): ThreadWorkerWithMessageChannel {
     return new Worker(this.filePath, {
-      env: SHARE_ENV
+      env: SHARE_ENV,
+      ...this.opts.workerOptions
     })
   }
 
index 5dd97665f6177767e1502da8825b4200567e570e..4c90207cb9ffb61c7a764688a38536b22b5cb65e 100644 (file)
@@ -201,6 +201,24 @@ describe('Fixed thread pool test suite', () => {
     expect(numberOfExitEvents).toBe(numberOfThreads)
   })
 
+  it('Verify that thread pool options are checked', async () => {
+    const workerFilePath = './tests/worker-files/cluster/testWorker.js'
+    let pool1 = new FixedThreadPool(numberOfThreads, workerFilePath)
+    expect(pool1.opts.workerOptions).toBeUndefined()
+    await pool1.destroy()
+    pool1 = new FixedThreadPool(numberOfThreads, workerFilePath, {
+      workerOptions: {
+        env: { TEST: 'test' },
+        name: 'test'
+      }
+    })
+    expect(pool1.opts.workerOptions).toStrictEqual({
+      env: { TEST: 'test' },
+      name: 'test'
+    })
+    await pool1.destroy()
+  })
+
   it('Should work even without opts in input', async () => {
     const pool1 = new FixedThreadPool(
       numberOfThreads,