X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fpools%2Fthread%2Ffixed.ts;h=8dd4e7e33e422b4467a76931b201c45c0ad80712;hb=7884d1837ee55026fe5204a56f4ebeca17e7e7dd;hp=36eddace5ec149c41b8daafbd4ada0b91babb32d;hpb=325f50bc1777ea44abc9736ce9d780ec0c8f90e2;p=poolifier.git diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index 36eddace..8dd4e7e3 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -1,150 +1,145 @@ -import { isMainThread, MessageChannel, SHARE_ENV, Worker } from 'worker_threads' -import type { Draft, MessageValue } from '../../utility-types' +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 } from '../pool' +import { type WorkerType, WorkerTypes } from '../worker' -export type WorkerWithMessageChannel = Worker & Draft - -export interface FixedThreadPoolOptions { - /** - * A function that will listen for error event on each worker thread. - */ - errorHandler?: (this: Worker, e: Error) => void - /** - * A function that will listen for online event on each worker thread. - */ - onlineHandler?: (this: Worker) => void - /** - * A function that will listen for exit event on each worker thread. - */ - exitHandler?: (this: Worker, code: number) => void +/** + * Options for a poolifier thread pool. + */ +export interface ThreadPoolOptions extends PoolOptions { /** - * This is just to avoid not useful warnings message, is used to set `maxListeners` on event emitters (workers are event emitters). + * Worker options. * - * @default 1000 + * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options */ - maxTasks?: number + workerOptions?: WorkerOptions } /** - * A thread pool with a static number of threads, is possible to execute tasks in sync or async mode as you prefer. - * - * This pool will select the worker thread in a round robin fashion. + * A thread pool with a fixed number of threads. * + * @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 */ -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export class FixedThreadPool { - public readonly workers: WorkerWithMessageChannel[] = [] - public nextWorker: number = 0 - - // threadId as key and an integer value - public readonly tasks: Map = new Map< - WorkerWithMessageChannel, - number - >() - - protected id: number = 0 - +export class FixedThreadPool< + Data = unknown, + Response = unknown +> extends AbstractPool { /** - * @param numThreads Num of threads for this worker pool. - * @param filePath A file path with implementation of `ThreadWorker` class, relative path is fine. - * @param opts An object with possible options for example `errorHandler`, `onlineHandler`. Default: `{ maxTasks: 1000 }` + * Constructs a new poolifier fixed thread pool. + * + * @param numberOfThreads - Number of threads for this pool. + * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute. + * @param opts - Options for this fixed thread pool. */ public constructor ( - public readonly numThreads: number, - public readonly filePath: string, - public readonly opts: FixedThreadPoolOptions = { maxTasks: 1000 } + numberOfThreads: number, + filePath: string, + protected readonly opts: ThreadPoolOptions = {} ) { - if (!isMainThread) { - throw new Error('Cannot start a thread pool from a worker thread !!!') - } - // TODO christopher 2021-02-07: Improve this check e.g. with a pattern or blank check - if (!this.filePath) { - throw new Error('Please specify a file with a worker implementation') - } + super(numberOfThreads, filePath, opts) + } - for (let i = 1; i <= this.numThreads; i++) { - this.newWorker() - } + /** @inheritDoc */ + protected isMain (): boolean { + return isMainThread } - public async destroy (): Promise { - for (const worker of this.workers) { - await worker.terminate() - } + /** @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, worker.threadId) + workerNode.closeChannel() + await worker.terminate() + await waitWorkerExit } - /** - * Execute the task specified into the constructor with the data parameter. - * - * @param data The input for the task specified. - * @returns Promise that is resolved when the task is done. - */ - public execute (data: Data): Promise { - // configure worker to handle message with the specified task - const worker = this.chooseWorker() - const previousWorkerIndex = this.tasks.get(worker) - if (previousWorkerIndex !== undefined) { - this.tasks.set(worker, previousWorkerIndex + 1) - } else { - throw Error('Worker could not be found in tasks map') - } - const id = ++this.id - const res = this.internalExecute(worker, id) - worker.postMessage({ data: data || {}, id: id }) - return res + /** @inheritDoc */ + protected sendToWorker ( + workerNodeKey: number, + message: MessageValue, + transferList?: TransferListItem[] + ): void { + ( + this.workerNodes[workerNodeKey].messageChannel as MessageChannel + ).port1.postMessage(message, transferList) } - protected internalExecute ( - worker: WorkerWithMessageChannel, - id: number - ): Promise { - return new Promise((resolve, reject) => { - const listener: (message: MessageValue) => void = message => { - if (message.id === id) { - worker.port2?.removeListener('message', listener) - const previousWorkerIndex = this.tasks.get(worker) - if (previousWorkerIndex !== undefined) { - this.tasks.set(worker, previousWorkerIndex + 1) - } else { - throw Error('Worker could not be found in tasks map') - } - if (message.error) reject(message.error) - else resolve(message.data as Response) - } - } - worker.port2?.on('message', listener) - }) + /** @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] + ) } - protected chooseWorker (): WorkerWithMessageChannel { - if (this.workers.length - 1 === this.nextWorker) { - this.nextWorker = 0 - return this.workers[this.nextWorker] - } else { - this.nextWorker++ - return this.workers[this.nextWorker] - } + /** @inheritDoc */ + protected registerWorkerMessageListener( + workerNodeKey: number, + listener: (message: MessageValue) => void + ): void { + ( + this.workerNodes[workerNodeKey].messageChannel as MessageChannel + ).port1.on('message', listener) } - protected newWorker (): WorkerWithMessageChannel { - const worker: WorkerWithMessageChannel = new Worker(this.filePath, { - env: SHARE_ENV + /** @inheritDoc */ + protected createWorker (): Worker { + return new Worker(this.filePath, { + env: SHARE_ENV, + ...this.opts.workerOptions }) - worker.on('error', this.opts.errorHandler ?? (() => {})) - worker.on('online', this.opts.onlineHandler ?? (() => {})) - // TODO handle properly when a thread exit - worker.on('exit', this.opts.exitHandler ?? (() => {})) - this.workers.push(worker) - const { port1, port2 } = new MessageChannel() - worker.postMessage({ parent: port1 }, [port1]) - worker.port1 = port1 - worker.port2 = port2 - // we will attach a listener for every task, - // when task is completed the listener will be removed but to avoid warnings we are increasing the max listeners size - worker.port2.setMaxListeners(this.opts.maxTasks ?? 1000) - // init tasks map - this.tasks.set(worker, 0) - return worker + } + + /** @inheritDoc */ + protected get type (): PoolType { + return PoolTypes.fixed + } + + /** @inheritDoc */ + protected get worker (): WorkerType { + 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() } }