From 8c0b113f317f5e59160fb5174fd951330756221b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 27 Aug 2023 20:17:26 +0200 Subject: [PATCH] refactor: cleanup worker choice strategies options namespace MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 4 ++ docs/api.md | 4 +- src/pools/abstract-pool.ts | 12 +++--- .../selection-strategies-types.ts | 2 +- .../worker-choice-strategy-context.ts | 10 ++--- src/utils.ts | 2 +- tests/pools/abstract/abstract-pool.test.js | 42 +++++++++---------- 7 files changed, 40 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47af726e..f4c452a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- Rename worker choice strategy options `choiceRetries` to `retries`. + ## [2.6.36] - 2023-08-27 ### Fixed diff --git a/docs/api.md b/docs/api.md index 0a2fea30..cc486202 100644 --- a/docs/api.md +++ b/docs/api.md @@ -74,14 +74,14 @@ An object with these properties: - `workerChoiceStrategyOptions` (optional) - The worker choice strategy options object to use in this pool. Properties: - - `choiceRetries` (optional) - The number of retries to perform if no worker is eligible. + - `retries` (optional) - The number of retries to perform if no worker is eligible. - `measurement` (optional) - The measurement to use in worker choice strategies: `runTime`, `waitTime` or `elu`. - `runTime` (optional) - Use the tasks [simple moving median](./worker-choice-strategies.md#simple-moving-median) runtime instead of the tasks simple moving average runtime in worker choice strategies. - `waitTime` (optional) - Use the tasks [simple moving median](./worker-choice-strategies.md#simple-moving-median) wait time instead of the tasks simple moving average wait time in worker choice strategies. - `elu` (optional) - Use the tasks [simple moving median](./worker-choice-strategies.md#simple-moving-median) ELU instead of the tasks simple moving average ELU in worker choice strategies. - `weights` (optional) - The worker weights to use in weighted round robin worker choice strategies: `{ 0: 200, 1: 300, ..., n: 100 }`. - Default: `{ choiceRetries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } }` + Default: `{ retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } }` - `restartWorkerOnError` (optional) - Restart worker on uncaught error in this pool. Default: `true` diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 482a20db..41b23a8c 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -255,19 +255,19 @@ export abstract class AbstractPool< ) } if ( - workerChoiceStrategyOptions.choiceRetries != null && - !Number.isSafeInteger(workerChoiceStrategyOptions.choiceRetries) + workerChoiceStrategyOptions.retries != null && + !Number.isSafeInteger(workerChoiceStrategyOptions.retries) ) { throw new TypeError( - 'Invalid worker choice strategy options: choice retries must be an integer' + 'Invalid worker choice strategy options: retries must be an integer' ) } if ( - workerChoiceStrategyOptions.choiceRetries != null && - workerChoiceStrategyOptions.choiceRetries < 0 + workerChoiceStrategyOptions.retries != null && + workerChoiceStrategyOptions.retries < 0 ) { throw new RangeError( - `Invalid worker choice strategy options: choice retries '${workerChoiceStrategyOptions.choiceRetries}' must be greater or equal than zero` + `Invalid worker choice strategy options: retries '${workerChoiceStrategyOptions.retries}' must be greater or equal than zero` ) } if ( diff --git a/src/pools/selection-strategies/selection-strategies-types.ts b/src/pools/selection-strategies/selection-strategies-types.ts index e7bf0e1e..b476592a 100644 --- a/src/pools/selection-strategies/selection-strategies-types.ts +++ b/src/pools/selection-strategies/selection-strategies-types.ts @@ -72,7 +72,7 @@ export interface WorkerChoiceStrategyOptions { * * @defaultValue 6 */ - readonly choiceRetries?: number + readonly retries?: number /** * Measurement to use in worker choice strategy supporting it. */ diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index 7c7574fa..e3aaaea3 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -37,7 +37,7 @@ export class WorkerChoiceStrategyContext< /** * The number of times the worker choice strategy in the context has been retried. */ - private choiceRetriesCount = 0 + private retriesCount = 0 /** * Worker choice strategy context constructor. @@ -178,16 +178,16 @@ export class WorkerChoiceStrategyContext< ).choose() if ( workerNodeKey == null && - this.choiceRetriesCount < (this.opts.choiceRetries as number) + this.retriesCount < (this.opts.retries as number) ) { - this.choiceRetriesCount++ + this.retriesCount++ return this.execute() } else if (workerNodeKey == null) { throw new Error( - `Worker node key chosen is null or undefined after ${this.choiceRetriesCount} retries` + `Worker node key chosen is null or undefined after ${this.retriesCount} retries` ) } - this.choiceRetriesCount = 0 + this.retriesCount = 0 return workerNodeKey } diff --git a/src/utils.ts b/src/utils.ts index 8b88d2af..21b2032b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -31,7 +31,7 @@ export const EMPTY_FUNCTION: () => void = Object.freeze(() => { */ export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS: WorkerChoiceStrategyOptions = { - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 75b0a35c..5435565a 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -188,13 +188,13 @@ describe('Abstract pool test suite', () => { WorkerChoiceStrategies.ROUND_ROBIN ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } }) expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } @@ -236,14 +236,14 @@ describe('Abstract pool test suite', () => { WorkerChoiceStrategies.LEAST_USED ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: true }, waitTime: { median: false }, elu: { median: false }, weights: { 0: 300, 1: 200 } }) expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: true }, waitTime: { median: false }, elu: { median: false }, @@ -276,13 +276,13 @@ describe('Abstract pool test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategyOptions: { - choiceRetries: 'invalidChoiceRetries' + retries: 'invalidChoiceRetries' } } ) ).toThrowError( new TypeError( - 'Invalid worker choice strategy options: choice retries must be an integer' + 'Invalid worker choice strategy options: retries must be an integer' ) ) expect( @@ -292,13 +292,13 @@ describe('Abstract pool test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategyOptions: { - choiceRetries: -1 + retries: -1 } } ) ).toThrowError( new RangeError( - "Invalid worker choice strategy options: choice retries '-1' must be greater or equal than zero" + "Invalid worker choice strategy options: retries '-1' must be greater or equal than zero" ) ) expect( @@ -452,13 +452,13 @@ describe('Abstract pool test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } }) expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } @@ -466,7 +466,7 @@ describe('Abstract pool test suite', () => { for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { expect(workerChoiceStrategy.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } @@ -496,13 +496,13 @@ describe('Abstract pool test suite', () => { elu: { median: true } }) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: true }, waitTime: { median: false }, elu: { median: true } }) expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: true }, waitTime: { median: false }, elu: { median: true } @@ -510,7 +510,7 @@ describe('Abstract pool test suite', () => { for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { expect(workerChoiceStrategy.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: true }, waitTime: { median: false }, elu: { median: true } @@ -540,13 +540,13 @@ describe('Abstract pool test suite', () => { elu: { median: false } }) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } }) expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } @@ -554,7 +554,7 @@ describe('Abstract pool test suite', () => { for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { expect(workerChoiceStrategy.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } @@ -588,18 +588,18 @@ describe('Abstract pool test suite', () => { ) expect(() => pool.setWorkerChoiceStrategyOptions({ - choiceRetries: 'invalidChoiceRetries' + retries: 'invalidChoiceRetries' }) ).toThrowError( new TypeError( - 'Invalid worker choice strategy options: choice retries must be an integer' + 'Invalid worker choice strategy options: retries must be an integer' ) ) expect(() => - pool.setWorkerChoiceStrategyOptions({ choiceRetries: -1 }) + pool.setWorkerChoiceStrategyOptions({ retries: -1 }) ).toThrowError( new RangeError( - "Invalid worker choice strategy options: choice retries '-1' must be greater or equal than zero" + "Invalid worker choice strategy options: retries '-1' must be greater or equal than zero" ) ) expect(() => -- 2.34.1