X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fthread%2Ffixed.ts;h=7977cd61abd5263023b855fe9f7f23ba5135de10;hb=6a3ecc500583fc68d5bbce89f8448ccd022ab900;hp=2ad94a7bf16083072bf1303745834c2b7c06506a;hpb=e4c07d066abc51e978a18f44a973a548f24fb7ad;p=poolifier.git diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index 2ad94a7b..7977cd61 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -1,28 +1,18 @@ import { - type MessageChannel, - type MessagePort, - SHARE_ENV, + isMainThread, type TransferListItem, - Worker, - type WorkerOptions, - isMainThread + type Worker } 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 options. - * - * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options - */ - workerOptions?: WorkerOptions -} +export type ThreadPoolOptions = PoolOptions /** * A thread pool with a fixed number of threads. @@ -46,9 +36,10 @@ export class FixedThreadPool< public constructor ( numberOfThreads: number, filePath: string, - protected readonly opts: ThreadPoolOptions = {} + opts: ThreadPoolOptions = {}, + maximumNumberOfThreads?: number ) { - super(numberOfThreads, filePath, opts) + super(numberOfThreads, filePath, opts, maximumNumberOfThreads) } /** @inheritDoc */ @@ -56,33 +47,17 @@ export class FixedThreadPool< return isMainThread } - /** @inheritDoc */ - protected async destroyWorkerNode (workerNodeKey: number): Promise { - this.flushTasksQueue(workerNodeKey) - // FIXME: wait for tasks to be finished - const workerNode = this.workerNodes[workerNodeKey] - const worker = workerNode.worker - const waitWorkerExit = new Promise(resolve => { - worker.on('exit', () => { - resolve() - }) - }) - await this.sendKillMessageToWorker(workerNodeKey) - workerNode.closeChannel() - await worker.terminate() - await waitWorkerExit - } - /** @inheritDoc */ protected sendToWorker ( workerNodeKey: number, message: MessageValue, - transferList?: TransferListItem[] + transferList?: readonly TransferListItem[] ): void { - ( - this.workerNodes[workerNodeKey].messageChannel as MessageChannel - ).port1.postMessage( - { ...message, workerId: this.workerNodes[workerNodeKey].info.id }, + this.workerNodes[workerNodeKey]?.messageChannel?.port1.postMessage( + { + ...message, + workerId: this.getWorkerInfo(workerNodeKey)?.id + } satisfies MessageValue, transferList ) } @@ -90,14 +65,14 @@ export class FixedThreadPool< /** @inheritDoc */ protected sendStartupMessageToWorker (workerNodeKey: number): void { const workerNode = this.workerNodes[workerNodeKey] - const port2: MessagePort = (workerNode.messageChannel as MessageChannel) - .port2 + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const port2 = workerNode.messageChannel!.port2 workerNode.worker.postMessage( { ready: false, - workerId: workerNode.info.id, + workerId: this.getWorkerInfo(workerNodeKey)?.id, port: port2 - }, + } satisfies MessageValue, [port2] ) } @@ -107,17 +82,42 @@ export class FixedThreadPool< workerNodeKey: number, listener: (message: MessageValue) => void ): void { - ( - this.workerNodes[workerNodeKey].messageChannel as MessageChannel - ).port1.on('message', listener) + this.workerNodes[workerNodeKey].messageChannel?.port1.on( + 'message', + listener + ) + } + + /** @inheritDoc */ + protected registerOnceWorkerMessageListener( + workerNodeKey: number, + listener: (message: MessageValue) => void + ): void { + this.workerNodes[workerNodeKey].messageChannel?.port1.once( + 'message', + listener + ) + } + + /** @inheritDoc */ + protected deregisterWorkerMessageListener( + workerNodeKey: number, + listener: (message: MessageValue) => void + ): void { + this.workerNodes[workerNodeKey].messageChannel?.port1.off( + 'message', + listener + ) + } + + /** @inheritDoc */ + protected shallCreateDynamicWorker (): boolean { + return false } /** @inheritDoc */ - protected createWorker (): Worker { - return new Worker(this.filePath, { - env: SHARE_ENV, - ...this.opts.workerOptions - }) + protected checkAndEmitDynamicWorkerCreationEvents (): void { + /* noop */ } /** @inheritDoc */