X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=f3ec3be3d83f4541da3713cbd46406d19be41ac0;hb=6e9c39d3eedabc9f12808490983d36e551a75a28;hp=116fb8448d8435ecb98c5573bb781f258026e589;hpb=dd95187664018669192560e65e3b996a8143b699;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 116fb844..f3ec3be3 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,7 +1,12 @@ 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, + sleep +} from '../utils' import { Deque } from '../deque' import { type IWorker, @@ -12,6 +17,9 @@ import { type WorkerUsage } from './worker' +type EmptyQueueCallback = (workerId: number) => void +type BackPressureCallback = EmptyQueueCallback + /** * Worker node. * @@ -25,17 +33,18 @@ 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. @@ -69,13 +78,14 @@ implements IWorkerNode { } this.worker = worker this.info = this.initWorkerInfo(worker, workerType) + this.usage = this.initWorkerUsage() if (workerType === 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 +97,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 +106,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 +115,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 +124,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,6 +180,20 @@ 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 EmptyQueueCallback)(this.info.id as number) + ++this.onEmptyQueueCount + await sleep(exponentialDelay(this.onEmptyQueueCount)) + await this.startOnEmptyQueue() + } + private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo { return { id: this.getWorkerId(worker, workerType), @@ -196,6 +220,7 @@ implements IWorkerNode { get maxQueued (): number { return getTasksQueueMaxSize() }, + stolen: 0, failed: 0 }, runTime: { @@ -236,6 +261,7 @@ implements IWorkerNode { get queued (): number { return getTaskFunctionQueueSize() }, + stolen: 0, failed: 0 }, runTime: {