X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fworker-choice-strategies-context.ts;h=a736896a792649a85c9294d9b53aa6633b442820;hb=6e5d7052fe741b50e68f8614b33d3754be41415f;hp=d088015acc1ad9b62fd57a1221871e42210418bb;hpb=c99df098ff02de4aeb34a422f2ab7c525e7c37ae;p=poolifier.git diff --git a/src/pools/selection-strategies/worker-choice-strategies-context.ts b/src/pools/selection-strategies/worker-choice-strategies-context.ts index d088015a..a736896a 100644 --- a/src/pools/selection-strategies/worker-choice-strategies-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategies-context.ts @@ -5,19 +5,18 @@ import type { StrategyPolicy, TaskStatisticsRequirements, WorkerChoiceStrategy, - WorkerChoiceStrategyOptions + WorkerChoiceStrategyOptions, } from './selection-strategies-types.js' import { WorkerChoiceStrategies } from './selection-strategies-types.js' import { buildWorkerChoiceStrategiesPolicy, buildWorkerChoiceStrategiesTaskStatisticsRequirements, getWorkerChoiceStrategiesRetries, - getWorkerChoiceStrategy + getWorkerChoiceStrategy, } from './selection-strategies-utils.js' /** * The worker choice strategies context. - * * @typeParam Worker - Type of worker. * @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. @@ -41,8 +40,8 @@ export class WorkerChoiceStrategiesContext< * The worker choice strategies registered in the context. */ private readonly workerChoiceStrategies: Map< - WorkerChoiceStrategy, - IWorkerChoiceStrategy + WorkerChoiceStrategy, + IWorkerChoiceStrategy > /** @@ -62,7 +61,6 @@ export class WorkerChoiceStrategiesContext< /** * Worker choice strategies context constructor. - * * @param pool - The pool instance. * @param workerChoiceStrategies - The worker choice strategies. @defaultValue [WorkerChoiceStrategies.ROUND_ROBIN] * @param opts - The worker choice strategy options. @@ -70,15 +68,15 @@ export class WorkerChoiceStrategiesContext< public constructor ( private readonly pool: IPool, workerChoiceStrategies: WorkerChoiceStrategy[] = [ - WorkerChoiceStrategies.ROUND_ROBIN + WorkerChoiceStrategies.ROUND_ROBIN, ], opts?: WorkerChoiceStrategyOptions ) { this.execute = this.execute.bind(this) this.defaultWorkerChoiceStrategy = workerChoiceStrategies[0] this.workerChoiceStrategies = new Map< - WorkerChoiceStrategy, - IWorkerChoiceStrategy + WorkerChoiceStrategy, + IWorkerChoiceStrategy >() for (const workerChoiceStrategy of workerChoiceStrategies) { this.addWorkerChoiceStrategy(workerChoiceStrategy, this.pool, opts) @@ -99,7 +97,6 @@ export class WorkerChoiceStrategiesContext< /** * Gets the active worker choice strategies in the context policy. - * * @returns The strategies policy. */ public getPolicy (): StrategyPolicy { @@ -108,7 +105,6 @@ export class WorkerChoiceStrategiesContext< /** * Gets the active worker choice strategies in the context task statistics requirements. - * * @returns The strategies task statistics requirements. */ public getTaskStatisticsRequirements (): TaskStatisticsRequirements { @@ -117,7 +113,6 @@ export class WorkerChoiceStrategiesContext< /** * Sets the default worker choice strategy to use in the context. - * * @param workerChoiceStrategy - The default worker choice strategy to set. * @param opts - The worker choice strategy options. */ @@ -133,20 +128,18 @@ export class WorkerChoiceStrategiesContext< /** * Updates the worker node key in the active worker choice strategies in the context internals. - * + * @param workerNodeKey - The worker node key. * @returns `true` if the update is successful, `false` otherwise. */ public update (workerNodeKey: number): boolean { - const res: boolean[] = [] - for (const workerChoiceStrategy of this.workerChoiceStrategies.values()) { - res.push(workerChoiceStrategy.update(workerNodeKey)) - } - return res.every(r => r) + return Array.from( + this.workerChoiceStrategies, + ([_, workerChoiceStrategy]) => workerChoiceStrategy.update(workerNodeKey) + ).every(r => r) } /** * Executes the given worker choice strategy in the context algorithm. - * * @param workerChoiceStrategy - The worker choice strategy algorithm to execute. @defaultValue this.defaultWorkerChoiceStrategy * @returns The key of the worker node. * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined. @@ -163,7 +156,6 @@ export class WorkerChoiceStrategiesContext< /** * Executes the given worker choice strategy. - * * @param workerChoiceStrategy - The worker choice strategy. * @returns The key of the worker node. * @throws {@link https://nodejs.org/api/errors.html#class-error} If after computed retries the worker node key is null or undefined. @@ -182,7 +174,7 @@ export class WorkerChoiceStrategiesContext< } while (workerNodeKey == null && retriesCount < this.retries) if (workerNodeKey == null) { throw new Error( - `Worker node key chosen is null or undefined after ${retriesCount} retries` + `Worker node key chosen is null or undefined after ${retriesCount.toString()} retries` ) } return workerNodeKey @@ -190,21 +182,18 @@ export class WorkerChoiceStrategiesContext< /** * Removes the worker node key from the active worker choice strategies in the context. - * * @param workerNodeKey - The worker node key. * @returns `true` if the removal is successful, `false` otherwise. */ public remove (workerNodeKey: number): boolean { - const res: boolean[] = [] - for (const workerChoiceStrategy of this.workerChoiceStrategies.values()) { - res.push(workerChoiceStrategy.remove(workerNodeKey)) - } - return res.every(r => r) + return Array.from( + this.workerChoiceStrategies, + ([_, workerChoiceStrategy]) => workerChoiceStrategy.remove(workerNodeKey) + ).every(r => r) } /** * Sets the active worker choice strategies in the context options. - * * @param opts - The worker choice strategy options. */ public setOptions (opts: WorkerChoiceStrategyOptions | undefined): void { @@ -215,7 +204,6 @@ export class WorkerChoiceStrategiesContext< /** * Synchronizes the active worker choice strategies in the context with the given worker choice strategies. - * * @param workerChoiceStrategies - The worker choice strategies to synchronize. * @param opts - The worker choice strategy options. */ @@ -244,8 +232,8 @@ export class WorkerChoiceStrategiesContext< /** * Adds a worker choice strategy to the context. - * * @param workerChoiceStrategy - The worker choice strategy to add. + * @param pool - The pool instance. * @param opts - The worker choice strategy options. * @returns The worker choice strategies. */ @@ -270,7 +258,6 @@ export class WorkerChoiceStrategiesContext< /** * Removes a worker choice strategy from the context. - * * @param workerChoiceStrategy - The worker choice strategy to remove. * @returns `true` if the worker choice strategy is removed, `false` otherwise. */