### Changed
- Compute statistics at the worker level only if needed.
+- Add `worker-threads` options to thread pool options.
### Fixed
-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.
min: number,
protected readonly max: number,
filePath: string,
- opts: PoolOptions<ThreadWorkerWithMessageChannel> = {}
+ opts: ThreadPoolOptions = {}
) {
super(min, filePath, opts)
}
MessageChannel,
SHARE_ENV,
Worker,
+ type WorkerOptions,
isMainThread
} from 'node:worker_threads'
import type { Draft, MessageValue } from '../../utility-types'
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.
*/
public constructor (
numberOfThreads: number,
filePath: string,
- opts: PoolOptions<ThreadWorkerWithMessageChannel> = {}
+ protected readonly opts: ThreadPoolOptions = {}
) {
super(numberOfThreads, filePath, opts)
}
/** @inheritDoc */
protected createWorker (): ThreadWorkerWithMessageChannel {
return new Worker(this.filePath, {
- env: SHARE_ENV
+ env: SHARE_ENV,
+ ...this.opts.workerOptions
})
}
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,