poolifier 2.3.7 → 2.3.9
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/lib/index.d.ts +14 -881
- package/lib/index.js +1 -1
- package/lib/index.mjs +1 -0
- package/lib/pools/abstract-pool.d.ts +219 -0
- package/lib/pools/cluster/dynamic.d.ts +30 -0
- package/lib/pools/cluster/fixed.d.ts +64 -0
- package/lib/pools/pool-internal.d.ts +85 -0
- package/lib/pools/pool-worker.d.ts +35 -0
- package/lib/pools/pool.d.ts +73 -0
- package/lib/pools/selection-strategies/abstract-worker-choice-strategy.d.ts +27 -0
- package/lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy.d.ts +27 -0
- package/lib/pools/selection-strategies/fair-share-worker-choice-strategy.d.ts +29 -0
- package/lib/pools/selection-strategies/less-recently-used-worker-choice-strategy.d.ts +15 -0
- package/lib/pools/selection-strategies/round-robin-worker-choice-strategy.d.ts +19 -0
- package/lib/pools/selection-strategies/selection-strategies-types.d.ts +55 -0
- package/lib/pools/selection-strategies/selection-strategies-utils.d.ts +11 -0
- package/lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.d.ts +43 -0
- package/lib/pools/selection-strategies/worker-choice-strategy-context.d.ts +48 -0
- package/lib/pools/thread/dynamic.d.ts +31 -0
- package/lib/pools/thread/fixed.d.ts +48 -0
- package/lib/utility-types.d.ts +63 -0
- package/lib/utils.d.ts +4 -0
- package/lib/worker/abstract-worker.d.ts +86 -0
- package/lib/worker/cluster-worker.d.ts +32 -0
- package/lib/worker/thread-worker.d.ts +30 -0
- package/lib/worker/worker-options.d.ts +61 -0
- package/package.json +52 -35
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { IPoolWorker } from '../pool-worker';
|
|
2
|
+
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy';
|
|
3
|
+
import type { RequiredStatistics } from './selection-strategies-types';
|
|
4
|
+
/**
|
|
5
|
+
* Selects the next worker with a fair share scheduling algorithm.
|
|
6
|
+
* Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing.
|
|
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 response of execution. This can only be serializable data.
|
|
11
|
+
*/
|
|
12
|
+
export declare class FairShareWorkerChoiceStrategy<Worker extends IPoolWorker, Data, Response> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
|
|
13
|
+
/** {@inheritDoc} */
|
|
14
|
+
readonly requiredStatistics: RequiredStatistics;
|
|
15
|
+
/**
|
|
16
|
+
* Worker last virtual task execution timestamp.
|
|
17
|
+
*/
|
|
18
|
+
private readonly workerLastVirtualTaskTimestamp;
|
|
19
|
+
/** {@inheritDoc} */
|
|
20
|
+
reset(): boolean;
|
|
21
|
+
/** {@inheritDoc} */
|
|
22
|
+
choose(): Worker;
|
|
23
|
+
/**
|
|
24
|
+
* Computes worker last virtual task timestamp.
|
|
25
|
+
*
|
|
26
|
+
* @param worker - The worker.
|
|
27
|
+
*/
|
|
28
|
+
private computeWorkerLastVirtualTaskTimestamp;
|
|
29
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { IPoolWorker } from '../pool-worker';
|
|
2
|
+
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy';
|
|
3
|
+
/**
|
|
4
|
+
* Selects the less recently used worker.
|
|
5
|
+
*
|
|
6
|
+
* @typeParam Worker - Type of worker which manages the strategy.
|
|
7
|
+
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
|
|
8
|
+
* @typeParam Response - Type of response of execution. This can only be serializable data.
|
|
9
|
+
*/
|
|
10
|
+
export declare class LessRecentlyUsedWorkerChoiceStrategy<Worker extends IPoolWorker, Data, Response> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
|
|
11
|
+
/** {@inheritDoc} */
|
|
12
|
+
reset(): boolean;
|
|
13
|
+
/** {@inheritDoc} */
|
|
14
|
+
choose(): Worker;
|
|
15
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { IPoolWorker } from '../pool-worker';
|
|
2
|
+
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy';
|
|
3
|
+
/**
|
|
4
|
+
* Selects the next worker in a round robin fashion.
|
|
5
|
+
*
|
|
6
|
+
* @typeParam Worker - Type of worker which manages the strategy.
|
|
7
|
+
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
|
|
8
|
+
* @typeParam Response - Type of response of execution. This can only be serializable data.
|
|
9
|
+
*/
|
|
10
|
+
export declare class RoundRobinWorkerChoiceStrategy<Worker extends IPoolWorker, Data, Response> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
|
|
11
|
+
/**
|
|
12
|
+
* Index for the next worker.
|
|
13
|
+
*/
|
|
14
|
+
private nextWorkerIndex;
|
|
15
|
+
/** {@inheritDoc} */
|
|
16
|
+
reset(): boolean;
|
|
17
|
+
/** {@inheritDoc} */
|
|
18
|
+
choose(): Worker;
|
|
19
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { IPoolWorker } from '../pool-worker';
|
|
2
|
+
/**
|
|
3
|
+
* Enumeration of worker choice strategies.
|
|
4
|
+
*/
|
|
5
|
+
export declare const WorkerChoiceStrategies: Readonly<{
|
|
6
|
+
/**
|
|
7
|
+
* Round robin worker selection strategy.
|
|
8
|
+
*/
|
|
9
|
+
readonly ROUND_ROBIN: "ROUND_ROBIN";
|
|
10
|
+
/**
|
|
11
|
+
* Less recently used worker selection strategy.
|
|
12
|
+
*/
|
|
13
|
+
readonly LESS_RECENTLY_USED: "LESS_RECENTLY_USED";
|
|
14
|
+
/**
|
|
15
|
+
* Fair share worker selection strategy.
|
|
16
|
+
*/
|
|
17
|
+
readonly FAIR_SHARE: "FAIR_SHARE";
|
|
18
|
+
/**
|
|
19
|
+
* Weighted round robin worker selection strategy.
|
|
20
|
+
*/
|
|
21
|
+
readonly WEIGHTED_ROUND_ROBIN: "WEIGHTED_ROUND_ROBIN";
|
|
22
|
+
}>;
|
|
23
|
+
/**
|
|
24
|
+
* Worker choice strategy.
|
|
25
|
+
*/
|
|
26
|
+
export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies;
|
|
27
|
+
/**
|
|
28
|
+
* Pool tasks usage statistics requirements.
|
|
29
|
+
*/
|
|
30
|
+
export interface RequiredStatistics {
|
|
31
|
+
runTime: boolean;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Worker choice strategy interface.
|
|
35
|
+
*
|
|
36
|
+
* @typeParam Worker - Type of worker which manages the strategy.
|
|
37
|
+
*/
|
|
38
|
+
export interface IWorkerChoiceStrategy<Worker extends IPoolWorker> {
|
|
39
|
+
/**
|
|
40
|
+
* Is the pool attached to the strategy dynamic?.
|
|
41
|
+
*/
|
|
42
|
+
readonly isDynamicPool: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Required pool tasks usage statistics.
|
|
45
|
+
*/
|
|
46
|
+
readonly requiredStatistics: RequiredStatistics;
|
|
47
|
+
/**
|
|
48
|
+
* Resets strategy internals (counters, statistics, etc.).
|
|
49
|
+
*/
|
|
50
|
+
reset: () => boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Chooses a worker in the pool.
|
|
53
|
+
*/
|
|
54
|
+
choose: () => Worker;
|
|
55
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { IPoolInternal } from '../pool-internal';
|
|
2
|
+
import type { IPoolWorker } from '../pool-worker';
|
|
3
|
+
import type { IWorkerChoiceStrategy, WorkerChoiceStrategy } from './selection-strategies-types';
|
|
4
|
+
/**
|
|
5
|
+
* Gets the worker choice strategy instance.
|
|
6
|
+
*
|
|
7
|
+
* @param pool - The pool instance.
|
|
8
|
+
* @param workerChoiceStrategy - The worker choice strategy.
|
|
9
|
+
* @returns The worker choice strategy instance.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getWorkerChoiceStrategy<Worker extends IPoolWorker, Data, Response>(pool: IPoolInternal<Worker, Data, Response>, workerChoiceStrategy?: WorkerChoiceStrategy): IWorkerChoiceStrategy<Worker>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { IPoolInternal } from '../pool-internal';
|
|
2
|
+
import type { IPoolWorker } from '../pool-worker';
|
|
3
|
+
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy';
|
|
4
|
+
import type { RequiredStatistics } from './selection-strategies-types';
|
|
5
|
+
/**
|
|
6
|
+
* Selects the next worker with a weighted round robin scheduling algorithm.
|
|
7
|
+
* Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin.
|
|
8
|
+
*
|
|
9
|
+
* @typeParam Worker - Type of worker which manages the strategy.
|
|
10
|
+
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
|
|
11
|
+
* @typeParam Response - Type of response of execution. This can only be serializable data.
|
|
12
|
+
*/
|
|
13
|
+
export declare class WeightedRoundRobinWorkerChoiceStrategy<Worker extends IPoolWorker, Data, Response> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
|
|
14
|
+
/** {@inheritDoc} */
|
|
15
|
+
readonly requiredStatistics: RequiredStatistics;
|
|
16
|
+
/**
|
|
17
|
+
* Worker index where the current task will be submitted.
|
|
18
|
+
*/
|
|
19
|
+
private currentWorkerIndex;
|
|
20
|
+
/**
|
|
21
|
+
* Default worker weight.
|
|
22
|
+
*/
|
|
23
|
+
private readonly defaultWorkerWeight;
|
|
24
|
+
/**
|
|
25
|
+
* Per worker virtual task runtime map.
|
|
26
|
+
*/
|
|
27
|
+
private readonly workersTaskRunTime;
|
|
28
|
+
/**
|
|
29
|
+
* Constructs a worker choice strategy that selects with a weighted round robin scheduling algorithm.
|
|
30
|
+
*
|
|
31
|
+
* @param pool - The pool instance.
|
|
32
|
+
*/
|
|
33
|
+
constructor(pool: IPoolInternal<Worker, Data, Response>);
|
|
34
|
+
/** {@inheritDoc} */
|
|
35
|
+
reset(): boolean;
|
|
36
|
+
/** {@inheritDoc} */
|
|
37
|
+
choose(): Worker;
|
|
38
|
+
private initWorkersTaskRunTime;
|
|
39
|
+
private initWorkerTaskRunTime;
|
|
40
|
+
private setWorkerTaskRunTime;
|
|
41
|
+
private getWorkerVirtualTaskRunTime;
|
|
42
|
+
private computeWorkerWeight;
|
|
43
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { IPoolInternal } from '../pool-internal';
|
|
2
|
+
import type { IPoolWorker } from '../pool-worker';
|
|
3
|
+
import type { IWorkerChoiceStrategy, WorkerChoiceStrategy } from './selection-strategies-types';
|
|
4
|
+
/**
|
|
5
|
+
* The worker choice strategy context.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam Worker - Type of worker.
|
|
8
|
+
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
|
|
9
|
+
* @typeParam Response - Type of response of execution. This can only be serializable data.
|
|
10
|
+
*/
|
|
11
|
+
export declare class WorkerChoiceStrategyContext<Worker extends IPoolWorker, Data, Response> {
|
|
12
|
+
private readonly pool;
|
|
13
|
+
private readonly createDynamicallyWorkerCallback;
|
|
14
|
+
private workerChoiceStrategy;
|
|
15
|
+
/**
|
|
16
|
+
* Worker choice strategy context constructor.
|
|
17
|
+
*
|
|
18
|
+
* @param pool - The pool instance.
|
|
19
|
+
* @param createDynamicallyWorkerCallback - The worker creation callback for dynamic pool.
|
|
20
|
+
* @param workerChoiceStrategy - The worker choice strategy.
|
|
21
|
+
*/
|
|
22
|
+
constructor(pool: IPoolInternal<Worker, Data, Response>, createDynamicallyWorkerCallback: () => Worker, workerChoiceStrategy?: WorkerChoiceStrategy);
|
|
23
|
+
/**
|
|
24
|
+
* Gets the worker choice strategy instance specific to the pool type.
|
|
25
|
+
*
|
|
26
|
+
* @param workerChoiceStrategy - The worker choice strategy.
|
|
27
|
+
* @returns The worker choice strategy instance for the pool type.
|
|
28
|
+
*/
|
|
29
|
+
private getPoolWorkerChoiceStrategy;
|
|
30
|
+
/**
|
|
31
|
+
* Gets the worker choice strategy used in the context.
|
|
32
|
+
*
|
|
33
|
+
* @returns The worker choice strategy.
|
|
34
|
+
*/
|
|
35
|
+
getWorkerChoiceStrategy(): IWorkerChoiceStrategy<Worker>;
|
|
36
|
+
/**
|
|
37
|
+
* Sets the worker choice strategy to use in the context.
|
|
38
|
+
*
|
|
39
|
+
* @param workerChoiceStrategy - The worker choice strategy to set.
|
|
40
|
+
*/
|
|
41
|
+
setWorkerChoiceStrategy(workerChoiceStrategy: WorkerChoiceStrategy): void;
|
|
42
|
+
/**
|
|
43
|
+
* Chooses a worker with the underlying selection strategy.
|
|
44
|
+
*
|
|
45
|
+
* @returns The chosen one.
|
|
46
|
+
*/
|
|
47
|
+
execute(): Worker;
|
|
48
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { PoolOptions } from '../pool';
|
|
2
|
+
import { PoolType } from '../pool-internal';
|
|
3
|
+
import type { ThreadWorkerWithMessageChannel } from './fixed';
|
|
4
|
+
import { FixedThreadPool } from './fixed';
|
|
5
|
+
/**
|
|
6
|
+
* A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads.
|
|
7
|
+
*
|
|
8
|
+
* This thread pool creates new threads when the others are busy, up to the maximum number of threads.
|
|
9
|
+
* When the maximum number of threads is reached, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
|
|
12
|
+
* @typeParam Response - Type of response of execution. This can only be serializable data.
|
|
13
|
+
* @author [Alessandro Pio Ardizio](https://github.com/pioardi)
|
|
14
|
+
* @since 0.0.1
|
|
15
|
+
*/
|
|
16
|
+
export declare class DynamicThreadPool<Data = unknown, Response = unknown> extends FixedThreadPool<Data, Response> {
|
|
17
|
+
protected readonly max: number;
|
|
18
|
+
/**
|
|
19
|
+
* Constructs a new poolifier dynamic thread pool.
|
|
20
|
+
*
|
|
21
|
+
* @param min - Minimum number of threads which are always active.
|
|
22
|
+
* @param max - Maximum number of threads that can be created by this pool.
|
|
23
|
+
* @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
|
|
24
|
+
* @param opts - Options for this dynamic thread pool.
|
|
25
|
+
*/
|
|
26
|
+
constructor(min: number, max: number, filePath: string, opts?: PoolOptions<ThreadWorkerWithMessageChannel>);
|
|
27
|
+
/** {@inheritDoc} */
|
|
28
|
+
get type(): PoolType;
|
|
29
|
+
/** {@inheritDoc} */
|
|
30
|
+
get busy(): boolean;
|
|
31
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { MessageChannel, Worker } from 'worker_threads';
|
|
3
|
+
import type { Draft, MessageValue } from '../../utility-types';
|
|
4
|
+
import { AbstractPool } from '../abstract-pool';
|
|
5
|
+
import type { PoolOptions } from '../pool';
|
|
6
|
+
import { PoolType } from '../pool-internal';
|
|
7
|
+
/**
|
|
8
|
+
* A thread worker with message channels for communication between main thread and thread worker.
|
|
9
|
+
*/
|
|
10
|
+
export type ThreadWorkerWithMessageChannel = Worker & Draft<MessageChannel>;
|
|
11
|
+
/**
|
|
12
|
+
* A thread pool with a fixed number of threads.
|
|
13
|
+
*
|
|
14
|
+
* It is possible to perform tasks in sync or asynchronous mode as you prefer.
|
|
15
|
+
*
|
|
16
|
+
* This pool selects the threads in a round robin fashion.
|
|
17
|
+
*
|
|
18
|
+
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
|
|
19
|
+
* @typeParam Response - Type of response of execution. This can only be serializable data.
|
|
20
|
+
* @author [Alessandro Pio Ardizio](https://github.com/pioardi)
|
|
21
|
+
* @since 0.0.1
|
|
22
|
+
*/
|
|
23
|
+
export declare class FixedThreadPool<Data = unknown, Response = unknown> extends AbstractPool<ThreadWorkerWithMessageChannel, Data, Response> {
|
|
24
|
+
/**
|
|
25
|
+
* Constructs a new poolifier fixed thread pool.
|
|
26
|
+
*
|
|
27
|
+
* @param numberOfThreads - Number of threads for this pool.
|
|
28
|
+
* @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
|
|
29
|
+
* @param opts - Options for this fixed thread pool.
|
|
30
|
+
*/
|
|
31
|
+
constructor(numberOfThreads: number, filePath: string, opts?: PoolOptions<ThreadWorkerWithMessageChannel>);
|
|
32
|
+
/** {@inheritDoc} */
|
|
33
|
+
protected isMain(): boolean;
|
|
34
|
+
/** {@inheritDoc} */
|
|
35
|
+
destroyWorker(worker: ThreadWorkerWithMessageChannel): Promise<void>;
|
|
36
|
+
/** {@inheritDoc} */
|
|
37
|
+
protected sendToWorker(worker: ThreadWorkerWithMessageChannel, message: MessageValue<Data>): void;
|
|
38
|
+
/** {@inheritDoc} */
|
|
39
|
+
registerWorkerMessageListener<Message extends Data | Response>(messageChannel: ThreadWorkerWithMessageChannel, listener: (message: MessageValue<Message>) => void): void;
|
|
40
|
+
/** {@inheritDoc} */
|
|
41
|
+
protected createWorker(): ThreadWorkerWithMessageChannel;
|
|
42
|
+
/** {@inheritDoc} */
|
|
43
|
+
protected afterWorkerSetup(worker: ThreadWorkerWithMessageChannel): void;
|
|
44
|
+
/** {@inheritDoc} */
|
|
45
|
+
get type(): PoolType;
|
|
46
|
+
/** {@inheritDoc} */
|
|
47
|
+
get busy(): boolean;
|
|
48
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import type { Worker as ClusterWorker } from 'cluster';
|
|
4
|
+
import type { MessagePort } from 'worker_threads';
|
|
5
|
+
import type { IPoolWorker } from './pools/pool-worker';
|
|
6
|
+
import type { KillBehavior } from './worker/worker-options';
|
|
7
|
+
/**
|
|
8
|
+
* Make all properties in T non-readonly.
|
|
9
|
+
*/
|
|
10
|
+
export type Draft<T> = {
|
|
11
|
+
-readonly [P in keyof T]?: T[P];
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Message object that is passed between worker and main worker.
|
|
15
|
+
*/
|
|
16
|
+
export interface MessageValue<Data = unknown, MainWorker extends ClusterWorker | MessagePort | unknown = unknown> {
|
|
17
|
+
/**
|
|
18
|
+
* Input data that will be passed to the worker.
|
|
19
|
+
*/
|
|
20
|
+
readonly data?: Data;
|
|
21
|
+
/**
|
|
22
|
+
* Id of the message.
|
|
23
|
+
*/
|
|
24
|
+
readonly id?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Kill code.
|
|
27
|
+
*/
|
|
28
|
+
readonly kill?: KillBehavior | 1;
|
|
29
|
+
/**
|
|
30
|
+
* Error.
|
|
31
|
+
*/
|
|
32
|
+
readonly error?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Task runtime.
|
|
35
|
+
*/
|
|
36
|
+
readonly taskRunTime?: number;
|
|
37
|
+
/**
|
|
38
|
+
* Reference to main worker.
|
|
39
|
+
*
|
|
40
|
+
* Only for internal use.
|
|
41
|
+
*/
|
|
42
|
+
readonly parent?: MainWorker;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* An object holding the worker that will be used to resolve/rejects the promise later on.
|
|
46
|
+
*
|
|
47
|
+
* @typeParam Worker - Type of worker.
|
|
48
|
+
* @typeParam Response - Type of response of execution. This can only be serializable data.
|
|
49
|
+
*/
|
|
50
|
+
export interface PromiseWorkerResponseWrapper<Worker extends IPoolWorker, Response = unknown> {
|
|
51
|
+
/**
|
|
52
|
+
* Resolve callback to fulfill the promise.
|
|
53
|
+
*/
|
|
54
|
+
readonly resolve: (value: Response) => void;
|
|
55
|
+
/**
|
|
56
|
+
* Reject callback to reject the promise.
|
|
57
|
+
*/
|
|
58
|
+
readonly reject: (reason?: string) => void;
|
|
59
|
+
/**
|
|
60
|
+
* The worker that has the assigned task.
|
|
61
|
+
*/
|
|
62
|
+
readonly worker: Worker;
|
|
63
|
+
}
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
/// <reference types="node" />
|
|
4
|
+
/// <reference types="node" />
|
|
5
|
+
import { AsyncResource } from 'async_hooks';
|
|
6
|
+
import type { Worker } from 'cluster';
|
|
7
|
+
import type { MessagePort } from 'worker_threads';
|
|
8
|
+
import type { MessageValue } from '../utility-types';
|
|
9
|
+
import type { WorkerOptions } from './worker-options';
|
|
10
|
+
/**
|
|
11
|
+
* Base class that implements some shared logic for all poolifier workers.
|
|
12
|
+
*
|
|
13
|
+
* @typeParam MainWorker - Type of main worker.
|
|
14
|
+
* @typeParam Data - Type of data this worker receives from pool's execution. This can only be serializable data.
|
|
15
|
+
* @typeParam Response - Type of response the worker sends back to the main worker. This can only be serializable data.
|
|
16
|
+
*/
|
|
17
|
+
export declare abstract class AbstractWorker<MainWorker extends Worker | MessagePort, Data = unknown, Response = unknown> extends AsyncResource {
|
|
18
|
+
protected mainWorker: MainWorker | undefined | null;
|
|
19
|
+
/**
|
|
20
|
+
* Timestamp of the last task processed by this worker.
|
|
21
|
+
*/
|
|
22
|
+
protected lastTaskTimestamp: number;
|
|
23
|
+
/**
|
|
24
|
+
* Handler Id of the `aliveInterval` worker alive check.
|
|
25
|
+
*/
|
|
26
|
+
protected readonly aliveInterval?: NodeJS.Timeout;
|
|
27
|
+
/**
|
|
28
|
+
* Options for the worker.
|
|
29
|
+
*/
|
|
30
|
+
readonly opts: WorkerOptions;
|
|
31
|
+
/**
|
|
32
|
+
* Constructs a new poolifier worker.
|
|
33
|
+
*
|
|
34
|
+
* @param type - The type of async event.
|
|
35
|
+
* @param isMain - Whether this is the main worker or not.
|
|
36
|
+
* @param fn - Function processed by the worker when the pool's `execution` function is invoked.
|
|
37
|
+
* @param mainWorker - Reference to main worker.
|
|
38
|
+
* @param opts - Options for the worker.
|
|
39
|
+
*/
|
|
40
|
+
constructor(type: string, isMain: boolean, fn: (data: Data) => Response, mainWorker: MainWorker | undefined | null, opts?: WorkerOptions);
|
|
41
|
+
protected messageListener(value: MessageValue<Data, MainWorker>, fn: (data: Data) => Response): void;
|
|
42
|
+
private checkWorkerOptions;
|
|
43
|
+
/**
|
|
44
|
+
* Checks if the `fn` parameter is passed to the constructor.
|
|
45
|
+
*
|
|
46
|
+
* @param fn - The function that should be defined.
|
|
47
|
+
*/
|
|
48
|
+
private checkFunctionInput;
|
|
49
|
+
/**
|
|
50
|
+
* Returns the main worker.
|
|
51
|
+
*
|
|
52
|
+
* @returns Reference to the main worker.
|
|
53
|
+
*/
|
|
54
|
+
protected getMainWorker(): MainWorker;
|
|
55
|
+
/**
|
|
56
|
+
* Sends a message to the main worker.
|
|
57
|
+
*
|
|
58
|
+
* @param message - The response message.
|
|
59
|
+
*/
|
|
60
|
+
protected abstract sendToMainWorker(message: MessageValue<Response>): void;
|
|
61
|
+
/**
|
|
62
|
+
* Checks if the worker should be terminated, because its living too long.
|
|
63
|
+
*/
|
|
64
|
+
protected checkAlive(): void;
|
|
65
|
+
/**
|
|
66
|
+
* Handles an error and convert it to a string so it can be sent back to the main worker.
|
|
67
|
+
*
|
|
68
|
+
* @param e - The error raised by the worker.
|
|
69
|
+
* @returns Message of the error.
|
|
70
|
+
*/
|
|
71
|
+
protected handleError(e: Error | string): string;
|
|
72
|
+
/**
|
|
73
|
+
* Runs the given function synchronously.
|
|
74
|
+
*
|
|
75
|
+
* @param fn - Function that will be executed.
|
|
76
|
+
* @param value - Input data for the given function.
|
|
77
|
+
*/
|
|
78
|
+
protected run(fn: (data?: Data) => Response, value: MessageValue<Data>): void;
|
|
79
|
+
/**
|
|
80
|
+
* Runs the given function asynchronously.
|
|
81
|
+
*
|
|
82
|
+
* @param fn - Function that will be executed.
|
|
83
|
+
* @param value - Input data for the given function.
|
|
84
|
+
*/
|
|
85
|
+
protected runAsync(fn: (data?: Data) => Promise<Response>, value: MessageValue<Data>): void;
|
|
86
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Worker } from 'cluster';
|
|
3
|
+
import type { MessageValue } from '../utility-types';
|
|
4
|
+
import { AbstractWorker } from './abstract-worker';
|
|
5
|
+
import type { WorkerOptions } from './worker-options';
|
|
6
|
+
/**
|
|
7
|
+
* A cluster worker used by a poolifier `ClusterPool`.
|
|
8
|
+
*
|
|
9
|
+
* When this worker is inactive for more than the given `maxInactiveTime`,
|
|
10
|
+
* it will send a termination request to its main worker.
|
|
11
|
+
*
|
|
12
|
+
* If you use a `DynamicClusterPool` the extra workers that were created will be terminated,
|
|
13
|
+
* but the minimum number of workers will be guaranteed.
|
|
14
|
+
*
|
|
15
|
+
* @typeParam Data - Type of data this worker receives from pool's execution. This can only be serializable data.
|
|
16
|
+
* @typeParam Response - Type of response the worker sends back to the main worker. This can only be serializable data.
|
|
17
|
+
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
|
|
18
|
+
* @since 2.0.0
|
|
19
|
+
*/
|
|
20
|
+
export declare class ClusterWorker<Data = unknown, Response = unknown> extends AbstractWorker<Worker, Data, Response> {
|
|
21
|
+
/**
|
|
22
|
+
* Constructs a new poolifier cluster worker.
|
|
23
|
+
*
|
|
24
|
+
* @param fn - Function processed by the worker when the pool's `execution` function is invoked.
|
|
25
|
+
* @param opts - Options for the worker.
|
|
26
|
+
*/
|
|
27
|
+
constructor(fn: (data: Data) => Response, opts?: WorkerOptions);
|
|
28
|
+
/** {@inheritDoc} */
|
|
29
|
+
protected sendToMainWorker(message: MessageValue<Response>): void;
|
|
30
|
+
/** {@inheritDoc} */
|
|
31
|
+
protected handleError(e: Error | string): string;
|
|
32
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { MessagePort } from 'worker_threads';
|
|
3
|
+
import type { MessageValue } from '../utility-types';
|
|
4
|
+
import { AbstractWorker } from './abstract-worker';
|
|
5
|
+
import type { WorkerOptions } from './worker-options';
|
|
6
|
+
/**
|
|
7
|
+
* A thread worker used by a poolifier `ThreadPool`.
|
|
8
|
+
*
|
|
9
|
+
* When this worker is inactive for more than the given `maxInactiveTime`,
|
|
10
|
+
* it will send a termination request to its main thread.
|
|
11
|
+
*
|
|
12
|
+
* If you use a `DynamicThreadPool` the extra workers that were created will be terminated,
|
|
13
|
+
* but the minimum number of workers will be guaranteed.
|
|
14
|
+
*
|
|
15
|
+
* @typeParam Data - Type of data this worker receives from pool's execution. This can only be serializable data.
|
|
16
|
+
* @typeParam Response - Type of response the worker sends back to the main thread. This can only be serializable data.
|
|
17
|
+
* @author [Alessandro Pio Ardizio](https://github.com/pioardi)
|
|
18
|
+
* @since 0.0.1
|
|
19
|
+
*/
|
|
20
|
+
export declare class ThreadWorker<Data = unknown, Response = unknown> extends AbstractWorker<MessagePort, Data, Response> {
|
|
21
|
+
/**
|
|
22
|
+
* Constructs a new poolifier thread worker.
|
|
23
|
+
*
|
|
24
|
+
* @param fn - Function processed by the worker when the pool's `execution` function is invoked.
|
|
25
|
+
* @param opts - Options for the worker.
|
|
26
|
+
*/
|
|
27
|
+
constructor(fn: (data: Data) => Response, opts?: WorkerOptions);
|
|
28
|
+
/** {@inheritDoc} */
|
|
29
|
+
protected sendToMainWorker(message: MessageValue<Response>): void;
|
|
30
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enumeration of kill behaviors.
|
|
3
|
+
*/
|
|
4
|
+
export declare const KillBehaviors: Readonly<{
|
|
5
|
+
/**
|
|
6
|
+
* If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker **wont** be deleted.
|
|
7
|
+
*/
|
|
8
|
+
readonly SOFT: "SOFT";
|
|
9
|
+
/**
|
|
10
|
+
* If `lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted.
|
|
11
|
+
*/
|
|
12
|
+
readonly HARD: "HARD";
|
|
13
|
+
}>;
|
|
14
|
+
/**
|
|
15
|
+
* Kill behavior.
|
|
16
|
+
*/
|
|
17
|
+
export type KillBehavior = keyof typeof KillBehaviors;
|
|
18
|
+
/**
|
|
19
|
+
* Detects whether the given value is a kill behavior or not.
|
|
20
|
+
*
|
|
21
|
+
* @typeParam KB - Which specific KillBehavior to test against.
|
|
22
|
+
* @param killBehavior - Which kind of kill behavior to detect.
|
|
23
|
+
* @param value - Any value.
|
|
24
|
+
* @returns `true` if `value` was strictly equals to `killBehavior`, otherwise `false`.
|
|
25
|
+
*/
|
|
26
|
+
export declare function isKillBehavior<KB extends KillBehavior>(killBehavior: KB, value: unknown): value is KB;
|
|
27
|
+
/**
|
|
28
|
+
* Options for workers.
|
|
29
|
+
*/
|
|
30
|
+
export interface WorkerOptions {
|
|
31
|
+
/**
|
|
32
|
+
* Maximum waiting time in milliseconds for tasks.
|
|
33
|
+
*
|
|
34
|
+
* After this time, newly created workers will be terminated.
|
|
35
|
+
* The last active time of your worker unit will be updated when a task is submitted to a worker or when a worker terminate a task.
|
|
36
|
+
*
|
|
37
|
+
* - If `killBehavior` is set to `KillBehaviors.HARD` this value represents also the timeout for the tasks that you submit to the pool,
|
|
38
|
+
* when this timeout expires your tasks is interrupted and the worker is killed if is not part of the minimum size of the pool.
|
|
39
|
+
* - If `killBehavior` is set to `KillBehaviors.SOFT` your tasks have no timeout and your workers will not be terminated until your task is completed.
|
|
40
|
+
*
|
|
41
|
+
* @defaultValue 60000 ms
|
|
42
|
+
*/
|
|
43
|
+
maxInactiveTime?: number;
|
|
44
|
+
/**
|
|
45
|
+
* Whether your worker will perform asynchronous or not.
|
|
46
|
+
*
|
|
47
|
+
* @defaultValue false
|
|
48
|
+
*/
|
|
49
|
+
async?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* `killBehavior` dictates if your async unit (worker/process) will be deleted in case that a task is active on it.
|
|
52
|
+
*
|
|
53
|
+
* - SOFT: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker **won't** be deleted.
|
|
54
|
+
* - HARD: If `lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted.
|
|
55
|
+
*
|
|
56
|
+
* This option only apply to the newly created workers.
|
|
57
|
+
*
|
|
58
|
+
* @defaultValue KillBehaviors.SOFT
|
|
59
|
+
*/
|
|
60
|
+
killBehavior?: KillBehavior;
|
|
61
|
+
}
|