X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fthread%2Ffixed.ts;h=32107e48635314e25269cc37e2c0ad7fdd3fcd75;hb=7c7bb28936c5de8829dba0794760498077d3ea90;hp=ac629e1fb0ff7f363be0a3fe5107f2b02748c8d8;hpb=6677a3d36e9e7241c54db7cd69daa40f52fcbcb3;p=poolifier.git diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index ac629e1f..32107e48 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -1,18 +1,16 @@ import { + type MessageChannel, + type MessagePort, SHARE_ENV, + type TransferListItem, Worker, type WorkerOptions, isMainThread } from 'node:worker_threads' import type { MessageValue } from '../../utility-types' import { AbstractPool } from '../abstract-pool' -import { - type PoolOptions, - type PoolType, - PoolTypes, - type WorkerType, - WorkerTypes -} from '../pool' +import { type PoolOptions, type PoolType, PoolTypes } from '../pool' +import { type WorkerType, WorkerTypes } from '../worker' /** * Options for a poolifier thread pool. @@ -59,14 +57,57 @@ export class FixedThreadPool< } /** @inheritDoc */ - protected async destroyWorker (worker: Worker): 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() + }) + }) + await this.sendKillMessageToWorker(workerNodeKey, worker.threadId) + workerNode.closeChannel() await worker.terminate() + await waitWorkerExit + } + + /** @inheritDoc */ + protected sendToWorker ( + workerNodeKey: number, + message: MessageValue, + transferList?: TransferListItem[] + ): void { + ( + this.workerNodes[workerNodeKey].messageChannel as MessageChannel + ).port1.postMessage(message, transferList) + } + + /** @inheritDoc */ + protected sendStartupMessageToWorker (workerNodeKey: number): void { + const worker = this.workerNodes[workerNodeKey].worker + const port2: MessagePort = ( + this.workerNodes[workerNodeKey].messageChannel as MessageChannel + ).port2 + worker.postMessage( + { + ready: false, + workerId: worker.threadId, + port: port2 + }, + [port2] + ) } /** @inheritDoc */ - protected sendToWorker (worker: Worker, message: MessageValue): void { - worker.postMessage(message) + protected registerWorkerMessageListener( + workerNodeKey: number, + listener: (message: MessageValue) => void + ): void { + ( + this.workerNodes[workerNodeKey].messageChannel as MessageChannel + ).port1.on('message', listener) } /** @inheritDoc */ @@ -87,16 +128,6 @@ export class FixedThreadPool< return WorkerTypes.thread } - /** @inheritDoc */ - protected get minSize (): number { - return this.numberOfWorkers - } - - /** @inheritDoc */ - protected get maxSize (): number { - return this.numberOfWorkers - } - /** @inheritDoc */ protected get busy (): boolean { return this.internalBusy()