X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fpools%2Fselection-strategies%2Fless-used-worker-choice-strategy.ts;h=1503e057ef42fc3890eb0e8cfbc33496a1dd9f5e;hb=08f3f44cef6256fdbab1a2a56842b291fd6dcd42;hp=acf1e5036aeed44a7ae8f6d11394160b95aec10a;hpb=f06e48d8e14dcfe3277bd16b1bd2463136af13e6;p=poolifier.git diff --git a/src/pools/selection-strategies/less-used-worker-choice-strategy.ts b/src/pools/selection-strategies/less-used-worker-choice-strategy.ts index acf1e503..1503e057 100644 --- a/src/pools/selection-strategies/less-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-used-worker-choice-strategy.ts @@ -1,13 +1,18 @@ +import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' +import type { IPool } from '../pool' import type { IWorker } from '../worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' -import type { IWorkerChoiceStrategy } from './selection-strategies-types' +import type { + IWorkerChoiceStrategy, + WorkerChoiceStrategyOptions +} from './selection-strategies-types' /** * Selects the less 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 response of execution. This can only be serializable data. + * @typeParam Response - Type of execution response. This can only be serializable data. */ export class LessUsedWorkerChoiceStrategy< Worker extends IWorker, @@ -16,6 +21,15 @@ export class LessUsedWorkerChoiceStrategy< > extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { + /** @inheritDoc */ + public constructor ( + pool: IPool, + opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS + ) { + super(pool, opts) + this.checkOptions(this.opts) + } + /** @inheritDoc */ public reset (): boolean { return true @@ -23,20 +37,20 @@ export class LessUsedWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number { - const freeWorkerNodeKey = this.pool.findFreeWorkerNodeKey() + const freeWorkerNodeKey = this.findFreeWorkerNodeKey() if (freeWorkerNodeKey !== -1) { return freeWorkerNodeKey } let minNumberOfTasks = Infinity let lessUsedWorkerNodeKey!: number - for (const [index, workerNode] of this.pool.workerNodes.entries()) { + for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { const tasksUsage = workerNode.tasksUsage const workerTasks = tasksUsage.run + tasksUsage.running if (workerTasks === 0) { - return index + return workerNodeKey } else if (workerTasks < minNumberOfTasks) { minNumberOfTasks = workerTasks - lessUsedWorkerNodeKey = index + lessUsedWorkerNodeKey = workerNodeKey } } return lessUsedWorkerNodeKey