X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=59c4de7a388e81434c373f1835b144c1b3c88f57;hb=fa548cda5120ac0708d82f37cd0ce1260d7f96c1;hp=78756560c0fbaa78fedb96aad7153a21d14e61ff;hpb=c172526cd82999efbace45402e0bd6d9ae173963;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 78756560..59c4de7a 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,25 +1,19 @@ 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 } from './worker' +import { checkWorkerNodeArguments } from './utils' /** * Worker node. @@ -28,7 +22,8 @@ import { * @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 */ @@ -41,12 +36,8 @@ implements IWorkerNode { public messageChannel?: MessageChannel /** @inheritdoc */ public tasksQueueBackPressureSize: number - /** @inheritdoc */ - public onBackPressure?: WorkerNodeEventCallback - /** @inheritdoc */ - public onEmptyQueue?: WorkerNodeEventCallback private readonly tasksQueue: Deque> - private onEmptyQueueCount: number + private onBackPressureStarted: boolean private readonly taskFunctionsUsage: Map /** @@ -56,7 +47,8 @@ implements IWorkerNode { * @param tasksQueueBackPressureSize - The tasks queue back pressure size. */ constructor (worker: Worker, tasksQueueBackPressureSize: number) { - this.checkWorkerNodeArguments(worker, tasksQueueBackPressureSize) + super() + checkWorkerNodeArguments(worker, tasksQueueBackPressureSize) this.worker = worker this.info = this.initWorkerInfo(worker) this.usage = this.initWorkerUsage() @@ -65,7 +57,7 @@ implements IWorkerNode { } this.tasksQueueBackPressureSize = tasksQueueBackPressureSize this.tasksQueue = new Deque>() - this.onEmptyQueueCount = 0 + this.onBackPressureStarted = false this.taskFunctionsUsage = new Map() } @@ -77,8 +69,10 @@ implements IWorkerNode { /** @inheritdoc */ public enqueueTask (task: Task): number { const tasksQueueSize = this.tasksQueue.push(task) - if (this.onBackPressure != null && this.hasBackPressure()) { - this.onBackPressure(this.info.id as number) + if (this.hasBackPressure() && !this.onBackPressureStarted) { + this.onBackPressureStarted = true + this.emit('backPressure', { workerId: this.info.id as number }) + this.onBackPressureStarted = false } return tasksQueueSize } @@ -86,28 +80,22 @@ implements IWorkerNode { /** @inheritdoc */ public unshiftTask (task: Task): number { const tasksQueueSize = this.tasksQueue.unshift(task) - if (this.onBackPressure != null && this.hasBackPressure()) { - this.onBackPressure(this.info.id as number) + if (this.hasBackPressure() && !this.onBackPressureStarted) { + this.onBackPressureStarted = true + this.emit('backPressure', { workerId: this.info.id as number }) + this.onBackPressureStarted = false } return tasksQueueSize } /** @inheritdoc */ public dequeueTask (): Task | undefined { - const task = this.tasksQueue.shift() - if (this.onEmptyQueue != null && this.tasksQueue.size === 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.startOnEmptyQueue().catch(EMPTY_FUNCTION) - } - return task + return this.tasksQueue.pop() } /** @inheritdoc */ @@ -129,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 } } @@ -161,18 +149,9 @@ implements IWorkerNode { return this.taskFunctionsUsage.get(name) } - private async startOnEmptyQueue (): Promise { - if ( - this.onEmptyQueueCount > 0 && - (this.usage.tasks.executing > 0 || this.tasksQueue.size > 0) - ) { - this.onEmptyQueueCount = 0 - return - } - (this.onEmptyQueue as WorkerNodeEventCallback)(this.info.id as number) - ++this.onEmptyQueueCount - await sleep(exponentialDelay(this.onEmptyQueueCount)) - await this.startOnEmptyQueue() + /** @inheritdoc */ + public deleteTaskFunctionWorkerUsage (name: string): boolean { + return this.taskFunctionsUsage.delete(name) } private initWorkerInfo (worker: Worker): WorkerInfo { @@ -201,21 +180,22 @@ implements IWorkerNode { get maxQueued (): number { return getTasksQueueMaxSize() }, + sequentiallyStolen: 0, stolen: 0, failed: 0 }, runTime: { - history: new CircularArray() + history: new CircularArray() }, waitTime: { - history: new CircularArray() + history: new CircularArray() }, elu: { idle: { - history: new CircularArray() + history: new CircularArray() }, active: { - history: new CircularArray() + history: new CircularArray() } } } @@ -242,47 +222,24 @@ implements IWorkerNode { get queued (): number { return getTaskFunctionQueueSize() }, + sequentiallyStolen: 0, stolen: 0, failed: 0 }, runTime: { - history: new CircularArray() + history: new CircularArray() }, waitTime: { - history: new CircularArray() + history: new CircularArray() }, elu: { idle: { - history: new CircularArray() + history: new CircularArray() }, active: { - history: new CircularArray() + history: new CircularArray() } } } } - - private checkWorkerNodeArguments ( - worker: Worker, - tasksQueueBackPressureSize: number - ): void { - if (worker == null) { - throw new TypeError('Cannot construct a worker node without a worker') - } - if (tasksQueueBackPressureSize == null) { - throw new TypeError( - 'Cannot construct a worker node without a tasks queue back pressure size' - ) - } - if (!Number.isSafeInteger(tasksQueueBackPressureSize)) { - throw new TypeError( - 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer' - ) - } - if (tasksQueueBackPressureSize <= 0) { - throw new RangeError( - 'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer' - ) - } - } }