X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=82e276886c08f56bc4051bc537df88b222600af0;hb=e560a9f2e03d63760bb42ac7d8d8bd9b23c9b1a5;hp=7667dd16a6b99c2994eaeda37866662b43f8d178;hpb=db0e38eed9f8945c38ff579f5b33cb4bc44f62a1;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 7667dd16..82e27688 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -40,6 +40,24 @@ implements IWorkerNode { * @param poolMaxSize - The pool maximum size. */ constructor (worker: Worker, workerType: WorkerType, poolMaxSize: 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 (poolMaxSize == null) { + throw new TypeError( + 'Cannot construct a worker node without a pool maximum size' + ) + } + if (!Number.isSafeInteger(poolMaxSize)) { + throw new TypeError( + 'Cannot construct a worker node with a pool maximum size that is not an integer' + ) + } this.worker = worker this.info = this.initWorkerInfo(worker, workerType) if (workerType === WorkerTypes.thread) { @@ -82,7 +100,7 @@ implements IWorkerNode { /** @inheritdoc */ public hasBackPressure (): boolean { - return this.tasksQueueSize() >= this.tasksQueueBackPressureSize + return this.tasksQueue.size >= this.tasksQueueBackPressureSize } /** @inheritdoc */ @@ -172,21 +190,25 @@ implements IWorkerNode { } private initTaskFunctionWorkerUsage (name: string): WorkerUsage { - const getTaskQueueSize = (): number => { - let taskQueueSize = 0 + const getTaskFunctionQueueSize = (): number => { + let taskFunctionQueueSize = 0 for (const task of this.tasksQueue) { - if (task.name === name) { - ++taskQueueSize + if ( + (task.name === DEFAULT_TASK_NAME && + name === (this.info.taskFunctions as string[])[1]) || + (task.name !== DEFAULT_TASK_NAME && name === task.name) + ) { + ++taskFunctionQueueSize } } - return taskQueueSize + return taskFunctionQueueSize } return { tasks: { executed: 0, executing: 0, get queued (): number { - return getTaskQueueSize() + return getTaskFunctionQueueSize() }, failed: 0 },