X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=59c4de7a388e81434c373f1835b144c1b3c88f57;hb=refs%2Ftags%2Fv3.0.10;hp=6eb2d646bdcaa86f93eb07cf341012463f295a9a;hpb=25926072f5397949802f6af5a2f7d49d6f15cacc;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 6eb2d646..59c4de7a 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,21 +1,14 @@ 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, - EMPTY_FUNCTION, - exponentialDelay, - getWorkerId, - getWorkerType, - sleep -} from '../utils' +import { DEFAULT_TASK_NAME, getWorkerId, getWorkerType } from '../utils' import { Deque } from '../deque' import { type IWorker, type IWorkerNode, type StrategyData, type WorkerInfo, - type WorkerNodeEventCallback, type WorkerType, WorkerTypes, type WorkerUsage @@ -29,7 +22,8 @@ import { checkWorkerNodeArguments } from './utils' * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. */ export class WorkerNode -implements IWorkerNode { + extends EventEmitter + implements IWorkerNode { /** @inheritdoc */ public readonly worker: Worker /** @inheritdoc */ @@ -42,13 +36,8 @@ implements IWorkerNode { public messageChannel?: MessageChannel /** @inheritdoc */ public tasksQueueBackPressureSize: number - /** @inheritdoc */ - public onBackPressure?: WorkerNodeEventCallback - /** @inheritdoc */ - public onEmptyQueue?: WorkerNodeEventCallback private readonly tasksQueue: Deque> private onBackPressureStarted: boolean - private onEmptyQueueCount: number private readonly taskFunctionsUsage: Map /** @@ -58,6 +47,7 @@ implements IWorkerNode { * @param tasksQueueBackPressureSize - The tasks queue back pressure size. */ constructor (worker: Worker, tasksQueueBackPressureSize: number) { + super() checkWorkerNodeArguments(worker, tasksQueueBackPressureSize) this.worker = worker this.info = this.initWorkerInfo(worker) @@ -68,7 +58,6 @@ implements IWorkerNode { this.tasksQueueBackPressureSize = tasksQueueBackPressureSize this.tasksQueue = new Deque>() this.onBackPressureStarted = false - this.onEmptyQueueCount = 0 this.taskFunctionsUsage = new Map() } @@ -80,13 +69,9 @@ implements IWorkerNode { /** @inheritdoc */ public enqueueTask (task: Task): number { const tasksQueueSize = this.tasksQueue.push(task) - if ( - this.onBackPressure != null && - this.hasBackPressure() && - !this.onBackPressureStarted - ) { + if (this.hasBackPressure() && !this.onBackPressureStarted) { this.onBackPressureStarted = true - this.onBackPressure(this.info.id as number) + this.emit('backPressure', { workerId: this.info.id as number }) this.onBackPressureStarted = false } return tasksQueueSize @@ -95,13 +80,9 @@ implements IWorkerNode { /** @inheritdoc */ public unshiftTask (task: Task): number { const tasksQueueSize = this.tasksQueue.unshift(task) - if ( - this.onBackPressure != null && - this.hasBackPressure() && - !this.onBackPressureStarted - ) { + if (this.hasBackPressure() && !this.onBackPressureStarted) { this.onBackPressureStarted = true - this.onBackPressure(this.info.id as number) + this.emit('backPressure', { workerId: this.info.id as number }) this.onBackPressureStarted = false } return tasksQueueSize @@ -109,28 +90,12 @@ implements IWorkerNode { /** @inheritdoc */ public dequeueTask (): Task | undefined { - const task = this.tasksQueue.shift() - if ( - this.onEmptyQueue != null && - this.tasksQueue.size === 0 && - this.onEmptyQueueCount === 0 - ) { - this.startOnEmptyQueue().catch(EMPTY_FUNCTION) - } - return task + return this.tasksQueue.shift() } /** @inheritdoc */ public popTask (): Task | undefined { - const task = this.tasksQueue.pop() - if ( - this.onEmptyQueue != null && - this.tasksQueue.size === 0 && - this.onEmptyQueueCount === 0 - ) { - this.startOnEmptyQueue().catch(EMPTY_FUNCTION) - } - return task + return this.tasksQueue.pop() } /** @inheritdoc */ @@ -152,10 +117,10 @@ implements IWorkerNode { /** @inheritdoc */ public closeChannel (): void { if (this.messageChannel != null) { - this.messageChannel?.port1.unref() - this.messageChannel?.port2.unref() - this.messageChannel?.port1.close() - this.messageChannel?.port2.close() + this.messageChannel.port1.unref() + this.messageChannel.port2.unref() + this.messageChannel.port1.close() + this.messageChannel.port2.close() delete this.messageChannel } } @@ -189,20 +154,6 @@ implements IWorkerNode { return this.taskFunctionsUsage.delete(name) } - private async startOnEmptyQueue (): Promise { - if ( - this.onEmptyQueueCount > 0 && - (this.usage.tasks.executing > 0 || this.tasksQueue.size > 0) - ) { - this.onEmptyQueueCount = 0 - return - } - ++this.onEmptyQueueCount - this.onEmptyQueue?.(this.info.id as number) - await sleep(exponentialDelay(this.onEmptyQueueCount)) - await this.startOnEmptyQueue() - } - private initWorkerInfo (worker: Worker): WorkerInfo { return { id: getWorkerId(worker), @@ -229,6 +180,7 @@ implements IWorkerNode { get maxQueued (): number { return getTasksQueueMaxSize() }, + sequentiallyStolen: 0, stolen: 0, failed: 0 }, @@ -270,6 +222,7 @@ implements IWorkerNode { get queued (): number { return getTaskFunctionQueueSize() }, + sequentiallyStolen: 0, stolen: 0, failed: 0 },