Commit | Line | Data |
---|---|---|
3300e7bc JB |
1 | import type { IPoolInternal } from '../pool-internal' |
2 | import type { IPoolWorker } from '../pool-worker' | |
3 | ||
bdaf31cd JB |
4 | /** |
5 | * Enumeration of worker choice strategies. | |
6 | */ | |
7 | export const WorkerChoiceStrategies = Object.freeze({ | |
8 | /** | |
9 | * Round robin worker selection strategy. | |
10 | */ | |
11 | ROUND_ROBIN: 'ROUND_ROBIN', | |
12 | /** | |
737c6d97 | 13 | * Less used worker selection strategy. |
bdaf31cd | 14 | */ |
737c6d97 | 15 | LESS_USED: 'LESS_USED', |
168c526f JB |
16 | /** |
17 | * Less busy worker selection strategy. | |
18 | */ | |
19 | LESS_BUSY: 'LESS_BUSY', | |
23ff945a JB |
20 | /** |
21 | * Fair share worker selection strategy. | |
22 | */ | |
23 | FAIR_SHARE: 'FAIR_SHARE', | |
b3432a63 JB |
24 | /** |
25 | * Weighted round robin worker selection strategy. | |
26 | */ | |
27 | WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN' | |
bdaf31cd JB |
28 | } as const) |
29 | ||
30 | /** | |
31 | * Worker choice strategy. | |
32 | */ | |
33 | export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies | |
34 | ||
10fcfaf4 | 35 | /** |
9cd39dd4 | 36 | * Pool worker tasks usage statistics requirements. |
10fcfaf4 | 37 | */ |
78cea37e | 38 | export interface RequiredStatistics { |
10fcfaf4 | 39 | runTime: boolean |
c6bd2650 | 40 | avgRunTime: boolean |
10fcfaf4 JB |
41 | } |
42 | ||
bdaf31cd JB |
43 | /** |
44 | * Worker choice strategy interface. | |
bdaf31cd | 45 | */ |
3300e7bc JB |
46 | export interface IWorkerChoiceStrategy< |
47 | Worker extends IPoolWorker, | |
48 | Data = unknown, | |
49 | Response = unknown | |
50 | > { | |
51 | /** | |
52 | * The pool instance. | |
3300e7bc JB |
53 | */ |
54 | readonly pool: IPoolInternal<Worker, Data, Response> | |
bdaf31cd | 55 | /** |
6533c3e6 | 56 | * Is the pool bound to the strategy dynamic?. |
bdaf31cd | 57 | */ |
ea7a90d3 | 58 | readonly isDynamicPool: boolean |
10fcfaf4 | 59 | /** |
ea7a90d3 | 60 | * Required pool tasks usage statistics. |
10fcfaf4 | 61 | */ |
ea7a90d3 JB |
62 | readonly requiredStatistics: RequiredStatistics |
63 | /** | |
a6f7f1b4 | 64 | * Resets strategy internals (counters, statistics, etc.). |
ea7a90d3 | 65 | */ |
78cea37e | 66 | reset: () => boolean |
bdaf31cd | 67 | /** |
c923ce56 | 68 | * Chooses a worker in the pool and returns its key. |
bdaf31cd | 69 | */ |
c923ce56 | 70 | choose: () => number |
97a2abc3 JB |
71 | /** |
72 | * Removes a worker reference from strategy internals. | |
73 | * | |
74 | * @param workerKey - The worker key. | |
75 | */ | |
76 | remove: (workerKey: number) => boolean | |
bdaf31cd | 77 | } |