X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fthread%2Ffixed.ts;h=ac629e1fb0ff7f363be0a3fe5107f2b02748c8d8;hb=369d0a5139123c2b62260d5793b36097479b23a2;hp=a9d8f685fec3432db4fc6829dabcfd84f146babe;hpb=e1e2947c42b2affcabcc1969257b032fba0ac800;p=poolifier.git diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index a9d8f685..ac629e1f 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -1,11 +1,10 @@ import { - MessageChannel, 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, @@ -27,27 +26,18 @@ export interface ThreadPoolOptions extends PoolOptions { workerOptions?: WorkerOptions } -/** - * A thread worker with message channels for communication between main thread and thread worker. - */ -export type ThreadWorkerWithMessageChannel = Worker & Draft - /** * 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. * @@ -69,47 +59,24 @@ export class FixedThreadPool< } /** @inheritDoc */ - protected async destroyWorker ( - worker: ThreadWorkerWithMessageChannel - ): Promise { + protected async destroyWorker (worker: Worker): Promise { this.sendToWorker(worker, { kill: 1 }) await worker.terminate() } /** @inheritDoc */ - protected sendToWorker ( - worker: ThreadWorkerWithMessageChannel, - message: MessageValue - ): void { + protected sendToWorker (worker: Worker, message: MessageValue): void { worker.postMessage(message) } /** @inheritDoc */ - protected registerWorkerMessageListener( - worker: ThreadWorkerWithMessageChannel, - listener: (message: MessageValue) => void - ): void { - worker.port2?.on('message', listener) - } - - /** @inheritDoc */ - protected createWorker (): ThreadWorkerWithMessageChannel { + protected createWorker (): Worker { return new Worker(this.filePath, { 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()) - } - /** @inheritDoc */ protected get type (): PoolType { return PoolTypes.fixed