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