mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
smarter sharding™ (#732)
This commit is contained in:
@@ -69,7 +69,6 @@
|
|||||||
"no-mixed-requires": "error",
|
"no-mixed-requires": "error",
|
||||||
"no-new-require": "error",
|
"no-new-require": "error",
|
||||||
"no-path-concat": "error",
|
"no-path-concat": "error",
|
||||||
"no-process-env": "error",
|
|
||||||
|
|
||||||
"array-bracket-spacing": "error",
|
"array-bracket-spacing": "error",
|
||||||
"block-spacing": "error",
|
"block-spacing": "error",
|
||||||
|
|||||||
@@ -27,6 +27,14 @@ class Client extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
this.options = mergeDefault(Constants.DefaultOptions, options);
|
this.options = mergeDefault(Constants.DefaultOptions, options);
|
||||||
|
|
||||||
|
if (!this.options.shard_id && process.env.hasOwnProperty('SHARD_ID')) {
|
||||||
|
this.options.shard_id = process.env.SHARD_ID;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.options.shard_count && process.env.hasOwnProperty('SHARD_COUNT')) {
|
||||||
|
this.options.shard_count = process.env.SHARD_COUNT;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The REST manager of the client
|
* The REST manager of the client
|
||||||
* @type {RESTManager}
|
* @type {RESTManager}
|
||||||
|
|||||||
@@ -26,15 +26,19 @@ class Shard {
|
|||||||
* The process of the shard
|
* The process of the shard
|
||||||
* @type {ChildProcess}
|
* @type {ChildProcess}
|
||||||
*/
|
*/
|
||||||
this.process = childProcess.fork(path.resolve(this.manager.file), [id, this.manager.shards.size]);
|
this.process = childProcess.fork(path.resolve(this.manager.file), [], {
|
||||||
|
env: {
|
||||||
this.process.once('exit', () => {
|
SHARD_ID: this.id, SHARD_COUNT: this.manager.totalShards,
|
||||||
if (this.manager.respawn) this.manager.createShard(this.id);
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
this.process.on('message', message => {
|
this.process.on('message', message => {
|
||||||
this.manager.emit('message', this, message);
|
this.manager.emit('message', this, message);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.process.once('exit', () => {
|
||||||
|
if (this.manager.respawn) this.manager.createShard(this.id);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -69,13 +69,8 @@ class ShardingManager extends EventEmitter {
|
|||||||
* @param {number} [amount=this.totalShards] Number of shards to spawn
|
* @param {number} [amount=this.totalShards] Number of shards to spawn
|
||||||
* @param {number} [delay=5500] How long to wait in between spawning each shard (in milliseconds)
|
* @param {number} [delay=5500] How long to wait in between spawning each shard (in milliseconds)
|
||||||
*/
|
*/
|
||||||
spawn(amount, delay = 5500) {
|
spawn(amount = this.totalShards, delay = 5500) {
|
||||||
if (typeof amount !== 'undefined') {
|
this.totalShards = amount;
|
||||||
if (typeof amount !== 'number' || isNaN(amount)) throw new TypeError('Amount of shards must be a number.');
|
|
||||||
if (amount < 1) throw new RangeError('Amount of shards must be at least 1.');
|
|
||||||
this.totalShards = amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.createShard();
|
this.createShard();
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
if (this.shards.size === this.totalShards) clearInterval(interval);
|
if (this.shards.size === this.totalShards) clearInterval(interval);
|
||||||
|
|||||||
Reference in New Issue
Block a user