poolifier 2.5.3 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -25
- package/lib/index.d.ts +8 -9
- package/lib/index.js +1 -1
- package/lib/index.mjs +1 -1
- package/lib/pools/abstract-pool.d.ts +10 -16
- package/lib/pools/cluster/fixed.d.ts +1 -3
- package/lib/pools/pool.d.ts +3 -1
- package/lib/pools/selection-strategies/abstract-worker-choice-strategy.d.ts +19 -10
- package/lib/pools/selection-strategies/fair-share-worker-choice-strategy.d.ts +2 -2
- package/lib/pools/selection-strategies/least-busy-worker-choice-strategy.d.ts +2 -2
- package/lib/pools/selection-strategies/least-elu-worker-choice-strategy.d.ts +25 -0
- package/lib/pools/selection-strategies/selection-strategies-types.d.ts +66 -22
- package/lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.d.ts +2 -2
- package/lib/pools/selection-strategies/worker-choice-strategy-context.d.ts +4 -4
- package/lib/pools/thread/dynamic.d.ts +3 -3
- package/lib/pools/thread/fixed.d.ts +14 -4
- package/lib/pools/worker.d.ts +56 -28
- package/lib/utility-types.d.ts +50 -45
- package/lib/worker/abstract-worker.d.ts +10 -3
- package/lib/worker/cluster-worker.d.ts +2 -1
- package/lib/worker/thread-worker.d.ts +2 -1
- package/lib/worker/worker-functions.d.ts +33 -0
- package/lib/worker/worker-options.d.ts +4 -4
- package/package.json +9 -9
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { IPool } from '../pool';
|
|
2
2
|
import type { IWorker } from '../worker';
|
|
3
3
|
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy';
|
|
4
|
-
import type
|
|
4
|
+
import { type IWorkerChoiceStrategy, type TaskStatisticsRequirements, type WorkerChoiceStrategyOptions } from './selection-strategies-types';
|
|
5
5
|
/**
|
|
6
6
|
* Selects the next worker with a fair share scheduling algorithm.
|
|
7
7
|
* Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing.
|
|
@@ -12,7 +12,7 @@ import type { IWorkerChoiceStrategy, RequiredStatistics, WorkerChoiceStrategyOpt
|
|
|
12
12
|
*/
|
|
13
13
|
export declare class FairShareWorkerChoiceStrategy<Worker extends IWorker, Data = unknown, Response = unknown> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> implements IWorkerChoiceStrategy {
|
|
14
14
|
/** @inheritDoc */
|
|
15
|
-
readonly
|
|
15
|
+
readonly taskStatisticsRequirements: TaskStatisticsRequirements;
|
|
16
16
|
/**
|
|
17
17
|
* Workers' virtual task end execution timestamp.
|
|
18
18
|
*/
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { IPool } from '../pool';
|
|
2
2
|
import type { IWorker } from '../worker';
|
|
3
3
|
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy';
|
|
4
|
-
import type { IWorkerChoiceStrategy,
|
|
4
|
+
import type { IWorkerChoiceStrategy, TaskStatisticsRequirements, WorkerChoiceStrategyOptions } from './selection-strategies-types';
|
|
5
5
|
/**
|
|
6
6
|
* Selects the least busy worker.
|
|
7
7
|
*
|
|
@@ -11,7 +11,7 @@ import type { IWorkerChoiceStrategy, RequiredStatistics, WorkerChoiceStrategyOpt
|
|
|
11
11
|
*/
|
|
12
12
|
export declare class LeastBusyWorkerChoiceStrategy<Worker extends IWorker, Data = unknown, Response = unknown> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> implements IWorkerChoiceStrategy {
|
|
13
13
|
/** @inheritDoc */
|
|
14
|
-
readonly
|
|
14
|
+
readonly taskStatisticsRequirements: TaskStatisticsRequirements;
|
|
15
15
|
/** @inheritDoc */
|
|
16
16
|
constructor(pool: IPool<Worker, Data, Response>, opts?: WorkerChoiceStrategyOptions);
|
|
17
17
|
/** @inheritDoc */
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { IPool } from '../pool';
|
|
2
|
+
import type { IWorker } from '../worker';
|
|
3
|
+
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy';
|
|
4
|
+
import type { IWorkerChoiceStrategy, TaskStatisticsRequirements, WorkerChoiceStrategyOptions } from './selection-strategies-types';
|
|
5
|
+
/**
|
|
6
|
+
* Selects the worker with the least ELU.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam Worker - Type of worker which manages the strategy.
|
|
9
|
+
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
|
|
10
|
+
* @typeParam Response - Type of execution response. This can only be serializable data.
|
|
11
|
+
*/
|
|
12
|
+
export declare class LeastEluWorkerChoiceStrategy<Worker extends IWorker, Data = unknown, Response = unknown> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> implements IWorkerChoiceStrategy {
|
|
13
|
+
/** @inheritDoc */
|
|
14
|
+
readonly taskStatisticsRequirements: TaskStatisticsRequirements;
|
|
15
|
+
/** @inheritDoc */
|
|
16
|
+
constructor(pool: IPool<Worker, Data, Response>, opts?: WorkerChoiceStrategyOptions);
|
|
17
|
+
/** @inheritDoc */
|
|
18
|
+
reset(): boolean;
|
|
19
|
+
/** @inheritDoc */
|
|
20
|
+
update(): boolean;
|
|
21
|
+
/** @inheritDoc */
|
|
22
|
+
choose(): number;
|
|
23
|
+
/** @inheritDoc */
|
|
24
|
+
remove(): boolean;
|
|
25
|
+
}
|
|
@@ -14,6 +14,12 @@ export declare const WorkerChoiceStrategies: Readonly<{
|
|
|
14
14
|
* Least busy worker selection strategy.
|
|
15
15
|
*/
|
|
16
16
|
readonly LEAST_BUSY: "LEAST_BUSY";
|
|
17
|
+
/**
|
|
18
|
+
* Least ELU worker selection strategy.
|
|
19
|
+
*
|
|
20
|
+
* @experimental
|
|
21
|
+
*/
|
|
22
|
+
readonly LEAST_ELU: "LEAST_ELU";
|
|
17
23
|
/**
|
|
18
24
|
* Fair share worker selection strategy.
|
|
19
25
|
*/
|
|
@@ -33,22 +39,53 @@ export declare const WorkerChoiceStrategies: Readonly<{
|
|
|
33
39
|
* Worker choice strategy.
|
|
34
40
|
*/
|
|
35
41
|
export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies;
|
|
42
|
+
/**
|
|
43
|
+
* Enumeration of measurements.
|
|
44
|
+
*/
|
|
45
|
+
export declare const Measurements: Readonly<{
|
|
46
|
+
readonly runTime: "runTime";
|
|
47
|
+
readonly waitTime: "waitTime";
|
|
48
|
+
readonly elu: "elu";
|
|
49
|
+
}>;
|
|
50
|
+
/**
|
|
51
|
+
* Measurement.
|
|
52
|
+
*/
|
|
53
|
+
export type Measurement = keyof typeof Measurements;
|
|
54
|
+
/**
|
|
55
|
+
* Measurement options.
|
|
56
|
+
*/
|
|
57
|
+
export interface MeasurementOptions {
|
|
58
|
+
/**
|
|
59
|
+
* Set measurement median.
|
|
60
|
+
*/
|
|
61
|
+
median: boolean;
|
|
62
|
+
}
|
|
36
63
|
/**
|
|
37
64
|
* Worker choice strategy options.
|
|
38
65
|
*/
|
|
39
66
|
export interface WorkerChoiceStrategyOptions {
|
|
40
67
|
/**
|
|
41
|
-
*
|
|
68
|
+
* Measurement to use for worker choice strategy.
|
|
69
|
+
*/
|
|
70
|
+
measurement?: Measurement;
|
|
71
|
+
/**
|
|
72
|
+
* Runtime options.
|
|
73
|
+
*
|
|
74
|
+
* @defaultValue \{ median: false \}
|
|
75
|
+
*/
|
|
76
|
+
runTime?: MeasurementOptions;
|
|
77
|
+
/**
|
|
78
|
+
* Wait time options.
|
|
42
79
|
*
|
|
43
|
-
* @defaultValue false
|
|
80
|
+
* @defaultValue \{ median: false \}
|
|
44
81
|
*/
|
|
45
|
-
|
|
82
|
+
waitTime?: MeasurementOptions;
|
|
46
83
|
/**
|
|
47
|
-
*
|
|
84
|
+
* Event loop utilization options.
|
|
48
85
|
*
|
|
49
|
-
* @defaultValue false
|
|
86
|
+
* @defaultValue \{ median: false \}
|
|
50
87
|
*/
|
|
51
|
-
|
|
88
|
+
elu?: MeasurementOptions;
|
|
52
89
|
/**
|
|
53
90
|
* Worker weights to use for weighted round robin worker selection strategy.
|
|
54
91
|
* Weight is the tasks maximum average or median runtime in milliseconds.
|
|
@@ -58,44 +95,51 @@ export interface WorkerChoiceStrategyOptions {
|
|
|
58
95
|
weights?: Record<number, number>;
|
|
59
96
|
}
|
|
60
97
|
/**
|
|
61
|
-
*
|
|
98
|
+
* Measurement statistics requirements.
|
|
62
99
|
*
|
|
63
100
|
* @internal
|
|
64
101
|
*/
|
|
65
|
-
export interface
|
|
102
|
+
export interface MeasurementStatisticsRequirements {
|
|
66
103
|
/**
|
|
67
|
-
* Require
|
|
104
|
+
* Require measurement aggregate.
|
|
68
105
|
*/
|
|
69
|
-
|
|
106
|
+
aggregate: boolean;
|
|
70
107
|
/**
|
|
71
|
-
* Require
|
|
108
|
+
* Require measurement average.
|
|
72
109
|
*/
|
|
73
|
-
|
|
110
|
+
average: boolean;
|
|
74
111
|
/**
|
|
75
|
-
* Require
|
|
112
|
+
* Require measurement median.
|
|
76
113
|
*/
|
|
77
|
-
|
|
114
|
+
median: boolean;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Pool worker node worker usage statistics requirements.
|
|
118
|
+
*
|
|
119
|
+
* @internal
|
|
120
|
+
*/
|
|
121
|
+
export interface TaskStatisticsRequirements {
|
|
78
122
|
/**
|
|
79
|
-
*
|
|
123
|
+
* Tasks runtime requirements.
|
|
80
124
|
*/
|
|
81
|
-
|
|
125
|
+
runTime: MeasurementStatisticsRequirements;
|
|
82
126
|
/**
|
|
83
|
-
*
|
|
127
|
+
* Tasks wait time requirements.
|
|
84
128
|
*/
|
|
85
|
-
|
|
129
|
+
waitTime: MeasurementStatisticsRequirements;
|
|
86
130
|
/**
|
|
87
|
-
*
|
|
131
|
+
* Tasks event loop utilization requirements.
|
|
88
132
|
*/
|
|
89
|
-
|
|
133
|
+
elu: MeasurementStatisticsRequirements;
|
|
90
134
|
}
|
|
91
135
|
/**
|
|
92
136
|
* Worker choice strategy interface.
|
|
93
137
|
*/
|
|
94
138
|
export interface IWorkerChoiceStrategy {
|
|
95
139
|
/**
|
|
96
|
-
*
|
|
140
|
+
* Tasks statistics requirements.
|
|
97
141
|
*/
|
|
98
|
-
readonly
|
|
142
|
+
readonly taskStatisticsRequirements: TaskStatisticsRequirements;
|
|
99
143
|
/**
|
|
100
144
|
* Resets strategy internals.
|
|
101
145
|
*
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { IWorker } from '../worker';
|
|
2
2
|
import type { IPool } from '../pool';
|
|
3
3
|
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy';
|
|
4
|
-
import type { IWorkerChoiceStrategy,
|
|
4
|
+
import type { IWorkerChoiceStrategy, TaskStatisticsRequirements, WorkerChoiceStrategyOptions } from './selection-strategies-types';
|
|
5
5
|
/**
|
|
6
6
|
* Selects the next worker with a weighted round robin scheduling algorithm.
|
|
7
7
|
* Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin.
|
|
@@ -12,7 +12,7 @@ import type { IWorkerChoiceStrategy, RequiredStatistics, WorkerChoiceStrategyOpt
|
|
|
12
12
|
*/
|
|
13
13
|
export declare class WeightedRoundRobinWorkerChoiceStrategy<Worker extends IWorker, Data = unknown, Response = unknown> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> implements IWorkerChoiceStrategy {
|
|
14
14
|
/** @inheritDoc */
|
|
15
|
-
readonly
|
|
15
|
+
readonly taskStatisticsRequirements: TaskStatisticsRequirements;
|
|
16
16
|
/**
|
|
17
17
|
* Worker node id where the current task will be submitted.
|
|
18
18
|
*/
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { IPool } from '../pool';
|
|
2
2
|
import type { IWorker } from '../worker';
|
|
3
|
-
import type {
|
|
3
|
+
import type { TaskStatisticsRequirements, WorkerChoiceStrategy, WorkerChoiceStrategyOptions } from './selection-strategies-types';
|
|
4
4
|
/**
|
|
5
5
|
* The worker choice strategy context.
|
|
6
6
|
*
|
|
@@ -20,11 +20,11 @@ export declare class WorkerChoiceStrategyContext<Worker extends IWorker, Data =
|
|
|
20
20
|
*/
|
|
21
21
|
constructor(pool: IPool<Worker, Data, Response>, workerChoiceStrategy?: WorkerChoiceStrategy, opts?: WorkerChoiceStrategyOptions);
|
|
22
22
|
/**
|
|
23
|
-
* Gets the worker choice strategy in the context
|
|
23
|
+
* Gets the worker choice strategy task statistics requirements in the context.
|
|
24
24
|
*
|
|
25
|
-
* @returns The
|
|
25
|
+
* @returns The task statistics requirements.
|
|
26
26
|
*/
|
|
27
|
-
|
|
27
|
+
getTaskStatisticsRequirements(): TaskStatisticsRequirements;
|
|
28
28
|
/**
|
|
29
29
|
* Sets the worker choice strategy to use in the context.
|
|
30
30
|
*
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import { FixedThreadPool, type
|
|
1
|
+
import { type PoolType } from '../pool';
|
|
2
|
+
import { FixedThreadPool, type ThreadPoolOptions } from './fixed';
|
|
3
3
|
/**
|
|
4
4
|
* A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads.
|
|
5
5
|
*
|
|
@@ -21,7 +21,7 @@ export declare class DynamicThreadPool<Data = unknown, Response = unknown> exten
|
|
|
21
21
|
* @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
|
|
22
22
|
* @param opts - Options for this dynamic thread pool.
|
|
23
23
|
*/
|
|
24
|
-
constructor(min: number, max: number, filePath: string, opts?:
|
|
24
|
+
constructor(min: number, max: number, filePath: string, opts?: ThreadPoolOptions);
|
|
25
25
|
/** @inheritDoc */
|
|
26
26
|
protected get type(): PoolType;
|
|
27
27
|
/** @inheritDoc */
|
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { MessageChannel, Worker } from 'node:worker_threads';
|
|
2
|
+
import { MessageChannel, Worker, type WorkerOptions } from 'node:worker_threads';
|
|
3
3
|
import type { Draft, MessageValue } from '../../utility-types';
|
|
4
4
|
import { AbstractPool } from '../abstract-pool';
|
|
5
5
|
import { type PoolOptions, type PoolType, type WorkerType } from '../pool';
|
|
6
|
+
/**
|
|
7
|
+
* Options for a poolifier thread pool.
|
|
8
|
+
*/
|
|
9
|
+
export interface ThreadPoolOptions extends PoolOptions<Worker> {
|
|
10
|
+
/**
|
|
11
|
+
* Worker options.
|
|
12
|
+
*
|
|
13
|
+
* @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
|
|
14
|
+
*/
|
|
15
|
+
workerOptions?: WorkerOptions;
|
|
16
|
+
}
|
|
6
17
|
/**
|
|
7
18
|
* A thread worker with message channels for communication between main thread and thread worker.
|
|
8
19
|
*/
|
|
@@ -12,14 +23,13 @@ export type ThreadWorkerWithMessageChannel = Worker & Draft<MessageChannel>;
|
|
|
12
23
|
*
|
|
13
24
|
* It is possible to perform tasks in sync or asynchronous mode as you prefer.
|
|
14
25
|
*
|
|
15
|
-
* This pool selects the threads in a round robin fashion.
|
|
16
|
-
*
|
|
17
26
|
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
|
|
18
27
|
* @typeParam Response - Type of execution response. This can only be serializable data.
|
|
19
28
|
* @author [Alessandro Pio Ardizio](https://github.com/pioardi)
|
|
20
29
|
* @since 0.0.1
|
|
21
30
|
*/
|
|
22
31
|
export declare class FixedThreadPool<Data = unknown, Response = unknown> extends AbstractPool<ThreadWorkerWithMessageChannel, Data, Response> {
|
|
32
|
+
protected readonly opts: ThreadPoolOptions;
|
|
23
33
|
/**
|
|
24
34
|
* Constructs a new poolifier fixed thread pool.
|
|
25
35
|
*
|
|
@@ -27,7 +37,7 @@ export declare class FixedThreadPool<Data = unknown, Response = unknown> extends
|
|
|
27
37
|
* @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
|
|
28
38
|
* @param opts - Options for this fixed thread pool.
|
|
29
39
|
*/
|
|
30
|
-
constructor(numberOfThreads: number, filePath: string, opts?:
|
|
40
|
+
constructor(numberOfThreads: number, filePath: string, opts?: ThreadPoolOptions);
|
|
31
41
|
/** @inheritDoc */
|
|
32
42
|
protected isMain(): boolean;
|
|
33
43
|
/** @inheritDoc */
|
package/lib/pools/worker.d.ts
CHANGED
|
@@ -32,64 +32,92 @@ export interface Task<Data = unknown> {
|
|
|
32
32
|
*/
|
|
33
33
|
readonly data?: Data;
|
|
34
34
|
/**
|
|
35
|
-
*
|
|
35
|
+
* Timestamp.
|
|
36
36
|
*/
|
|
37
|
-
readonly
|
|
37
|
+
readonly timestamp?: number;
|
|
38
38
|
/**
|
|
39
39
|
* Message UUID.
|
|
40
40
|
*/
|
|
41
41
|
readonly id?: string;
|
|
42
42
|
}
|
|
43
43
|
/**
|
|
44
|
-
*
|
|
44
|
+
* Measurement statistics.
|
|
45
45
|
*
|
|
46
46
|
* @internal
|
|
47
47
|
*/
|
|
48
|
-
export interface
|
|
48
|
+
export interface MeasurementStatistics {
|
|
49
49
|
/**
|
|
50
|
-
*
|
|
50
|
+
* Measurement aggregate.
|
|
51
51
|
*/
|
|
52
|
-
|
|
52
|
+
aggregate: number;
|
|
53
53
|
/**
|
|
54
|
-
*
|
|
54
|
+
* Measurement average.
|
|
55
55
|
*/
|
|
56
|
-
|
|
56
|
+
average: number;
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
58
|
+
* Measurement median.
|
|
59
59
|
*/
|
|
60
|
-
|
|
60
|
+
median: number;
|
|
61
61
|
/**
|
|
62
|
-
*
|
|
62
|
+
* Measurement history.
|
|
63
63
|
*/
|
|
64
|
-
|
|
64
|
+
history: CircularArray<number>;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Event loop utilization measurement statistics.
|
|
68
|
+
*
|
|
69
|
+
* @internal
|
|
70
|
+
*/
|
|
71
|
+
export interface EventLoopUtilizationMeasurementStatistics {
|
|
72
|
+
idle: MeasurementStatistics;
|
|
73
|
+
active: MeasurementStatistics;
|
|
74
|
+
utilization: number;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Task statistics.
|
|
78
|
+
*
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
export interface TaskStatistics {
|
|
82
|
+
/**
|
|
83
|
+
* Number of executed tasks.
|
|
84
|
+
*/
|
|
85
|
+
executed: number;
|
|
65
86
|
/**
|
|
66
|
-
*
|
|
87
|
+
* Number of executing tasks.
|
|
67
88
|
*/
|
|
68
|
-
|
|
89
|
+
executing: number;
|
|
69
90
|
/**
|
|
70
|
-
*
|
|
91
|
+
* Number of queued tasks.
|
|
71
92
|
*/
|
|
72
|
-
|
|
93
|
+
readonly queued: number;
|
|
73
94
|
/**
|
|
74
|
-
*
|
|
95
|
+
* Number of failed tasks.
|
|
75
96
|
*/
|
|
76
|
-
|
|
97
|
+
failed: number;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Worker usage statistics.
|
|
101
|
+
*
|
|
102
|
+
* @internal
|
|
103
|
+
*/
|
|
104
|
+
export interface WorkerUsage {
|
|
77
105
|
/**
|
|
78
|
-
* Tasks
|
|
106
|
+
* Tasks statistics.
|
|
79
107
|
*/
|
|
80
|
-
|
|
108
|
+
tasks: TaskStatistics;
|
|
81
109
|
/**
|
|
82
|
-
*
|
|
110
|
+
* Tasks runtime statistics.
|
|
83
111
|
*/
|
|
84
|
-
|
|
112
|
+
runTime: MeasurementStatistics;
|
|
85
113
|
/**
|
|
86
|
-
*
|
|
114
|
+
* Tasks wait time statistics.
|
|
87
115
|
*/
|
|
88
|
-
|
|
116
|
+
waitTime: MeasurementStatistics;
|
|
89
117
|
/**
|
|
90
|
-
*
|
|
118
|
+
* Tasks event loop utilization statistics.
|
|
91
119
|
*/
|
|
92
|
-
|
|
120
|
+
elu: EventLoopUtilizationMeasurementStatistics;
|
|
93
121
|
}
|
|
94
122
|
/**
|
|
95
123
|
* Worker interface.
|
|
@@ -123,9 +151,9 @@ export interface WorkerNode<Worker extends IWorker, Data = unknown> {
|
|
|
123
151
|
*/
|
|
124
152
|
readonly worker: Worker;
|
|
125
153
|
/**
|
|
126
|
-
* Worker node
|
|
154
|
+
* Worker node worker usage statistics.
|
|
127
155
|
*/
|
|
128
|
-
|
|
156
|
+
workerUsage: WorkerUsage;
|
|
129
157
|
/**
|
|
130
158
|
* Worker node tasks queue.
|
|
131
159
|
*/
|
package/lib/utility-types.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
|
+
/// <reference types="node" />
|
|
3
4
|
import type { Worker as ClusterWorker } from 'node:cluster';
|
|
4
5
|
import type { MessagePort } from 'node:worker_threads';
|
|
6
|
+
import type { EventLoopUtilization } from 'node:perf_hooks';
|
|
5
7
|
import type { KillBehavior } from './worker/worker-options';
|
|
6
8
|
import type { IWorker, Task } from './pools/worker';
|
|
7
9
|
/**
|
|
@@ -13,71 +15,74 @@ export type Draft<T> = {
|
|
|
13
15
|
-readonly [P in keyof T]?: T[P];
|
|
14
16
|
};
|
|
15
17
|
/**
|
|
16
|
-
*
|
|
18
|
+
* Task error.
|
|
17
19
|
*
|
|
18
20
|
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
|
|
19
|
-
* @typeParam MainWorker - Type of main worker.
|
|
20
|
-
* @internal
|
|
21
21
|
*/
|
|
22
|
-
export interface
|
|
23
|
-
/**
|
|
24
|
-
* Kill code.
|
|
25
|
-
*/
|
|
26
|
-
readonly kill?: KillBehavior | 1;
|
|
22
|
+
export interface TaskError<Data = unknown> {
|
|
27
23
|
/**
|
|
28
|
-
*
|
|
24
|
+
* Error message.
|
|
29
25
|
*/
|
|
30
|
-
|
|
26
|
+
message: string;
|
|
31
27
|
/**
|
|
32
|
-
*
|
|
28
|
+
* Data passed to the worker triggering the error.
|
|
33
29
|
*/
|
|
34
|
-
|
|
30
|
+
data?: Data;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Task performance.
|
|
34
|
+
*/
|
|
35
|
+
export interface TaskPerformance {
|
|
35
36
|
/**
|
|
36
|
-
*
|
|
37
|
+
* Task performance timestamp.
|
|
37
38
|
*/
|
|
38
|
-
|
|
39
|
+
timestamp: number;
|
|
39
40
|
/**
|
|
40
|
-
*
|
|
41
|
+
* Task runtime.
|
|
41
42
|
*/
|
|
42
|
-
|
|
43
|
+
runTime?: number;
|
|
43
44
|
/**
|
|
44
|
-
*
|
|
45
|
+
* Task event loop utilization.
|
|
45
46
|
*/
|
|
46
|
-
|
|
47
|
+
elu?: EventLoopUtilization;
|
|
47
48
|
}
|
|
48
49
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
|
|
52
|
-
* @typeParam Response - Type of execution response. This can only be serializable data.
|
|
53
|
-
*/
|
|
54
|
-
export type WorkerSyncFunction<Data = unknown, Response = unknown> = (data?: Data) => Response;
|
|
55
|
-
/**
|
|
56
|
-
* Worker asynchronous function that can be executed.
|
|
57
|
-
* This function must return a promise.
|
|
58
|
-
*
|
|
59
|
-
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
|
|
60
|
-
* @typeParam Response - Type of execution response. This can only be serializable data.
|
|
61
|
-
*/
|
|
62
|
-
export type WorkerAsyncFunction<Data = unknown, Response = unknown> = (data?: Data) => Promise<Response>;
|
|
63
|
-
/**
|
|
64
|
-
* Worker function that can be executed.
|
|
65
|
-
* This function can be synchronous or asynchronous.
|
|
66
|
-
*
|
|
67
|
-
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
|
|
68
|
-
* @typeParam Response - Type of execution response. This can only be serializable data.
|
|
50
|
+
* Performance statistics computation.
|
|
69
51
|
*/
|
|
70
|
-
export
|
|
52
|
+
export interface WorkerStatistics {
|
|
53
|
+
runTime: boolean;
|
|
54
|
+
elu: boolean;
|
|
55
|
+
}
|
|
71
56
|
/**
|
|
72
|
-
*
|
|
73
|
-
* This object can contain synchronous or asynchronous functions.
|
|
74
|
-
* The key is the name of the function.
|
|
75
|
-
* The value is the function itself.
|
|
57
|
+
* Message object that is passed between main worker and worker.
|
|
76
58
|
*
|
|
59
|
+
* @typeParam MessageData - Type of data sent to and/or from the worker. This can only be serializable data.
|
|
77
60
|
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
|
|
78
|
-
* @typeParam
|
|
61
|
+
* @typeParam MainWorker - Type of main worker.
|
|
62
|
+
* @internal
|
|
79
63
|
*/
|
|
80
|
-
export
|
|
64
|
+
export interface MessageValue<MessageData = unknown, Data = unknown, MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort> extends Task<MessageData> {
|
|
65
|
+
/**
|
|
66
|
+
* Kill code.
|
|
67
|
+
*/
|
|
68
|
+
readonly kill?: KillBehavior | 1;
|
|
69
|
+
/**
|
|
70
|
+
* Task error.
|
|
71
|
+
*/
|
|
72
|
+
readonly taskError?: TaskError<Data>;
|
|
73
|
+
/**
|
|
74
|
+
* Task performance.
|
|
75
|
+
*/
|
|
76
|
+
readonly taskPerformance?: TaskPerformance;
|
|
77
|
+
/**
|
|
78
|
+
* Reference to main worker.
|
|
79
|
+
*/
|
|
80
|
+
readonly parent?: MainWorker;
|
|
81
|
+
/**
|
|
82
|
+
* Whether to compute the given statistics or not.
|
|
83
|
+
*/
|
|
84
|
+
readonly statistics?: WorkerStatistics;
|
|
85
|
+
}
|
|
81
86
|
/**
|
|
82
87
|
* An object holding the execution response promise resolve/reject callbacks.
|
|
83
88
|
*
|
|
@@ -5,8 +5,9 @@
|
|
|
5
5
|
import { AsyncResource } from 'node:async_hooks';
|
|
6
6
|
import type { Worker } from 'node:cluster';
|
|
7
7
|
import type { MessagePort } from 'node:worker_threads';
|
|
8
|
-
import type { MessageValue,
|
|
8
|
+
import type { MessageValue, WorkerStatistics } from '../utility-types';
|
|
9
9
|
import { type WorkerOptions } from './worker-options';
|
|
10
|
+
import type { TaskFunctions, WorkerAsyncFunction, WorkerFunction, WorkerSyncFunction } from './worker-functions';
|
|
10
11
|
/**
|
|
11
12
|
* Base class that implements some shared logic for all poolifier workers.
|
|
12
13
|
*
|
|
@@ -26,6 +27,10 @@ export declare abstract class AbstractWorker<MainWorker extends Worker | Message
|
|
|
26
27
|
* Timestamp of the last task processed by this worker.
|
|
27
28
|
*/
|
|
28
29
|
protected lastTaskTimestamp: number;
|
|
30
|
+
/**
|
|
31
|
+
* Performance statistics computation.
|
|
32
|
+
*/
|
|
33
|
+
protected statistics: WorkerStatistics;
|
|
29
34
|
/**
|
|
30
35
|
* Handler id of the `aliveInterval` worker alive check.
|
|
31
36
|
*/
|
|
@@ -52,7 +57,7 @@ export declare abstract class AbstractWorker<MainWorker extends Worker | Message
|
|
|
52
57
|
*
|
|
53
58
|
* @param message - Message received.
|
|
54
59
|
*/
|
|
55
|
-
protected messageListener(message: MessageValue<Data, MainWorker>): void;
|
|
60
|
+
protected messageListener(message: MessageValue<Data, Data, MainWorker>): void;
|
|
56
61
|
/**
|
|
57
62
|
* Returns the main worker.
|
|
58
63
|
*
|
|
@@ -64,7 +69,7 @@ export declare abstract class AbstractWorker<MainWorker extends Worker | Message
|
|
|
64
69
|
*
|
|
65
70
|
* @param message - The response message.
|
|
66
71
|
*/
|
|
67
|
-
protected abstract sendToMainWorker(message: MessageValue<Response>): void;
|
|
72
|
+
protected abstract sendToMainWorker(message: MessageValue<Response, Data>): void;
|
|
68
73
|
/**
|
|
69
74
|
* Checks if the worker should be terminated, because its living too long.
|
|
70
75
|
*/
|
|
@@ -96,4 +101,6 @@ export declare abstract class AbstractWorker<MainWorker extends Worker | Message
|
|
|
96
101
|
* @param name - Name of the function that will be returned.
|
|
97
102
|
*/
|
|
98
103
|
private getTaskFunction;
|
|
104
|
+
private beginTaskPerformance;
|
|
105
|
+
private endTaskPerformance;
|
|
99
106
|
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { type Worker } from 'node:cluster';
|
|
3
|
-
import type { MessageValue
|
|
3
|
+
import type { MessageValue } from '../utility-types';
|
|
4
4
|
import { AbstractWorker } from './abstract-worker';
|
|
5
5
|
import type { WorkerOptions } from './worker-options';
|
|
6
|
+
import type { TaskFunctions, WorkerFunction } from './worker-functions';
|
|
6
7
|
/**
|
|
7
8
|
* A cluster worker used by a poolifier `ClusterPool`.
|
|
8
9
|
*
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { type MessagePort } from 'node:worker_threads';
|
|
3
|
-
import type { MessageValue
|
|
3
|
+
import type { MessageValue } from '../utility-types';
|
|
4
4
|
import { AbstractWorker } from './abstract-worker';
|
|
5
5
|
import type { WorkerOptions } from './worker-options';
|
|
6
|
+
import type { TaskFunctions, WorkerFunction } from './worker-functions';
|
|
6
7
|
/**
|
|
7
8
|
* A thread worker used by a poolifier `ThreadPool`.
|
|
8
9
|
*
|