-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
fixed.ts
126 lines (112 loc) · 3.23 KB
/
fixed.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import cluster, { type ClusterSettings, type Worker } from 'node:cluster'
import type { MessageValue } from '../../utility-types'
import { AbstractPool } from '../abstract-pool'
import {
type PoolOptions,
type PoolType,
PoolTypes,
type WorkerType,
WorkerTypes
} from '../pool'
/**
* Options for a poolifier cluster pool.
*/
export interface ClusterPoolOptions extends PoolOptions<Worker> {
/**
* Key/value pairs to add to worker process environment.
*
* @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
env?: any
/**
* Cluster settings.
*
* @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
*/
settings?: ClusterSettings
}
/**
* A cluster pool with a fixed number of workers.
*
* It is possible to perform tasks in sync or asynchronous mode as you prefer.
*
* This pool selects the workers in a round robin fashion.
*
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
* @typeParam Response - Type of execution response. This can only be serializable data.
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
* @since 2.0.0
*/
export class FixedClusterPool<
Data = unknown,
Response = unknown
> extends AbstractPool<Worker, Data, Response> {
/**
* Constructs a new poolifier fixed cluster pool.
*
* @param numberOfWorkers - Number of workers for this pool.
* @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
* @param opts - Options for this fixed cluster pool.
*/
public constructor (
numberOfWorkers: number,
filePath: string,
protected readonly opts: ClusterPoolOptions = {}
) {
super(numberOfWorkers, filePath, opts)
}
/** @inheritDoc */
protected setupHook (): void {
cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
}
/** @inheritDoc */
protected isMain (): boolean {
return cluster.isPrimary
}
/** @inheritDoc */
protected destroyWorker (worker: Worker): void {
this.sendToWorker(worker, { kill: 1 })
worker.kill()
}
/** @inheritDoc */
protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
worker.send(message)
}
/** @inheritDoc */
protected registerWorkerMessageListener<Message extends Data | Response>(
worker: Worker,
listener: (message: MessageValue<Message>) => void
): void {
worker.on('message', listener)
}
/** @inheritDoc */
protected createWorker (): Worker {
return cluster.fork(this.opts.env)
}
/** @inheritDoc */
protected afterWorkerSetup (worker: Worker): void {
// Listen to worker messages.
this.registerWorkerMessageListener(worker, super.workerListener())
}
/** @inheritDoc */
protected get type (): PoolType {
return PoolTypes.fixed
}
/** @inheritDoc */
protected get worker (): WorkerType {
return WorkerTypes.cluster
}
/** @inheritDoc */
protected get minSize (): number {
return this.numberOfWorkers
}
/** @inheritDoc */
protected get maxSize (): number {
return this.numberOfWorkers
}
/** @inheritDoc */
protected get busy (): boolean {
return this.internalBusy()
}
}