X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=1f46075a07e2a4849b6dcdd4a2f996fcea64e6be;hb=9fe8fd698590c2494dc6793cfd8c08026fe88a31;hp=116fb8448d8435ecb98c5573bb781f258026e589;hpb=a4ba0bba85ba79e61e4095edccbde9a6ee2fc08a;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 116fb844..1f46075a 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,9 +1,18 @@ import { MessageChannel } from 'node:worker_threads' import { CircularArray } from '../circular-array' import type { Task } from '../utility-types' -import { DEFAULT_TASK_NAME, once } from '../utils' +import { + DEFAULT_TASK_NAME, + EMPTY_FUNCTION, + exponentialDelay, + getWorkerId, + getWorkerType, + sleep +} from '../utils' import { Deque } from '../deque' import { + type BackPressureCallback, + type EmptyQueueCallback, type IWorker, type IWorkerNode, type WorkerInfo, @@ -25,38 +34,30 @@ implements IWorkerNode { /** @inheritdoc */ public readonly info: WorkerInfo /** @inheritdoc */ - public messageChannel?: MessageChannel - /** @inheritdoc */ public usage: WorkerUsage /** @inheritdoc */ + public messageChannel?: MessageChannel + /** @inheritdoc */ public tasksQueueBackPressureSize: number /** @inheritdoc */ - public onBackPressure?: (workerId: number) => void + public onBackPressure?: BackPressureCallback /** @inheritdoc */ - public onEmptyQueue?: (workerId: number) => void - private readonly taskFunctionsUsage: Map + public onEmptyQueue?: EmptyQueueCallback private readonly tasksQueue: Deque> + private onEmptyQueueCount: number + private readonly taskFunctionsUsage: Map /** * Constructs a new worker node. * * @param worker - The worker. - * @param workerType - The worker type. * @param tasksQueueBackPressureSize - The tasks queue back pressure size. */ - constructor ( - worker: Worker, - workerType: WorkerType, - tasksQueueBackPressureSize: number - ) { + constructor (worker: Worker, tasksQueueBackPressureSize: number) { if (worker == null) { throw new TypeError('Cannot construct a worker node without a worker') } - if (workerType == null) { - throw new TypeError( - 'Cannot construct a worker node without a worker type' - ) - } + if (tasksQueueBackPressureSize == null) { throw new TypeError( 'Cannot construct a worker node without a tasks queue back pressure size' @@ -68,14 +69,15 @@ implements IWorkerNode { ) } this.worker = worker - this.info = this.initWorkerInfo(worker, workerType) - if (workerType === WorkerTypes.thread) { + this.info = this.initWorkerInfo(worker) + this.usage = this.initWorkerUsage() + if (this.info.type === WorkerTypes.thread) { this.messageChannel = new MessageChannel() } - this.usage = this.initWorkerUsage() - this.taskFunctionsUsage = new Map() - this.tasksQueue = new Deque>() this.tasksQueueBackPressureSize = tasksQueueBackPressureSize + this.tasksQueue = new Deque>() + this.onEmptyQueueCount = 0 + this.taskFunctionsUsage = new Map() } /** @inheritdoc */ @@ -87,7 +89,7 @@ implements IWorkerNode { public enqueueTask (task: Task): number { const tasksQueueSize = this.tasksQueue.push(task) if (this.onBackPressure != null && this.hasBackPressure()) { - once(this.onBackPressure, this)(this.info.id as number) + this.onBackPressure(this.info.id as number) } return tasksQueueSize } @@ -96,7 +98,7 @@ implements IWorkerNode { public unshiftTask (task: Task): number { const tasksQueueSize = this.tasksQueue.unshift(task) if (this.onBackPressure != null && this.hasBackPressure()) { - once(this.onBackPressure, this)(this.info.id as number) + this.onBackPressure(this.info.id as number) } return tasksQueueSize } @@ -105,7 +107,7 @@ implements IWorkerNode { public dequeueTask (): Task | undefined { const task = this.tasksQueue.shift() if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { - once(this.onEmptyQueue, this)(this.info.id as number) + this.startOnEmptyQueue().catch(EMPTY_FUNCTION) } return task } @@ -114,7 +116,7 @@ implements IWorkerNode { public popTask (): Task | undefined { const task = this.tasksQueue.pop() if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { - once(this.onEmptyQueue, this)(this.info.id as number) + this.startOnEmptyQueue().catch(EMPTY_FUNCTION) } return task } @@ -170,10 +172,24 @@ implements IWorkerNode { return this.taskFunctionsUsage.get(name) } - private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo { + private async startOnEmptyQueue (): Promise { + if ( + this.onEmptyQueueCount > 0 && + (this.usage.tasks.executing > 0 || this.tasksQueue.size > 0) + ) { + this.onEmptyQueueCount = 0 + return + } + (this.onEmptyQueue as EmptyQueueCallback)(this.info.id as number) + ++this.onEmptyQueueCount + await sleep(exponentialDelay(this.onEmptyQueueCount)) + await this.startOnEmptyQueue() + } + + private initWorkerInfo (worker: Worker): WorkerInfo { return { - id: this.getWorkerId(worker, workerType), - type: workerType, + id: getWorkerId(worker), + type: getWorkerType(worker) as WorkerType, dynamic: false, ready: false } @@ -196,6 +212,7 @@ implements IWorkerNode { get maxQueued (): number { return getTasksQueueMaxSize() }, + stolen: 0, failed: 0 }, runTime: { @@ -236,6 +253,7 @@ implements IWorkerNode { get queued (): number { return getTaskFunctionQueueSize() }, + stolen: 0, failed: 0 }, runTime: { @@ -254,22 +272,4 @@ implements IWorkerNode { } } } - - /** - * Gets the worker id. - * - * @param worker - The worker. - * @param workerType - The worker type. - * @returns The worker id. - */ - private getWorkerId ( - worker: Worker, - workerType: WorkerType - ): number | undefined { - if (workerType === WorkerTypes.thread) { - return worker.threadId - } else if (workerType === WorkerTypes.cluster) { - return worker.id - } - } }