Commit | Line | Data |
---|---|---|
2fc5cae3 JB |
1 | import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' |
2 | import type { IPool } from '../pool' | |
f06e48d8 | 3 | import type { IWorker } from '../worker' |
bdaf31cd | 4 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' |
2fc5cae3 JB |
5 | import type { |
6 | IWorkerChoiceStrategy, | |
b1aae695 | 7 | StrategyPolicy, |
2fc5cae3 JB |
8 | WorkerChoiceStrategyOptions |
9 | } from './selection-strategies-types' | |
bdaf31cd JB |
10 | |
11 | /** | |
e4543b14 | 12 | * Selects the least used worker. |
bdaf31cd | 13 | * |
38e795c1 | 14 | * @typeParam Worker - Type of worker which manages the strategy. |
e102732c JB |
15 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
16 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. | |
bdaf31cd | 17 | */ |
e4543b14 | 18 | export class LeastUsedWorkerChoiceStrategy< |
f06e48d8 | 19 | Worker extends IWorker, |
b2b1d84e JB |
20 | Data = unknown, |
21 | Response = unknown | |
bf90656c JB |
22 | > |
23 | extends AbstractWorkerChoiceStrategy<Worker, Data, Response> | |
17393ac8 | 24 | implements IWorkerChoiceStrategy { |
b1aae695 JB |
25 | /** @inheritDoc */ |
26 | public readonly strategyPolicy: StrategyPolicy = { | |
27 | dynamicWorkerUsage: false, | |
28 | dynamicWorkerReady: true | |
29 | } | |
30 | ||
2fc5cae3 JB |
31 | /** @inheritDoc */ |
32 | public constructor ( | |
33 | pool: IPool<Worker, Data, Response>, | |
34 | opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS | |
35 | ) { | |
36 | super(pool, opts) | |
932fc8be | 37 | this.setTaskStatisticsRequirements(this.opts) |
2fc5cae3 JB |
38 | } |
39 | ||
afc003b2 | 40 | /** @inheritDoc */ |
a6f7f1b4 | 41 | public reset (): boolean { |
ea7a90d3 JB |
42 | return true |
43 | } | |
44 | ||
138d29a8 JB |
45 | /** @inheritDoc */ |
46 | public update (): boolean { | |
db703c75 JB |
47 | return true |
48 | } | |
49 | ||
50 | /** @inheritDoc */ | |
b1aae695 JB |
51 | public choose (): number | undefined { |
52 | const chosenWorkerNodeKey = this.leastUsedNextWorkerNodeKey() | |
53 | this.assignChosenWorkerNodeKey(chosenWorkerNodeKey) | |
54 | return this.nextWorkerNodeKey | |
9b106837 JB |
55 | } |
56 | ||
57 | /** @inheritDoc */ | |
58 | public remove (): boolean { | |
59 | return true | |
60 | } | |
61 | ||
b1aae695 | 62 | private leastUsedNextWorkerNodeKey (): number | undefined { |
f4ff1ce2 | 63 | let minNumberOfTasks = Infinity |
b1aae695 | 64 | let chosenWorkerNodeKey: number | undefined |
08f3f44c | 65 | for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { |
465b2940 | 66 | const workerTaskStatistics = workerNode.usage.tasks |
a4e07f72 | 67 | const workerTasks = |
1c6fe997 JB |
68 | workerTaskStatistics.executed + |
69 | workerTaskStatistics.executing + | |
70 | workerTaskStatistics.queued | |
8990357d | 71 | if (this.isWorkerNodeEligible(workerNodeKey) && workerTasks === 0) { |
b1aae695 | 72 | chosenWorkerNodeKey = workerNodeKey |
5ea80606 | 73 | break |
19dbc45b | 74 | } else if ( |
8990357d | 75 | this.isWorkerNodeEligible(workerNodeKey) && |
19dbc45b JB |
76 | workerTasks < minNumberOfTasks |
77 | ) { | |
f4ff1ce2 | 78 | minNumberOfTasks = workerTasks |
b1aae695 | 79 | chosenWorkerNodeKey = workerNodeKey |
bdaf31cd JB |
80 | } |
81 | } | |
b1aae695 | 82 | return chosenWorkerNodeKey |
97a2abc3 | 83 | } |
bdaf31cd | 84 | } |