From: Jérôme Benoit Date: Wed, 7 Jun 2023 17:49:16 +0000 (+0200) Subject: fix: add missing worker options to thread pool options X-Git-Tag: v2.5.4~7^2~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=90082c8c00fda45ab415c5c3f67a582e7b0d2ce1;p=poolifier.git fix: add missing worker options to thread pool options Signed-off-by: Jérôme Benoit --- diff --git a/CHANGELOG.md b/CHANGELOG.md index cb62e716..33593d1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/pools/thread/dynamic.ts b/src/pools/thread/dynamic.ts index 90889c9b..b6d41e9e 100644 --- a/src/pools/thread/dynamic.ts +++ b/src/pools/thread/dynamic.ts @@ -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 = {} + opts: ThreadPoolOptions = {} ) { super(min, filePath, opts) } diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index f0a49056..a9d8f685 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -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 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 = {} + 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 }) } diff --git a/tests/pools/thread/fixed.test.js b/tests/pools/thread/fixed.test.js index 5dd97665..4c90207c 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -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,