import {
- type MessageChannel,
- type MessagePort,
- SHARE_ENV,
- Worker,
- type WorkerOptions,
+ type TransferListItem,
+ type Worker,
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'
+import type { MessageValue } from '../../utility-types.js'
+import { AbstractPool } from '../abstract-pool.js'
+import { type PoolOptions, type PoolType, PoolTypes } from '../pool.js'
+import { type WorkerType, WorkerTypes } from '../worker.js'
/**
* 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
-}
+export type ThreadPoolOptions = PoolOptions<Worker>
/**
* A thread pool with a fixed number of threads.
public constructor (
numberOfThreads: number,
filePath: string,
- protected readonly opts: ThreadPoolOptions = {}
+ opts: ThreadPoolOptions = {},
+ maximumNumberOfThreads?: number
) {
- super(numberOfThreads, filePath, opts)
+ super(numberOfThreads, filePath, opts, maximumNumberOfThreads)
}
/** @inheritDoc */
}
/** @inheritDoc */
- protected async destroyWorker (worker: Worker): Promise<void> {
- this.sendToWorker(worker, { kill: true, workerId: worker.threadId })
- this.workerNodes[this.getWorkerNodeKey(worker)].closeChannel()
- await worker.terminate()
- }
-
- /** @inheritDoc */
- protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
- (
- this.getWorkerInfoByWorker(worker).messageChannel as MessageChannel
- ).port1.postMessage(message)
+ protected sendToWorker (
+ workerNodeKey: number,
+ message: MessageValue<Data>,
+ transferList?: TransferListItem[]
+ ): void {
+ this.workerNodes[workerNodeKey].messageChannel?.port1.postMessage(
+ {
+ ...message,
+ workerId: this.getWorkerInfo(workerNodeKey)?.id
+ } satisfies MessageValue<Data>,
+ transferList
+ )
}
/** @inheritDoc */
- protected sendStartupMessageToWorker (worker: Worker): void {
- const port2: MessagePort = (
- this.getWorkerInfoByWorker(worker).messageChannel as MessageChannel
- ).port2
- worker.postMessage(
+ protected sendStartupMessageToWorker (workerNodeKey: number): void {
+ const workerNode = this.workerNodes[workerNodeKey]
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ const port2 = workerNode.messageChannel!.port2
+ workerNode.worker.postMessage(
{
ready: false,
- workerId: worker.threadId,
+ workerId: this.getWorkerInfo(workerNodeKey)?.id,
port: port2
- },
+ } satisfies MessageValue<Data>,
[port2]
)
}
/** @inheritDoc */
protected registerWorkerMessageListener<Message extends Data | Response>(
- worker: Worker,
+ workerNodeKey: number,
+ listener: (message: MessageValue<Message>) => void
+ ): void {
+ this.workerNodes[workerNodeKey].messageChannel?.port1.on(
+ 'message',
+ listener
+ )
+ }
+
+ /** @inheritDoc */
+ protected registerOnceWorkerMessageListener<Message extends Data | Response>(
+ workerNodeKey: number,
listener: (message: MessageValue<Message>) => void
): void {
- (
- this.getWorkerInfoByWorker(worker).messageChannel as MessageChannel
- ).port1.on('message', listener)
+ this.workerNodes[workerNodeKey].messageChannel?.port1.once(
+ 'message',
+ listener
+ )
}
/** @inheritDoc */
- protected createWorker (): Worker {
- return new Worker(this.filePath, {
- env: SHARE_ENV,
- ...this.opts.workerOptions
- })
+ protected deregisterWorkerMessageListener<Message extends Data | Response>(
+ workerNodeKey: number,
+ listener: (message: MessageValue<Message>) => void
+ ): void {
+ this.workerNodes[workerNodeKey].messageChannel?.port1.off(
+ 'message',
+ listener
+ )
}
/** @inheritDoc */
- protected get type (): PoolType {
- return PoolTypes.fixed
+ protected shallCreateDynamicWorker (): boolean {
+ return false
}
/** @inheritDoc */
- protected get worker (): WorkerType {
- return WorkerTypes.thread
+ protected checkAndEmitDynamicWorkerCreationEvents (): void {
+ /* noop */
}
/** @inheritDoc */
- protected get minSize (): number {
- return this.numberOfWorkers
+ protected get type (): PoolType {
+ return PoolTypes.fixed
}
/** @inheritDoc */
- protected get maxSize (): number {
- return this.numberOfWorkers
+ protected get worker (): WorkerType {
+ return WorkerTypes.thread
}
/** @inheritDoc */