mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 11:33:30 +01:00
Merge branch 'indev-rewrite-sharding' into indev-rewrite
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -136,9 +136,8 @@ class WebSocketManager {
|
|||||||
const payload = this.client.options.ws;
|
const payload = this.client.options.ws;
|
||||||
payload.token = this.client.token;
|
payload.token = this.client.token;
|
||||||
if (this.client.options.shard_count > 0) {
|
if (this.client.options.shard_count > 0) {
|
||||||
payload.shard = [this.client.options.shard_id, this.client.options.shard_count];
|
payload.shard = [Number(this.client.options.shard_id), Number(this.client.options.shard_count)];
|
||||||
}
|
}
|
||||||
|
|
||||||
this.send({
|
this.send({
|
||||||
op: Constants.OPCodes.IDENTIFY,
|
op: Constants.OPCodes.IDENTIFY,
|
||||||
d: payload,
|
d: payload,
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
const Client = require('./client/Client');
|
const Client = require('./client/Client');
|
||||||
|
const ShardingManager = require('./sharding/ShardingManager');
|
||||||
|
|
||||||
exports.Client = Client;
|
exports.Client = Client;
|
||||||
|
exports.ShardingManager = ShardingManager;
|
||||||
|
|||||||
49
src/sharding/Shard.js
Normal file
49
src/sharding/Shard.js
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
const childProcess = require('child_process');
|
||||||
|
const path = require('path');
|
||||||
|
const crypto = require('crypto');
|
||||||
|
|
||||||
|
class Shard {
|
||||||
|
constructor(manager, id) {
|
||||||
|
this.manager = manager;
|
||||||
|
this.id = id;
|
||||||
|
this.process = childProcess.fork(path.resolve(this.manager.file), [id, this.manager.shards.size], { silent: true });
|
||||||
|
this.waitingForResponse = new Map();
|
||||||
|
this.process.on('message', m => {
|
||||||
|
if (m && m.type && m.id) {
|
||||||
|
if (this.waitingForResponse.get(m.id)) {
|
||||||
|
const resp = this.waitingForResponse.get(m.id);
|
||||||
|
resp.resolve(m.data);
|
||||||
|
this.waitingForResponse.delete(m.id);
|
||||||
|
} else {
|
||||||
|
const reply = data => {
|
||||||
|
this.send(m.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
send(type, timeout = 60000, data = {}) {
|
||||||
|
const id = crypto.randomBytes(16).toString('hex');
|
||||||
|
this._send(id, type, timeout, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
_send(id, type, timeout, data) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.process.send({
|
||||||
|
type,
|
||||||
|
id,
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
this.waitingForResponse.set(id, {
|
||||||
|
resolve,
|
||||||
|
});
|
||||||
|
setTimeout(() => {
|
||||||
|
reject(new Error('did not receive response'));
|
||||||
|
this.waitingForResponse.delete(id);
|
||||||
|
}, timeout);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Shard;
|
||||||
28
src/sharding/ShardListener.js
Normal file
28
src/sharding/ShardListener.js
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
const crypto = require('crypto');
|
||||||
|
|
||||||
|
class ShardListener {
|
||||||
|
constructor() {
|
||||||
|
this.waitingForResponse = new Map();
|
||||||
|
this.process = process;
|
||||||
|
}
|
||||||
|
|
||||||
|
send(type, timeout = 60000, data = {}) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const id = crypto.randomBytes(16).toString('hex');
|
||||||
|
this.process.send({
|
||||||
|
type,
|
||||||
|
id,
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
this.waitingForResponse.set(id, {
|
||||||
|
resolve,
|
||||||
|
});
|
||||||
|
setTimeout(() => {
|
||||||
|
reject(new Error('did not receive response'));
|
||||||
|
this.waitingForResponse.delete(id);
|
||||||
|
}, timeout);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = ShardListener;
|
||||||
39
src/sharding/ShardingManager.js
Normal file
39
src/sharding/ShardingManager.js
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
const childProcess = require('child_process');
|
||||||
|
const path = require('path');
|
||||||
|
const EventEmitter = require('events').EventEmitter;
|
||||||
|
const Collection = require('../util/Collection');
|
||||||
|
const Shard = require('./Shard');
|
||||||
|
const crypto = require('crypto');
|
||||||
|
|
||||||
|
class ShardingManager extends EventEmitter {
|
||||||
|
constructor(file, totalShards) {
|
||||||
|
super();
|
||||||
|
this.file = file;
|
||||||
|
if (!path.isAbsolute(file)) {
|
||||||
|
this.file = path.resolve(`${process.cwd()}${file}`);
|
||||||
|
}
|
||||||
|
this.totalShards = totalShards;
|
||||||
|
this.shards = new Collection();
|
||||||
|
this.waiting = new Collection();
|
||||||
|
}
|
||||||
|
|
||||||
|
createShard() {
|
||||||
|
const id = this.shards.size;
|
||||||
|
const shard = new Shard(this, id);
|
||||||
|
this.shards.set(id, shard);
|
||||||
|
this.emit('launch', id, shard);
|
||||||
|
}
|
||||||
|
|
||||||
|
spawn(amount) {
|
||||||
|
this.totalShards = amount;
|
||||||
|
this.createShard();
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
if (this.shards.size === this.totalShards) {
|
||||||
|
return clearInterval(interval);
|
||||||
|
}
|
||||||
|
this.createShard();
|
||||||
|
}, 5500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = ShardingManager;
|
||||||
26
test/shard.js
Normal file
26
test/shard.js
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
const Discord = require('../');
|
||||||
|
const { token } = require('./auth.json');
|
||||||
|
|
||||||
|
const client = new Discord.Client({
|
||||||
|
shard_id: process.argv[2],
|
||||||
|
shard_count: process.argv[3],
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on('message', msg => {
|
||||||
|
if (msg.content.startsWith('?eval') && msg.author.id === '66564597481480192') {
|
||||||
|
try {
|
||||||
|
const com = eval(msg.content.split(' ').slice(1).join(' '));
|
||||||
|
msg.channel.sendMessage('```\n' + com + '```');
|
||||||
|
} catch (e) {
|
||||||
|
msg.channel.sendMessage('```\n' + e + '```');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
process.send(123);
|
||||||
|
|
||||||
|
client.on('ready', () => {
|
||||||
|
console.log('Ready');
|
||||||
|
});
|
||||||
|
|
||||||
|
client.login(token).catch(console.log);
|
||||||
7
test/sharder.js
Normal file
7
test/sharder.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
const Discord = require('../');
|
||||||
|
|
||||||
|
const sharder = new Discord.ShardingManager(`${process.cwd()}/test/shard.js`);
|
||||||
|
|
||||||
|
sharder.on('launch', id => console.log(`launched ${id}`));
|
||||||
|
|
||||||
|
sharder.spawn(5);
|
||||||
Reference in New Issue
Block a user