mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 18:43:31 +01:00
work on sharding
This commit is contained in:
29
src/sharding/Shard.js
Normal file
29
src/sharding/Shard.js
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
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.process.on('message', m => {
|
||||||
|
if (m && m.type && m.id) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
send(type, timeout = 60000, data = {}) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const id = crypto.randomBytes(16).toString('hex');
|
||||||
|
this.process.send({
|
||||||
|
type,
|
||||||
|
id,
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Shard;
|
||||||
@@ -2,6 +2,8 @@ const childProcess = require('child_process');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
const Collection = require('../util/Collection');
|
const Collection = require('../util/Collection');
|
||||||
|
const Shard = require('./Shard');
|
||||||
|
const crypto = require('crypto');
|
||||||
|
|
||||||
class ShardingManager extends EventEmitter {
|
class ShardingManager extends EventEmitter {
|
||||||
constructor(file, totalShards) {
|
constructor(file, totalShards) {
|
||||||
@@ -12,11 +14,12 @@ class ShardingManager extends EventEmitter {
|
|||||||
}
|
}
|
||||||
this.totalShards = totalShards;
|
this.totalShards = totalShards;
|
||||||
this.shards = new Collection();
|
this.shards = new Collection();
|
||||||
|
this.waiting = new Collection();
|
||||||
}
|
}
|
||||||
|
|
||||||
createShard() {
|
createShard() {
|
||||||
const id = this.shards.size;
|
const id = this.shards.size;
|
||||||
const shard = childProcess.fork(path.resolve(this.file), [id, this.totalShards], { silent: true });
|
const shard = new Shard(this, id);
|
||||||
this.shards.set(id, shard);
|
this.shards.set(id, shard);
|
||||||
this.emit('launch', id, shard);
|
this.emit('launch', id, shard);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user