This repository was archived by the owner on Jan 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathShardingManager.ts
More file actions
315 lines (267 loc) · 9.62 KB
/
ShardingManager.ts
File metadata and controls
315 lines (267 loc) · 9.62 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { REST } from '@discordjs/rest';
import { chunk } from '@sapphire/utilities';
import { EventEmitter } from 'node:events';
import { cpus } from 'node:os';
import { setTimeout as sleep } from 'node:timers/promises';
import { z } from 'zod';
import type { IMessageHandlerConstructor } from './messages/base/IMessageHandler';
import { JsonMessageHandler } from './messages/JsonMessageHandler';
import type { BaseProcessClusterHandlerOptions } from './server/clusters/process/BaseProcessClusterHandler';
import { ForkProcessClusterHandler } from './server/clusters/process/ForkProcessClusterHandler';
import type {
IClusterHandler,
IClusterHandlerConstructor,
ClusterHandlerSendOptions,
} from './server/clusters/base/IClusterHandler';
import type { ShardPingOptions } from './server/ShardPing';
import {
fetchRecommendedShards,
FetchRecommendedShardsOptions,
fetchRecommendedShardsOptionsPredicate,
} from './server/utils/utils';
import type { NonNullObject } from './utils/types';
const shardingManagerOptionsPredicate = z.strictObject({
shardList: z.literal('auto').or(z.array(z.number().positive().int()).nonempty()).default('auto'),
totalShards: z.literal('auto').or(z.number().int().gte(1)).default('auto'),
clusterCount: z
.number()
.int()
.gte(1)
.default(() => cpus().length),
token: z.string().nonempty().nullish().default(null),
rest: z.instanceof(REST).optional(),
respawns: z.number().int().positive().or(z.literal(-1)).or(z.literal(Infinity)).default(-1),
shardOptions: z.object({}).default({}),
pingOptions: z.strictObject({
delay: z.number().int().gte(1).or(z.literal(-1)).or(z.literal(Infinity)).default(45_000),
delaySinceReceived: z.boolean().default(false),
}),
ClusterHandler: z.object({ spawn: z.function(), validate: z.function(), isPrimary: z.boolean() }).optional(),
MessageHandler: z.object({ build: z.function() }).optional(),
});
const shardingManagerSpawnOptions = z
.strictObject({
amount: z.literal('auto').or(z.number().int().gte(1)).optional(),
delay: z.number().int().positive().default(5500),
timeout: z.number().int().positive().default(30_000),
})
.merge(fetchRecommendedShardsOptionsPredicate);
const shardingManagerRespawnAllOptions = z.strictObject({
shardDelay: z.number().int().positive().default(5000),
respawnDelay: z.number().int().positive().default(500),
timeout: z.number().int().positive().or(z.literal(-1)).or(z.literal(Infinity)).default(30000),
});
/**
* The ShardingManager is an utility class that makes multi-thread sharding of a bot a much simpler experience.
*
* It works by spawning shards that will create a channel to a different thread, process, or server, defined by the
* implementation of the defined {@link IClusterHandler}, and sends messages between the primary process (the one that
* spawns the shards) with the shards using a message queue and message format as defined by {@link IMessageHandler}.
*
* Furthermore, this utility has several useful methods that allow managing the lifetimes of the shards as well as
* sending messages to one or all of them.
*/
export class ShardingManager<ShardOptions = NonNullObject> extends EventEmitter {
/**
* Number of total shards of all shard managers or "auto".
*/
public shardList: [number, ...number[]] | 'auto';
/**
* List of shards to spawn or "auto".
*/
public totalShards: number | 'auto';
/**
* The number of clusters to create.
*/
public clusterCount: number;
/**
* Whether or not the shards should respawn.
*/
public readonly respawns: number;
/**
* The REST handler.
*/
public readonly rest: REST;
/**
* The shard options validated by {@link IClusterHandlerConstructor.validate}.
*/
public readonly options: ShardOptions;
public readonly pingOptions: Required<ShardPingOptions>;
/**
* The {@link IClusterHandler} constructor.
*/
public readonly ClusterHandler: IClusterHandlerConstructor<ShardOptions>;
/**
* The {@link IMessageHandler} constructor.
*/
public readonly MessageHandler: IMessageHandlerConstructor;
/**
* Token to use for automatic shard count and passing to shards.
*/
public readonly token: string | null;
public readonly shards: IClusterHandler<ShardOptions>[] = [];
public constructor(options: ShardingManagerOptions<ShardOptions> = {}) {
super();
const resolved = shardingManagerOptionsPredicate.parse(options);
this.shardList = resolved.shardList;
this.totalShards = resolved.totalShards;
this.clusterCount = resolved.clusterCount;
this.respawns = resolved.respawns;
this.token = resolved.token?.replace(/^Bot\s*/i, '') ?? null;
this.rest = resolved.rest ?? new REST().setToken(this.token!);
this.ClusterHandler = options.ClusterHandler ?? (ForkProcessClusterHandler as any);
this.options = this.ClusterHandler.validate(options.shardOptions);
this.pingOptions = resolved.pingOptions;
this.ClusterHandler.setup(this.options);
this.MessageHandler = options.MessageHandler ?? JsonMessageHandler;
}
/**
* Whether or not the process is a primary one.
*/
public get isPrimary() {
return this.ClusterHandler.isPrimary;
}
/**
* Spawns all shards given the options and that the process is not primary.
* @param options The spawn options.
* @returns Whether or not the spawn has happened. Will always be the opposite of {@link IClusterHandlerConstructor.isPrimary}.
*/
public async spawn(options: ShardingManagerSpawnOptions): Promise<boolean> {
if (this.isPrimary) return false;
const resolved = shardingManagerSpawnOptions.parse(options);
let amount = resolved.amount ?? this.totalShards;
if (amount === 'auto') {
amount = await fetchRecommendedShards(this.rest, resolved);
}
if (this.shards.length >= amount) throw new Error('Shards have already spawned.');
if (this.shardList === 'auto' || this.totalShards === 'auto' || this.totalShards !== amount) {
this.shardList = [...Array(amount).keys()] as [number, ...number[]];
}
if (this.totalShards === 'auto' || this.totalShards !== amount) {
this.totalShards = amount;
}
const shards = z.number().int().gte(0).lt(amount).array().parse(this.shardList);
const clusterIds = chunk(shards, Math.ceil(shards.length / this.clusterCount));
// Spawn the shards
for (const shardIds of clusterIds) {
await Promise.all([
this.spawnShard(shardIds),
resolved.delay > 0 && this.shards.length !== clusterIds.length ? sleep(resolved.delay) : Promise.resolve(),
]);
}
return true;
}
public async spawnShard(shardIds: readonly number[]) {
const shard = new this.ClusterHandler(shardIds, this, this.MessageHandler);
await shard.start({ timeout: 0 });
this.shards.push(shard);
this.emit('shardCreate', shard);
return shard;
}
/**
* Kills all running shards and respawns them.
* @param options The options for respawning shards.
*/
public async respawnAll(options: ShardingManagerRespawnAllOptions) {
const resolved = shardingManagerRespawnAllOptions.parse(options);
let s = 0;
for (const shard of this.shards) {
await Promise.all([
shard.restart({ delay: resolved.respawnDelay, timeout: resolved.timeout }),
++s < this.shards.length && resolved.shardDelay > 0 ? sleep(resolved.shardDelay) : Promise.resolve(),
]);
}
}
/**
* Sends a message to all shards.
* @param data The data to be sent to the shards.
* @param options The options to be passed to each {@link IClusterHandler.send}.
* @returns An array of the resolved values from {@link IClusterHandler.send}.
*/
public broadcast(data: unknown, options: ClusterHandlerSendOptions): Promise<unknown[]> {
const promises = [];
for (const shard of this.shards.values()) promises.push(shard.send(data, options));
return Promise.all(promises);
}
}
export interface ShardingManagerOptions<ShardOptions extends NonNullObject = BaseProcessClusterHandlerOptions> {
/**
* Number of total shards of all shard managers or "auto".
* @default 'auto'
*/
totalShards?: number | 'auto';
/**
* List of shards to spawn or "auto".
* @default 'auto'
*/
shardList?: [number, ...number[]] | 'auto';
/**
* The number of clusters to create, defaults to the number of cores.
* @default
*/
clusters?: number;
/**
* The amount of times to respawn shards (`-1` or `Infinity` for no limitless)
* @default -1
*/
respawns?: number;
/**
* The {@link IClusterHandler} builder.
* @default ForkProcessClusterHandler
*/
ClusterHandler?: IClusterHandlerConstructor<ShardOptions>;
/**
* The shard options.
* @default {}
*/
shardOptions?: ShardOptions;
/**
* The options for the shard pinging.
* @default {}
*/
pingOptions?: ShardPingOptions;
/**
* The {@link IMessageHandler} builder.
* @default JsonMessageHandler
*/
MessageHandler?: IMessageHandlerConstructor;
/**
* Token to use for automatic shard count and passing to shards.
* @default process.env.DISCORD_TOKEN
*/
token?: string;
}
export interface ShardingManagerSpawnOptions extends FetchRecommendedShardsOptions {
/**
* Number of shards to spawn.
* @default this.totalShards
*/
amount?: number | 'auto';
/**
* How long to wait in between spawning each shard, in milliseconds.
* @default 5500
*/
delay?: number;
/**
* The amount in milliseconds to wait until the shard has become ready.
* @default 30000
*/
timeout?: number;
}
export interface ShardingManagerRespawnAllOptions {
/**
* How long to wait between shards, in milliseconds.
* @default 5000
*/
shardDelay?: number;
/**
* How long to wait between killing a shard's process and restarting it, in milliseconds.
* @default 500
*/
respawnDelay?: number;
/**
* The amount in milliseconds to wait for a shard to become ready before, continuing to another (`-1` or `Infinity` for no wait).
* @default 30000
*/
timeout?: number;
}