X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=772a839b1c077d23ae53f73b7a1475daa4c8bddc;hb=2eee72204bc851f616ded11cb5381f96c6dc5cbf;hp=d2397676bb77235279b72748e7e449c97ed0ca13;hpb=f8b3d84c1c95773a1e18d2c025d89d73c4f2ab15;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index d2397676..772a839b 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,24 +1,27 @@ -import { MessageChannel } from 'node:worker_threads' import { EventEmitter } from 'node:events' -import { CircularArray } from '../circular-array' -import type { Task } from '../utility-types' -import { DEFAULT_TASK_NAME, getWorkerId, getWorkerType } from '../utils' -import { Deque } from '../deque' +import { MessageChannel } from 'node:worker_threads' + +import { CircularArray } from '../circular-array.js' +import { PriorityQueue } from '../priority-queue.js' +import type { Task } from '../utility-types.js' +import { DEFAULT_TASK_NAME } from '../utils.js' +import { + checkWorkerNodeArguments, + createWorker, + getWorkerId, + getWorkerType +} from './utils.js' import { - type ErrorHandler, - type ExitHandler, + type EventHandler, type IWorker, type IWorkerNode, - type MessageHandler, - type OnlineHandler, type StrategyData, type WorkerInfo, type WorkerNodeOptions, type WorkerType, WorkerTypes, type WorkerUsage -} from './worker' -import { checkWorkerNodeArguments, createWorker } from './utils' +} from './worker.js' /** * Worker node. @@ -41,8 +44,8 @@ export class WorkerNode public messageChannel?: MessageChannel /** @inheritdoc */ public tasksQueueBackPressureSize: number - private readonly tasksQueue: Deque> - private onBackPressureStarted: boolean + private readonly tasksQueue: PriorityQueue> + private setBackPressureFlag: boolean private readonly taskFunctionsUsage: Map /** @@ -64,9 +67,10 @@ export class WorkerNode if (this.info.type === WorkerTypes.thread) { this.messageChannel = new MessageChannel() } - this.tasksQueueBackPressureSize = opts.tasksQueueBackPressureSize - this.tasksQueue = new Deque>() - this.onBackPressureStarted = false + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.tasksQueueBackPressureSize = opts.tasksQueueBackPressureSize! + this.tasksQueue = new PriorityQueue>(opts.tasksQueueBucketSize) + this.setBackPressureFlag = false this.taskFunctionsUsage = new Map() } @@ -77,34 +81,39 @@ export class WorkerNode /** @inheritdoc */ public enqueueTask (task: Task): number { - const tasksQueueSize = this.tasksQueue.push(task) - if (this.hasBackPressure() && !this.onBackPressureStarted) { - this.onBackPressureStarted = true - this.emit('backPressure', { workerId: this.info.id as number }) - this.onBackPressureStarted = false + const tasksQueueSize = this.tasksQueue.enqueue(task, task.priority) + if ( + !this.setBackPressureFlag && + this.hasBackPressure() && + !this.info.backPressure + ) { + this.setBackPressureFlag = true + this.info.backPressure = true + this.emit('backPressure', { workerId: this.info.id }) } + this.setBackPressureFlag = false return tasksQueueSize } /** @inheritdoc */ - public unshiftTask (task: Task): number { - const tasksQueueSize = this.tasksQueue.unshift(task) - if (this.hasBackPressure() && !this.onBackPressureStarted) { - this.onBackPressureStarted = true - this.emit('backPressure', { workerId: this.info.id as number }) - this.onBackPressureStarted = false + public dequeueTask (bucket?: number): Task | undefined { + const task = this.tasksQueue.dequeue(bucket) + if ( + !this.setBackPressureFlag && + !this.hasBackPressure() && + this.info.backPressure + ) { + this.setBackPressureFlag = true + this.info.backPressure = false } - return tasksQueueSize - } - - /** @inheritdoc */ - public dequeueTask (): Task | undefined { - return this.tasksQueue.shift() + this.setBackPressureFlag = false + return task } /** @inheritdoc */ - public popTask (): Task | undefined { - return this.tasksQueue.pop() + public dequeueLastPrioritizedTask (): Task | undefined { + // Start from the last empty or partially filled bucket + return this.dequeueTask(this.tasksQueue.buckets + 1) } /** @inheritdoc */ @@ -117,12 +126,6 @@ export class WorkerNode return this.tasksQueue.size >= this.tasksQueueBackPressureSize } - /** @inheritdoc */ - public resetUsage (): void { - this.usage = this.initWorkerUsage() - this.taskFunctionsUsage.clear() - } - /** @inheritdoc */ public async terminate (): Promise { const waitWorkerExit = new Promise(resolve => { @@ -134,6 +137,7 @@ export class WorkerNode this.removeAllListeners() switch (this.info.type) { case WorkerTypes.thread: + this.worker.unref?.() await this.worker.terminate?.() break case WorkerTypes.cluster: @@ -149,11 +153,7 @@ export class WorkerNode /** @inheritdoc */ public registerWorkerEventHandler ( event: string, - handler: - | OnlineHandler - | MessageHandler - | ErrorHandler - | ExitHandler + handler: EventHandler ): void { this.worker.on(event, handler) } @@ -161,32 +161,28 @@ export class WorkerNode /** @inheritdoc */ public registerOnceWorkerEventHandler ( event: string, - handler: - | OnlineHandler - | MessageHandler - | ErrorHandler - | ExitHandler + handler: EventHandler ): void { this.worker.once(event, handler) } /** @inheritdoc */ public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined { - if (!Array.isArray(this.info.taskFunctionNames)) { + if (!Array.isArray(this.info.taskFunctionsProperties)) { throw new Error( - `Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined` + `Cannot get task function worker usage for task function name '${name}' when task function properties list is not yet defined` ) } if ( - Array.isArray(this.info.taskFunctionNames) && - this.info.taskFunctionNames.length < 3 + Array.isArray(this.info.taskFunctionsProperties) && + this.info.taskFunctionsProperties.length < 3 ) { throw new Error( - `Cannot get task function worker usage for task function name '${name}' when task function names list has less than 3 elements` + `Cannot get task function worker usage for task function name '${name}' when task function properties list has less than 3 elements` ) } if (name === DEFAULT_TASK_NAME) { - name = this.info.taskFunctionNames[1] + name = this.info.taskFunctionsProperties[1].name } if (!this.taskFunctionsUsage.has(name)) { this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name)) @@ -212,9 +208,11 @@ export class WorkerNode private initWorkerInfo (worker: Worker): WorkerInfo { return { id: getWorkerId(worker), - type: getWorkerType(worker) as WorkerType, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + type: getWorkerType(worker)!, dynamic: false, ready: false, + backPressure: false, stealing: false } } @@ -263,7 +261,8 @@ export class WorkerNode for (const task of this.tasksQueue) { if ( (task.name === DEFAULT_TASK_NAME && - name === (this.info.taskFunctionNames as string[])[1]) || + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + name === this.info.taskFunctionsProperties![1].name) || (task.name !== DEFAULT_TASK_NAME && name === task.name) ) { ++taskFunctionQueueSize