X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=de65f27012634c493d697a0ae9e3972a7f5234db;hb=3ff2b9103a0933bd1575a979c5e0c2baad285eea;hp=78756560c0fbaa78fedb96aad7153a21d14e61ff;hpb=aed302d456407817a368e3f743020474b60facd2;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 78756560..de65f270 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -15,11 +15,12 @@ import { type IWorkerNode, type StrategyData, type WorkerInfo, - type WorkerNodeEventCallback, + type WorkerNodeEventDetail, type WorkerType, WorkerTypes, type WorkerUsage } from './worker' +import { checkWorkerNodeArguments } from './utils' /** * Worker node. @@ -28,7 +29,8 @@ import { * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. */ export class WorkerNode -implements IWorkerNode { + extends EventTarget + implements IWorkerNode { /** @inheritdoc */ public readonly worker: Worker /** @inheritdoc */ @@ -41,11 +43,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 @@ -56,7 +55,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,6 +65,7 @@ implements IWorkerNode { } this.tasksQueueBackPressureSize = tasksQueueBackPressureSize this.tasksQueue = new Deque>() + this.onBackPressureStarted = false this.onEmptyQueueCount = 0 this.taskFunctionsUsage = new Map() } @@ -77,8 +78,14 @@ 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.dispatchEvent( + new CustomEvent('backpressure', { + detail: { workerId: this.info.id as number } + }) + ) + this.onBackPressureStarted = false } return tasksQueueSize } @@ -86,8 +93,14 @@ 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.dispatchEvent( + new CustomEvent('backpressure', { + detail: { workerId: this.info.id as number } + }) + ) + this.onBackPressureStarted = false } return tasksQueueSize } @@ -95,7 +108,7 @@ implements IWorkerNode { /** @inheritdoc */ public dequeueTask (): Task | undefined { const task = this.tasksQueue.shift() - if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { + if (this.tasksQueue.size === 0 && this.onEmptyQueueCount === 0) { this.startOnEmptyQueue().catch(EMPTY_FUNCTION) } return task @@ -104,7 +117,7 @@ implements IWorkerNode { /** @inheritdoc */ public popTask (): Task | undefined { const task = this.tasksQueue.pop() - if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { + if (this.tasksQueue.size === 0 && this.onEmptyQueueCount === 0) { this.startOnEmptyQueue().catch(EMPTY_FUNCTION) } return task @@ -129,10 +142,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,6 +174,11 @@ implements IWorkerNode { return this.taskFunctionsUsage.get(name) } + /** @inheritdoc */ + public deleteTaskFunctionWorkerUsage (name: string): boolean { + return this.taskFunctionsUsage.delete(name) + } + private async startOnEmptyQueue (): Promise { if ( this.onEmptyQueueCount > 0 && @@ -169,8 +187,12 @@ implements IWorkerNode { this.onEmptyQueueCount = 0 return } - (this.onEmptyQueue as WorkerNodeEventCallback)(this.info.id as number) ++this.onEmptyQueueCount + this.dispatchEvent( + new CustomEvent('emptyqueue', { + detail: { workerId: this.info.id as number } + }) + ) await sleep(exponentialDelay(this.onEmptyQueueCount)) await this.startOnEmptyQueue() } @@ -205,17 +227,17 @@ implements IWorkerNode { 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() } } } @@ -246,43 +268,19 @@ implements IWorkerNode { 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' - ) - } - } }