mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 10:03:31 +01:00
Add worker-based sharding to the ShardingManager (#2908)
* Add worker-based sharding mode to ShardingManager * Fix ClientShardUtil mode * Fix worker not being cleared on shard death * Update docs and typings * Clean up Client sharding logic a bit * Add info about requirements for worker mode
This commit is contained in:
committed by
GitHub
parent
b759fc415e
commit
ab3a439198
@@ -31,12 +31,30 @@ class Client extends BaseClient {
|
||||
constructor(options = {}) {
|
||||
super(Object.assign({ _tokenType: 'Bot' }, options));
|
||||
|
||||
// Obtain shard details from environment
|
||||
if (!browser && !this.options.shardId && 'SHARD_ID' in process.env) {
|
||||
this.options.shardId = Number(process.env.SHARD_ID);
|
||||
}
|
||||
if (!browser && !this.options.shardCount && 'SHARD_COUNT' in process.env) {
|
||||
this.options.shardCount = Number(process.env.SHARD_COUNT);
|
||||
// Figure out the shard details
|
||||
if (!browser && process.env.SHARDING_MANAGER) {
|
||||
// Try loading workerData if it's present
|
||||
let workerData;
|
||||
try {
|
||||
workerData = require('worker_threads').workerData;
|
||||
} catch (err) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
if (!this.options.shardId) {
|
||||
if (workerData && 'SHARD_ID' in workerData) {
|
||||
this.options.shardId = workerData.SHARD_ID;
|
||||
} else if ('SHARD_ID' in process.env) {
|
||||
this.options.shardId = Number(process.env.SHARD_ID);
|
||||
}
|
||||
}
|
||||
if (!this.options.shardCount) {
|
||||
if (workerData && 'SHARD_COUNT' in workerData) {
|
||||
this.options.shardCount = workerData.SHARD_COUNT;
|
||||
} else if ('SHARD_COUNT' in process.env) {
|
||||
this.options.shardCount = Number(process.env.SHARD_COUNT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this._validateOptions();
|
||||
@@ -73,7 +91,9 @@ class Client extends BaseClient {
|
||||
* Shard helpers for the client (only if the process was spawned from a {@link ShardingManager})
|
||||
* @type {?ShardClientUtil}
|
||||
*/
|
||||
this.shard = !browser && process.env.SHARDING_MANAGER ? ShardClientUtil.singleton(this) : null;
|
||||
this.shard = !browser && process.env.SHARDING_MANAGER ?
|
||||
ShardClientUtil.singleton(this, process.env.SHARDING_MANAGER_MODE) :
|
||||
null;
|
||||
|
||||
/**
|
||||
* All of the {@link User} objects that have been cached at any point, mapped by their IDs
|
||||
|
||||
Reference in New Issue
Block a user