X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fless-busy-worker-choice-strategy.ts;h=d862405c2e07b224602e21093f235675ee8c750c;hb=a20f0ba5aa9c6946254aa197286ad9b70b6a0319;hp=e9ba619a9767814acae8c2dd4caec7d07fc0dcd6;hpb=cf9c7b65548f4d3a4e95e87731ae1c40766d48fb;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 e9ba619a..d862405c 100644 --- a/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts @@ -1,47 +1,70 @@ -import type { IPoolWorker } from '../pool-worker' +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 { RequiredStatistics } from './selection-strategies-types' +import type { + IWorkerChoiceStrategy, + RequiredStatistics, + WorkerChoiceStrategyOptions +} from './selection-strategies-types' /** * Selects the less busy 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 LessBusyWorkerChoiceStrategy< - Worker extends IPoolWorker, - Data, - Response -> extends AbstractWorkerChoiceStrategy { - /** {@inheritDoc} */ + Worker extends IWorker, + Data = unknown, + Response = unknown + > + extends AbstractWorkerChoiceStrategy + implements IWorkerChoiceStrategy { + /** @inheritDoc */ public readonly requiredStatistics: RequiredStatistics = { - runTime: true + runTime: true, + avgRunTime: false, + medRunTime: false } - /** {@inheritDoc} */ + /** @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 } - /** {@inheritDoc} */ + /** @inheritDoc */ public choose (): number { + const freeWorkerNodeKey = this.pool.findFreeWorkerNodeKey() + if (freeWorkerNodeKey !== -1) { + return freeWorkerNodeKey + } let minRunTime = Infinity - let lessBusyWorkerKey!: number - for (const [index, workerItem] of this.pool.workers.entries()) { - const workerRunTime = workerItem.tasksUsage.runTime + let lessBusyWorkerNodeKey!: number + for (const [index, workerNode] of this.pool.workerNodes.entries()) { + const workerRunTime = workerNode.tasksUsage.runTime if (workerRunTime === 0) { return index } else if (workerRunTime < minRunTime) { minRunTime = workerRunTime - lessBusyWorkerKey = index + lessBusyWorkerNodeKey = index } } - return lessBusyWorkerKey + return lessBusyWorkerNodeKey } - /** {@inheritDoc} */ - public remove (workerKey: number): boolean { + /** @inheritDoc */ + public remove (workerNodeKey: number): boolean { return true } }