1 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
} from
'../../utils'
2 import { PoolType
, type IPool
} from
'../pool'
3 import type { IWorker
} from
'../worker'
7 WorkerChoiceStrategyOptions
8 } from
'./selection-strategies-types'
11 * Worker choice strategy abstract base class.
13 * @typeParam Worker - Type of worker which manages the strategy.
14 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
15 * @typeParam Response - Type of response of execution. This can only be serializable data.
17 export abstract class AbstractWorkerChoiceStrategy
<
18 Worker
extends IWorker
,
21 > implements IWorkerChoiceStrategy
{
23 protected readonly isDynamicPool
: boolean
25 public readonly requiredStatistics
: RequiredStatistics
= {
32 * Constructs a worker choice strategy bound to the pool.
34 * @param pool - The pool instance.
35 * @param opts - The worker choice strategy options.
38 protected readonly pool
: IPool
<Worker
, Data
, Response
>,
39 protected readonly opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
41 this.checkOptions(this.opts
)
42 this.isDynamicPool
= this.pool
.type === PoolType
.DYNAMIC
43 this.choose
.bind(this)
46 private checkOptions (opts
: WorkerChoiceStrategyOptions
): void {
47 if (this.requiredStatistics
.avgRunTime
&& opts
.medRunTime
=== true) {
48 this.requiredStatistics
.medRunTime
= true
53 public abstract reset (): boolean
56 public abstract choose (): number
59 public abstract remove (workerNodeKey
: number): boolean