X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=ed6677027f810474aa9aa01e768fd5f483250679;hb=f4d1dbd1592e24d7a09f35013c3e0f0762240254;hp=45b4f1ee18c041db8837ecc987c4d5cba38e998d;hpb=e919313e51a0ee0fb5b81b9d5594c356e87fa930;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 45b4f1ee..ed667702 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -11,11 +11,11 @@ import { } from '../utils' import { Deque } from '../deque' import { - type BackPressureCallback, - type EmptyQueueCallback, type IWorker, type IWorkerNode, + type StrategyData, type WorkerInfo, + type WorkerNodeEventCallback, type WorkerType, WorkerTypes, type WorkerUsage @@ -36,14 +36,17 @@ implements IWorkerNode { /** @inheritdoc */ public usage: WorkerUsage /** @inheritdoc */ + public strategyData?: StrategyData + /** @inheritdoc */ public messageChannel?: MessageChannel /** @inheritdoc */ public tasksQueueBackPressureSize: number /** @inheritdoc */ - public onBackPressure?: BackPressureCallback + public onBackPressure?: WorkerNodeEventCallback /** @inheritdoc */ - public onEmptyQueue?: EmptyQueueCallback + public onEmptyQueue?: WorkerNodeEventCallback private readonly tasksQueue: Deque> + private onBackPressureStarted: boolean private onEmptyQueueCount: number private readonly taskFunctionsUsage: Map @@ -54,19 +57,7 @@ implements IWorkerNode { * @param tasksQueueBackPressureSize - The tasks queue back pressure size. */ constructor (worker: Worker, tasksQueueBackPressureSize: number) { - 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' - ) - } + this.checkWorkerNodeArguments(worker, tasksQueueBackPressureSize) this.worker = worker this.info = this.initWorkerInfo(worker) this.usage = this.initWorkerUsage() @@ -75,6 +66,7 @@ implements IWorkerNode { } this.tasksQueueBackPressureSize = tasksQueueBackPressureSize this.tasksQueue = new Deque>() + this.onBackPressureStarted = false this.onEmptyQueueCount = 0 this.taskFunctionsUsage = new Map() } @@ -87,8 +79,14 @@ implements IWorkerNode { /** @inheritdoc */ public enqueueTask (task: Task): number { const tasksQueueSize = this.tasksQueue.push(task) - if (this.onBackPressure != null && this.hasBackPressure()) { + if ( + this.onBackPressure != null && + this.hasBackPressure() && + !this.onBackPressureStarted + ) { + this.onBackPressureStarted = true this.onBackPressure(this.info.id as number) + this.onBackPressureStarted = false } return tasksQueueSize } @@ -96,8 +94,14 @@ implements IWorkerNode { /** @inheritdoc */ public unshiftTask (task: Task): number { const tasksQueueSize = this.tasksQueue.unshift(task) - if (this.onBackPressure != null && this.hasBackPressure()) { + if ( + this.onBackPressure != null && + this.hasBackPressure() && + !this.onBackPressureStarted + ) { + this.onBackPressureStarted = true this.onBackPressure(this.info.id as number) + this.onBackPressureStarted = false } return tasksQueueSize } @@ -105,7 +109,11 @@ implements IWorkerNode { /** @inheritdoc */ public dequeueTask (): Task | undefined { const task = this.tasksQueue.shift() - if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { + if ( + this.onEmptyQueue != null && + this.tasksQueue.size === 0 && + this.onEmptyQueueCount === 0 + ) { this.startOnEmptyQueue().catch(EMPTY_FUNCTION) } return task @@ -114,7 +122,11 @@ implements IWorkerNode { /** @inheritdoc */ public popTask (): Task | undefined { const task = this.tasksQueue.pop() - if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { + if ( + this.onEmptyQueue != null && + this.tasksQueue.size === 0 && + this.onEmptyQueueCount === 0 + ) { this.startOnEmptyQueue().catch(EMPTY_FUNCTION) } return task @@ -149,21 +161,21 @@ implements IWorkerNode { /** @inheritdoc */ public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined { - if (!Array.isArray(this.info.taskFunctions)) { + if (!Array.isArray(this.info.taskFunctionNames)) { throw new Error( `Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined` ) } if ( - Array.isArray(this.info.taskFunctions) && - this.info.taskFunctions.length < 3 + Array.isArray(this.info.taskFunctionNames) && + this.info.taskFunctionNames.length < 3 ) { throw new Error( `Cannot get task function worker usage for task function name '${name}' when task function names list has less than 3 elements` ) } if (name === DEFAULT_TASK_NAME) { - name = this.info.taskFunctions[1] + name = this.info.taskFunctionNames[1] } if (!this.taskFunctionsUsage.has(name)) { this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name)) @@ -179,8 +191,8 @@ implements IWorkerNode { this.onEmptyQueueCount = 0 return } - (this.onEmptyQueue as EmptyQueueCallback)(this.info.id as number) ++this.onEmptyQueueCount + this.onEmptyQueue?.(this.info.id as number) await sleep(exponentialDelay(this.onEmptyQueueCount)) await this.startOnEmptyQueue() } @@ -237,7 +249,7 @@ implements IWorkerNode { for (const task of this.tasksQueue) { if ( (task.name === DEFAULT_TASK_NAME && - name === (this.info.taskFunctions as string[])[1]) || + name === (this.info.taskFunctionNames as string[])[1]) || (task.name !== DEFAULT_TASK_NAME && name === task.name) ) { ++taskFunctionQueueSize @@ -271,4 +283,28 @@ implements IWorkerNode { } } } + + 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' + ) + } + } }