X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fless-busy-worker-choice-strategy.ts;h=f518f9b17f311ad3a8ad262e3b7e5f0be15cc61b;hb=dab8c377b70fc962ec217f2aeb719842f9f94cd6;hp=d98732d66882a66e961a04dc49e4241ae5db6e7c;hpb=f06e48d8e14dcfe3277bd16b1bd2463136af13e6;p=poolifier.git diff --git a/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts b/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts index d98732d6..f518f9b1 100644 --- a/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts @@ -1,8 +1,11 @@ +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, - RequiredStatistics + RequiredStatistics, + WorkerChoiceStrategyOptions } from './selection-strategies-types' /** @@ -10,7 +13,7 @@ import type { * * @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 LessBusyWorkerChoiceStrategy< Worker extends IWorker, @@ -26,33 +29,47 @@ export class LessBusyWorkerChoiceStrategy< medRunTime: false } + /** @inheritDoc */ + public constructor ( + pool: IPool, + opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS + ) { + super(pool, opts) + this.setRequiredStatistics(this.opts) + } + /** @inheritDoc */ public reset (): boolean { return true } + /** @inheritDoc */ + public update (): boolean { + return true + } + /** @inheritDoc */ public choose (): number { - const freeWorkerNodeKey = this.pool.findFreeWorkerNodeKey() + const freeWorkerNodeKey = this.findFreeWorkerNodeKey() if (freeWorkerNodeKey !== -1) { return freeWorkerNodeKey } let minRunTime = Infinity let lessBusyWorkerNodeKey!: number - for (const [index, workerNode] of this.pool.workerNodes.entries()) { + for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { const workerRunTime = workerNode.tasksUsage.runTime if (workerRunTime === 0) { - return index + return workerNodeKey } else if (workerRunTime < minRunTime) { minRunTime = workerRunTime - lessBusyWorkerNodeKey = index + lessBusyWorkerNodeKey = workerNodeKey } } return lessBusyWorkerNodeKey } /** @inheritDoc */ - public remove (workerNodeKey: number): boolean { + public remove (): boolean { return true } }