refactor: apply stricter strategy design pattern requirements on worker
[poolifier.git] / src / pools / selection-strategies / selection-strategies-types.ts
1 import type { IPoolInternal } from '../pool-internal'
2 import type { IPoolWorker } from '../pool-worker'
3
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 /**
13 * Less used worker selection strategy.
14 */
15 LESS_USED: 'LESS_USED',
16 /**
17 * Less busy worker selection strategy.
18 */
19 LESS_BUSY: 'LESS_BUSY',
20 /**
21 * Fair share worker selection strategy.
22 */
23 FAIR_SHARE: 'FAIR_SHARE',
24 /**
25 * Weighted round robin worker selection strategy.
26 */
27 WEIGHTED_ROUND_ROBIN: 'WEIGHTED_ROUND_ROBIN'
28 } as const)
29
30 /**
31 * Worker choice strategy.
32 */
33 export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
34
35 /**
36 * Pool worker tasks usage statistics requirements.
37 */
38 export interface RequiredStatistics {
39 runTime: boolean
40 avgRunTime: boolean
41 }
42
43 /**
44 * Worker choice strategy interface.
45 */
46 export interface IWorkerChoiceStrategy<
47 Worker extends IPoolWorker,
48 Data = unknown,
49 Response = unknown
50 > {
51 /**
52 * The pool instance.
53 * @readonly
54 */
55 readonly pool: IPoolInternal<Worker, Data, Response>
56 /**
57 * Is the pool attached to the strategy dynamic?.
58 * @readonly
59 */
60 readonly isDynamicPool: boolean
61 /**
62 * Required pool tasks usage statistics.
63 * @readonly
64 */
65 readonly requiredStatistics: RequiredStatistics
66 /**
67 * Resets strategy internals (counters, statistics, etc.).
68 */
69 reset: () => boolean
70 /**
71 * Chooses a worker in the pool and returns its key.
72 */
73 choose: () => number
74 /**
75 * Removes a worker reference from strategy internals.
76 *
77 * @param workerKey - The worker key.
78 */
79 remove: (workerKey: number) => boolean
80 }