refactor: apply stricter strategy design pattern requirements on worker
[poolifier.git] / src / pools / selection-strategies / selection-strategies-types.ts
CommitLineData
3300e7bc
JB
1import type { IPoolInternal } from '../pool-internal'
2import type { IPoolWorker } from '../pool-worker'
3
bdaf31cd
JB
4/**
5 * Enumeration of worker choice strategies.
6 */
7export 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 */
33export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
34
10fcfaf4 35/**
9cd39dd4 36 * Pool worker tasks usage statistics requirements.
10fcfaf4 37 */
78cea37e 38export 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
46export 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>
bdaf31cd
JB
56 /**
57 * Is the pool attached to the strategy dynamic?.
3300e7bc 58 * @readonly
bdaf31cd 59 */
ea7a90d3 60 readonly isDynamicPool: boolean
10fcfaf4 61 /**
ea7a90d3 62 * Required pool tasks usage statistics.
3300e7bc 63 * @readonly
10fcfaf4 64 */
ea7a90d3
JB
65 readonly requiredStatistics: RequiredStatistics
66 /**
a6f7f1b4 67 * Resets strategy internals (counters, statistics, etc.).
ea7a90d3 68 */
78cea37e 69 reset: () => boolean
bdaf31cd 70 /**
c923ce56 71 * Chooses a worker in the pool and returns its key.
bdaf31cd 72 */
c923ce56 73 choose: () => number
97a2abc3
JB
74 /**
75 * Removes a worker reference from strategy internals.
76 *
77 * @param workerKey - The worker key.
78 */
79 remove: (workerKey: number) => boolean
bdaf31cd 80}