1 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
} from
'../../utils'
2 import type { IPoolInternal
} from
'../pool-internal'
3 import { PoolType
} from
'../pool-internal'
4 import type { IWorker
} from
'../worker'
8 WorkerChoiceStrategyOptions
9 } from
'./selection-strategies-types'
12 * Worker choice strategy abstract base class.
14 * @typeParam Worker - Type of worker which manages the strategy.
15 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
16 * @typeParam Response - Type of response of execution. This can only be serializable data.
18 export abstract class AbstractWorkerChoiceStrategy
<
19 Worker
extends IWorker
,
22 > implements IWorkerChoiceStrategy
{
24 protected readonly isDynamicPool
: boolean
26 public readonly requiredStatistics
: RequiredStatistics
= {
33 * Constructs a worker choice strategy bound to the pool.
35 * @param pool - The pool instance.
36 * @param opts - The worker choice strategy options.
39 protected readonly pool
: IPoolInternal
<Worker
, Data
, Response
>,
40 protected readonly opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
42 this.checkOptions(this.opts
)
43 this.isDynamicPool
= this.pool
.type === PoolType
.DYNAMIC
44 this.choose
.bind(this)
47 private checkOptions (opts
: WorkerChoiceStrategyOptions
): void {
48 if (this.requiredStatistics
.avgRunTime
&& opts
.medRunTime
=== true) {
49 this.requiredStatistics
.medRunTime
= true
54 public abstract reset (): boolean
57 public abstract choose (): number
60 public abstract remove (workerNodeKey
: number): boolean