refactor: factor out worker choice strategies options default
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 10 Apr 2023 18:00:31 +0000 (20:00 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 10 Apr 2023 18:00:31 +0000 (20:00 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
src/pools/selection-strategies/abstract-worker-choice-strategy.ts
src/pools/selection-strategies/worker-choice-strategy-context.ts
src/utils.ts

index a840dc740116057f8fa7867f85348a676caf04a4..6462cb09a882c6c005ceea481cacb5519e2de0de 100644 (file)
@@ -1,6 +1,10 @@
 import crypto from 'node:crypto'
 import type { MessageValue, PromiseResponseWrapper } from '../utility-types'
-import { EMPTY_FUNCTION, median } from '../utils'
+import {
+  DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS,
+  EMPTY_FUNCTION,
+  median
+} from '../utils'
 import { KillBehaviors, isKillBehavior } from '../worker/worker-options'
 import { PoolEvents, type PoolOptions } from './pool'
 import { PoolEmitter } from './pool'
@@ -132,7 +136,7 @@ export abstract class AbstractPool<
       opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
     this.checkValidWorkerChoiceStrategy(this.opts.workerChoiceStrategy)
     this.opts.workerChoiceStrategyOptions =
-      opts.workerChoiceStrategyOptions ?? { medRunTime: false }
+      opts.workerChoiceStrategyOptions ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
     this.opts.enableEvents = opts.enableEvents ?? true
     this.opts.enableTasksQueue = opts.enableTasksQueue ?? false
   }
index 1da5dc52ee69537e5a828a23c74262246867316b..d60d2905674e5b6fc5baa66616a63e9ab9f1fc85 100644 (file)
@@ -1,3 +1,4 @@
+import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
 import type { IPoolInternal } from '../pool-internal'
 import { PoolType } from '../pool-internal'
 import type { IWorker } from '../worker'
@@ -36,7 +37,7 @@ export abstract class AbstractWorkerChoiceStrategy<
    */
   public constructor (
     protected readonly pool: IPoolInternal<Worker, Data, Response>,
-    protected readonly opts: WorkerChoiceStrategyOptions = { medRunTime: false }
+    protected readonly opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
   ) {
     this.checkOptions(this.opts)
     this.isDynamicPool = this.pool.type === PoolType.DYNAMIC
index e763f7b2709c083afc81f7c7e53c074e9c59e7a8..486668a2f80d49d80a70f00528a7c8b4ad33467a 100644 (file)
@@ -1,3 +1,4 @@
+import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
 import type { IPoolInternal } from '../pool-internal'
 import type { IWorker } from '../worker'
 import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy'
@@ -40,7 +41,7 @@ export class WorkerChoiceStrategyContext<
   public constructor (
     pool: IPoolInternal<Worker, Data, Response>,
     private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN,
-    opts: WorkerChoiceStrategyOptions = { medRunTime: false }
+    opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
   ) {
     this.execute.bind(this)
     this.workerChoiceStrategies = new Map<
index d85c5ba58b4988fad867a9b35f9dca5bb5bb2b45..fc328a6c688e2d85f04a04be0e4874584ad5c57c 100644 (file)
@@ -1,3 +1,5 @@
+import type { WorkerChoiceStrategyOptions } from './pools/selection-strategies/selection-strategies-types'
+
 /**
  * An intentional empty function.
  */
@@ -6,7 +8,15 @@ export const EMPTY_FUNCTION: () => void = Object.freeze(() => {
 })
 
 /**
- * Returns the median of the given data set.
+ * Default worker choice strategy options.
+ */
+export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS: WorkerChoiceStrategyOptions =
+  {
+    medRunTime: false
+  }
+
+/**
+ * Compute the median of the given data set.
  *
  * @param dataSet - Data set.
  * @returns The median of the given data set.