mirror of
https://github.com/danbulant/discord.js
synced 2026-06-09 01:31:29 +00:00
Add sharding manager
This commit is contained in:
parent
906672e538
commit
b1a25bd176
4 changed files with 2 additions and 68 deletions
File diff suppressed because one or more lines are too long
|
|
@ -1,48 +1,11 @@
|
||||||
const childProcess = require('child_process');
|
const childProcess = require('child_process');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const crypto = require('crypto');
|
|
||||||
|
|
||||||
class Shard {
|
class Shard {
|
||||||
constructor(manager, id) {
|
constructor(manager, id) {
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.process = childProcess.fork(path.resolve(this.manager.file), [id, this.manager.shards.size], { silent: true });
|
this.process = childProcess.fork(path.resolve(this.manager.file), [id, this.manager.shards.size]);
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
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;
|
|
||||||
|
|
@ -3,7 +3,6 @@ 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 Shard = require('./Shard');
|
||||||
const crypto = require('crypto');
|
|
||||||
|
|
||||||
class ShardingManager extends EventEmitter {
|
class ShardingManager extends EventEmitter {
|
||||||
constructor(file, totalShards) {
|
constructor(file, totalShards) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue