perf: alternate worker selection between start and end of worker nodes
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
1 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
2 import { PoolType, type IPool } from '../pool'
3 import type { IWorker } from '../worker'
4 import type {
5 IWorkerChoiceStrategy,
6 RequiredStatistics,
7 WorkerChoiceStrategyOptions
8 } from './selection-strategies-types'
9
10 /**
11 * Worker choice strategy abstract base class.
12 *
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 execution response. This can only be serializable data.
16 */
17 export abstract class AbstractWorkerChoiceStrategy<
18 Worker extends IWorker,
19 Data = unknown,
20 Response = unknown
21 > implements IWorkerChoiceStrategy {
22 /**
23 * Toggles finding the last free worker node key.
24 */
25 private toggleFindLastFreeWorkerNodeKey: boolean = false
26 /** @inheritDoc */
27 protected readonly isDynamicPool: boolean
28 /** @inheritDoc */
29 public readonly requiredStatistics: RequiredStatistics = {
30 runTime: false,
31 avgRunTime: false,
32 medRunTime: false
33 }
34
35 /**
36 * Constructs a worker choice strategy bound to the pool.
37 *
38 * @param pool - The pool instance.
39 * @param opts - The worker choice strategy options.
40 */
41 public constructor (
42 protected readonly pool: IPool<Worker, Data, Response>,
43 protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
44 ) {
45 this.isDynamicPool = this.pool.type === PoolType.DYNAMIC
46 this.choose = this.choose.bind(this)
47 }
48
49 protected checkOptions (opts: WorkerChoiceStrategyOptions): void {
50 if (this.requiredStatistics.avgRunTime && opts.medRunTime === true) {
51 this.requiredStatistics.avgRunTime = false
52 this.requiredStatistics.medRunTime = opts.medRunTime as boolean
53 }
54 if (this.requiredStatistics.medRunTime && opts.medRunTime === false) {
55 this.requiredStatistics.avgRunTime = true
56 this.requiredStatistics.medRunTime = opts.medRunTime as boolean
57 }
58 }
59
60 /** @inheritDoc */
61 public abstract reset (): boolean
62
63 /** @inheritDoc */
64 public abstract choose (): number
65
66 /** @inheritDoc */
67 public abstract remove (workerNodeKey: number): boolean
68
69 /** @inheritDoc */
70 public setOptions (opts: WorkerChoiceStrategyOptions): void {
71 opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
72 this.checkOptions(opts)
73 this.opts = opts
74 }
75
76 /**
77 * Finds a free worker node key.
78 *
79 * @returns The free worker node key or `-1` if there is no free worker node.
80 */
81 protected findFreeWorkerNodeKey (): number {
82 if (this.toggleFindLastFreeWorkerNodeKey) {
83 this.toggleFindLastFreeWorkerNodeKey = false
84 return this.pool.findLastFreeWorkerNodeKey()
85 }
86 this.toggleFindLastFreeWorkerNodeKey = true
87 return this.pool.findFreeWorkerNodeKey()
88 }
89 }