X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-used-worker-choice-strategy.ts;h=e8a7218e160879bcaa97399d78a3b39ff7857bc1;hb=3d76750ac8dacd95003ca89cf9542c3b2a014b8c;hp=a505e6270aea7f19c458d1872aa2f69fe6b190cb;hpb=d33be4309c69e39da5e81479e40b1a5ec7078bd5;p=poolifier.git diff --git a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts index a505e627..e8a7218e 100644 --- a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts @@ -11,8 +11,8 @@ import type { * Selects the least used worker. * * @typeParam Worker - Type of worker which manages the strategy. - * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of execution response. This can only be serializable data. + * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. + * @typeParam Response - Type of execution response. This can only be structured-cloneable data. */ export class LeastUsedWorkerChoiceStrategy< Worker extends IWorker, @@ -37,31 +37,38 @@ export class LeastUsedWorkerChoiceStrategy< /** @inheritDoc */ public update (): boolean { - let minNumberOfTasks = Infinity - for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { - const workerTaskStatistics = workerNode.workerUsage.tasks - const workerTasks = - workerTaskStatistics.executed + - workerTaskStatistics.executing + - workerTaskStatistics.queued - if (workerTasks === 0) { - this.nextWorkerNodeId = workerNodeKey - return true - } else if (workerTasks < minNumberOfTasks) { - minNumberOfTasks = workerTasks - this.nextWorkerNodeId = workerNodeKey - } - } return true } /** @inheritDoc */ public choose (): number { - return this.nextWorkerNodeId + return this.leastUsedNextWorkerNodeKey() } /** @inheritDoc */ public remove (): boolean { return true } + + private leastUsedNextWorkerNodeKey (): number { + let minNumberOfTasks = Infinity + for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { + const workerTaskStatistics = workerNode.usage.tasks + const workerTasks = + workerTaskStatistics.executed + + workerTaskStatistics.executing + + workerTaskStatistics.queued + if (this.isWorkerNodeReady(workerNodeKey) && workerTasks === 0) { + this.nextWorkerNodeKey = workerNodeKey + break + } else if ( + this.isWorkerNodeReady(workerNodeKey) && + workerTasks < minNumberOfTasks + ) { + minNumberOfTasks = workerTasks + this.nextWorkerNodeKey = workerNodeKey + } + } + return this.nextWorkerNodeKey + } }