+ protected readonly pool: IPool<Worker, Data, Response>,
+ protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+ ) {
+ this.isDynamicPool = this.pool.type === PoolType.DYNAMIC
+ this.choose = this.choose.bind(this)
+ }
+
+ protected checkOptions (opts: WorkerChoiceStrategyOptions): void {
+ if (this.requiredStatistics.avgRunTime && opts.medRunTime === true) {
+ this.requiredStatistics.avgRunTime = false
+ this.requiredStatistics.medRunTime = opts.medRunTime as boolean
+ }
+ if (this.requiredStatistics.medRunTime && opts.medRunTime === false) {
+ this.requiredStatistics.avgRunTime = true
+ this.requiredStatistics.medRunTime = opts.medRunTime as boolean
+ }
+ }
+
+ /** @inheritDoc */
+ public abstract reset (): boolean
+
+ /** @inheritDoc */
+ public abstract choose (): number
+
+ /** @inheritDoc */
+ public abstract remove (workerNodeKey: number): boolean
+
+ /** @inheritDoc */
+ public setOptions (opts: WorkerChoiceStrategyOptions): void {
+ opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+ this.checkOptions(opts)
+ this.opts = opts
+ }
+
+ /**
+ * Finds a free worker node key.
+ *
+ * @returns The free worker node key or `-1` if there is no free worker node.
+ */
+ protected findFreeWorkerNodeKey (): number {
+ if (this.toggleFindLastFreeWorkerNodeKey) {
+ this.toggleFindLastFreeWorkerNodeKey = false
+ return this.findLastFreeWorkerNodeKey()
+ }
+ this.toggleFindLastFreeWorkerNodeKey = true
+ return this.findFirstFreeWorkerNodeKey()
+ }