Overhaul sharding

This commit is contained in:
Schuyler Cebulskie
2017-11-19 01:28:46 -05:00
parent ad69e2ba4c
commit a414e4884f
4 changed files with 132 additions and 58 deletions

View File

@@ -127,7 +127,11 @@
"semi-spacing": "error",
"semi": "error",
"space-before-blocks": "error",
"space-before-function-paren": ["error", "never"],
"space-before-function-paren": ["error", {
"anonymous": "never",
"named": "never",
"asyncArrow": "always"
}],
"space-in-parens": "error",
"space-infix-ops": "error",
"space-unary-ops": "error",

View File

@@ -20,9 +20,13 @@ const Messages = {
SHARDING_REQUIRED: 'This session would have handled too many guilds - Sharding is required.',
SHARDING_CHILD_CONNECTION: 'Failed to send message to shard\'s process.',
SHARDING_PARENT_CONNECTION: 'Failed to send message to master process.',
SHARDING_NO_SHARDS: 'No shards have been spawned',
SHARDING_IN_PROCESS: 'Shards are still being spawned',
SHARDING_ALREADY_SPAWNED: count => `Already spawned ${count} shards`,
SHARDING_NO_SHARDS: 'No shards have been spawned.',
SHARDING_IN_PROCESS: 'Shards are still being spawned.',
SHARDING_ALREADY_SPAWNED: count => `Already spawned ${count} shards.`,
SHARDING_PROCESS_EXISTS: id => `Shard ${id} already has an active process.`,
SHARDING_READY_TIMEOUT: id => `Shard ${id}'s Client took too long to become ready.`,
SHARDING_READY_DISCONNECTED: id => `Shard ${id}'s Client disconnected before becoming ready.`,
SHARDING_READY_DIED: id => `Shard ${id}'s process exited before its Client became ready.`,
COLOR_RANGE: 'Color must be within the range 0 - 16777215 (0xFFFFFF).',
COLOR_CONVERT: 'Unable to convert color to a number.',

View File

@@ -3,6 +3,7 @@ const EventEmitter = require('events');
const path = require('path');
const Util = require('../util/Util');
const { Error } = require('../errors');
const delayFor = require('util').promisify(setTimeout);
/**
* Represents a Shard spawned by the ShardingManager.
@@ -15,6 +16,7 @@ class Shard extends EventEmitter {
*/
constructor(manager, id, args = []) {
super();
/**
* Manager that created the shard
* @type {ShardingManager}
@@ -27,6 +29,12 @@ class Shard extends EventEmitter {
*/
this.id = id;
/**
* Arguments for the shard's process
* @type {string[]}
*/
this.args = args;
/**
* Environment variables for the shard's process
* @type {Object}
@@ -37,20 +45,18 @@ class Shard extends EventEmitter {
CLIENT_TOKEN: this.manager.token,
});
/**
* Process of the shard
* @type {ChildProcess}
*/
this.process = childProcess.fork(path.resolve(this.manager.file), args, {
env: this.env,
}).on('message', this._handleMessage.bind(this));
/**
* Whether the shard's {@link Client} is ready
* @type {boolean}
*/
this.ready = false;
/**
* Process of the shard
* @type {?ChildProcess}
*/
this.process = null;
/**
* Ongoing promises for calls to {@link Shard#eval}, mapped by the `script` they were called with
* @type {Map<string, Promise>}
@@ -65,11 +71,55 @@ class Shard extends EventEmitter {
*/
this._fetches = new Map();
// Handle the death of the process
this.process.once('exit', () => {
this.ready = false;
if (this.manager.respawn) this.manager.createShard(this.id).catch(err => { this.manager.emit('error', err); });
/**
* Listener function for the {@link ChildProcess}' `exit` event
* @type {Function}
*/
this._exitListener = this._handleExit.bind(this);
}
/**
* Forks a child process for the shard.
* <warn>You should not need to call this manually.</warn>
* @param {boolean} [waitForReady=true] Whether to wait until the {@link Client} has become ready before resolving
* @returns {Promise<ChildProcess>}
*/
async spawn(waitForReady = true) {
if (this.process) throw new Error('SHARDING_PROCESS_EXISTS', this.id);
this.process = childProcess.fork(path.resolve(this.manager.file), this.args, { env: this.env })
.on('message', this._handleMessage.bind(this))
.on('exit', this._exitListener);
/**
* Emitted upon the creation of the shard's child process.
* @event Shard#spawn
* @param {ChildProcess} process Child process that was created
*/
this.emit('spawn', this.process);
if (!waitForReady) return this.process;
await new Promise((resolve, reject) => {
this.once('ready', resolve);
this.once('disconnect', () => reject(new Error('SHARDING_READY_DISCONNECTED', this.id)));
this.once('death', () => reject(new Error('SHARDING_READY_DIED', this.id)));
setTimeout(() => reject(new Error('SHARDING_READY_TIMEOUT', this.id)), 30000);
});
return this.process;
}
/**
* Kills and restarts the shard's process.
* @param {number} [delay=500] How long to wait between killing the process and restarting it (in milliseconds)
* @param {boolean} [waitForReady=true] Whether to wait the {@link Client} has become ready before resolving
* @returns {Promise<ChildProcess>}
*/
async respawn(delay = 500, waitForReady = true) {
this.process.removeListener('exit', this._exitListener);
this.process.kill();
this._handleExit(false);
if (delay > 0) await delayFor(delay);
return this.spawn(waitForReady);
}
/**
@@ -215,6 +265,23 @@ class Shard extends EventEmitter {
*/
this.manager.emit('message', this, message);
}
/**
* Handles the shard's process exiting.
* @param {boolean} [respawn=this.manager.respawn] Whether to spawn the shard again
* @private
*/
_handleExit(respawn = this.manager.respawn) {
/**
* Emitted upon the shard's child process exiting.
* @event Shard#death
* @param {ChildProcess} process Child process that exited
*/
this.emit('death', this.process);
this.ready = false;
this.process = null;
if (respawn) this.spawn().catch(err => this.emit('error', err));
}
}
module.exports = Shard;

View File

@@ -5,6 +5,7 @@ const Shard = require('./Shard');
const Collection = require('../util/Collection');
const Util = require('../util/Util');
const { Error, TypeError, RangeError } = require('../errors');
const delayFor = require('util').promisify(setTimeout);
/**
* This is a utility class that can be used to help you spawn shards of your client. Each shard is completely separate
@@ -82,33 +83,32 @@ class ShardingManager extends EventEmitter {
/**
* Spawns a single shard.
* @param {number} id The ID of the shard to spawn. **This is usually not necessary**
* @returns {Promise<Shard>}
* @param {number} id ID of the shard to spawn. **This is usually not necessary**
* @returns {Shard}
*/
createShard(id = this.shards.size) {
const shard = new Shard(this, id, this.shardArgs);
this.shards.set(id, shard);
/**
* Emitted upon launching a shard.
* Emitted upon creating a shard.
* @event ShardingManager#launch
* @param {Shard} shard Shard that was launched
* @param {Shard} shard Shard that was created
*/
this.emit('launch', shard);
return Promise.resolve(shard);
return shard;
}
/**
* Spawns multiple shards.
* @param {number} [amount=this.totalShards] Number of shards to spawn
* @param {number} [delay=7500] 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)
* @param {boolean} [waitForReady=true] Whether to wait for a shard to become ready before continuing to another
* @returns {Promise<Collection<number, Shard>>}
*/
spawn(amount = this.totalShards, delay = 7500) {
async spawn(amount = this.totalShards, delay = 5500, waitForReady = true) {
// Obtain/verify the number of shards to spawn
if (amount === 'auto') {
return Util.fetchRecommendedShards(this.token).then(count => {
this.totalShards = count;
return this._spawn(count, delay);
});
amount = await Util.fetchRecommendedShards(this.token);
} else {
if (typeof amount !== 'number' || isNaN(amount)) {
throw new TypeError('CLIENT_INVALID_OPTION', 'Amount of shards', 'a number.');
@@ -117,41 +117,22 @@ class ShardingManager extends EventEmitter {
if (amount !== Math.floor(amount)) {
throw new TypeError('CLIENT_INVALID_OPTION', 'Amount of shards', 'an integer.');
}
return this._spawn(amount, delay);
}
}
/**
* Actually spawns shards, unlike that poser above >:(
* @param {number} amount Number of shards to spawn
* @param {number} delay How long to wait in between spawning each shard (in milliseconds)
* @returns {Promise<Collection<number, Shard>>}
* @private
*/
_spawn(amount, delay) {
return new Promise(resolve => {
if (this.shards.size >= amount) throw new Error('SHARDING_ALREADY_SPAWNED', this.shards.size);
this.totalShards = amount;
// Make sure this many shards haven't already been spawned
if (this.shards.size >= amount) throw new Error('SHARDING_ALREADY_SPAWNED', this.shards.size);
this.totalShards = amount;
this.createShard();
if (this.shards.size >= this.totalShards) {
resolve(this.shards);
return;
}
// Spawn the shards
for (let s = 1; s <= amount; s++) {
const promises = [];
const shard = this.createShard();
promises.push(shard.spawn(waitForReady));
if (delay > 0 && s !== amount) promises.push(delayFor(delay));
await Promise.all(promises); // eslint-disable-line no-await-in-loop
}
if (delay <= 0) {
while (this.shards.size < this.totalShards) this.createShard();
resolve(this.shards);
} else {
const interval = setInterval(() => {
this.createShard();
if (this.shards.size >= this.totalShards) {
clearInterval(interval);
resolve(this.shards);
}
}, delay);
}
});
return this.shards;
}
/**
@@ -194,6 +175,24 @@ class ShardingManager extends EventEmitter {
for (const shard of this.shards.values()) promises.push(shard.fetchClientValue(prop));
return Promise.all(promises);
}
/**
* Kills all running shards and respawns them.
* @param {number} [shardDelay=5000] How long to wait between shards (in milliseconds)
* @param {number} [respawnDelay=500] How long to wait between killing a shard's process and restarting it
* (in milliseconds)
* @param {boolean} [waitForReady=true] Whether to wait for a shard to become ready before continuing to another
* @returns {Promise<Collection<string, Shard>>}
*/
async respawn(shardDelay = 5000, respawnDelay = 500, waitForReady = true) {
let s = 0;
for (const shard of this.shards) {
const promises = [shard.respawn(respawnDelay, waitForReady)];
if (++s < this.shards.size && shardDelay > 0) promises.push(delayFor(shardDelay));
await Promise.all(promises); // eslint-disable-line no-await-in-loop
}
return this.shards;
}
}
module.exports = ShardingManager;