X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fthread%2Ffixed.ts;h=7199baf8fdf5ad52bc43bcfdfe107de84719589a;hb=c2301b8e5ab9a84e16dd037c6f7e3e85030bb266;hp=9939a04b9e497f5e78a84396493db7819f837508;hpb=6b27d40762317ec8502657663bdc839e358cda03;p=poolifier.git diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index 9939a04b..7199baf8 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -1,34 +1,40 @@ import { - MessageChannel, + type MessageChannel, + type MessagePort, SHARE_ENV, Worker, + type WorkerOptions, isMainThread } from 'node:worker_threads' -import type { Draft, MessageValue } from '../../utility-types' +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' /** - * A thread worker with message channels for communication between main thread and thread worker. + * Options for a poolifier thread pool. */ -export type ThreadWorkerWithMessageChannel = Worker & Draft +export interface ThreadPoolOptions extends PoolOptions { + /** + * Worker options. + * + * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options + */ + workerOptions?: WorkerOptions +} /** * A thread pool with a fixed number of threads. * - * It is possible to perform tasks in sync or asynchronous mode as you prefer. - * - * This pool selects the threads in a round robin fashion. - * - * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of execution response. This can only be serializable data. + * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. + * @typeParam Response - Type of execution response. This can only be structured-cloneable data. * @author [Alessandro Pio Ardizio](https://github.com/pioardi) * @since 0.0.1 */ export class FixedThreadPool< Data = unknown, Response = unknown -> extends AbstractPool { +> extends AbstractPool { /** * Constructs a new poolifier fixed thread pool. * @@ -39,7 +45,7 @@ export class FixedThreadPool< public constructor ( numberOfThreads: number, filePath: string, - opts: PoolOptions = {} + protected readonly opts: ThreadPoolOptions = {} ) { super(numberOfThreads, filePath, opts) } @@ -50,49 +56,74 @@ export class FixedThreadPool< } /** @inheritDoc */ - protected async destroyWorker ( - worker: ThreadWorkerWithMessageChannel - ): Promise { - this.sendToWorker(worker, { kill: 1 }) + 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() + }) + }) + this.sendToWorker(workerNodeKey, { kill: true, workerId: worker.threadId }) + workerNode.closeChannel() await worker.terminate() + await waitWorkerExit } /** @inheritDoc */ protected sendToWorker ( - worker: ThreadWorkerWithMessageChannel, + workerNodeKey: number, message: MessageValue ): void { - worker.postMessage(message) + ( + this.getWorkerInfo(workerNodeKey).messageChannel as MessageChannel + ).port1.postMessage(message) + } + + /** @inheritDoc */ + protected sendStartupMessageToWorker (workerNodeKey: number): void { + const worker = this.workerNodes[workerNodeKey].worker + const port2: MessagePort = ( + this.getWorkerInfo(workerNodeKey).messageChannel as MessageChannel + ).port2 + worker.postMessage( + { + ready: false, + workerId: worker.threadId, + port: port2 + }, + [port2] + ) } /** @inheritDoc */ protected registerWorkerMessageListener( - worker: ThreadWorkerWithMessageChannel, + workerNodeKey: number, listener: (message: MessageValue) => void ): void { - worker.port2?.on('message', listener) + ( + this.getWorkerInfo(workerNodeKey).messageChannel as MessageChannel + ).port1.on('message', listener) } /** @inheritDoc */ - protected createWorker (): ThreadWorkerWithMessageChannel { + protected createWorker (): Worker { return new Worker(this.filePath, { - env: SHARE_ENV + env: SHARE_ENV, + ...this.opts.workerOptions }) } /** @inheritDoc */ - protected afterWorkerSetup (worker: ThreadWorkerWithMessageChannel): void { - const { port1, port2 } = new MessageChannel() - worker.postMessage({ parent: port1 }, [port1]) - worker.port1 = port1 - worker.port2 = port2 - // Listen to worker messages. - this.registerWorkerMessageListener(worker, super.workerListener()) + protected get type (): PoolType { + return PoolTypes.fixed } /** @inheritDoc */ - public get type (): PoolType { - return PoolTypes.fixed + protected get worker (): WorkerType { + return WorkerTypes.thread } /** @inheritDoc */ @@ -105,11 +136,6 @@ export class FixedThreadPool< return this.numberOfWorkers } - /** @inheritDoc */ - protected get full (): boolean { - return this.workerNodes.length >= this.numberOfWorkers - } - /** @inheritDoc */ protected get busy (): boolean { return this.internalBusy()