Skip to content

Commit 6deabc0

Browse files
Brainicismtaahamahdi
authored andcommitted
Add support for max_concurrency
1 parent 857b8de commit 6deabc0

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ declare namespace Eris {
291291
rest?: RequestHandlerOptions;
292292
restMode?: boolean;
293293
seedVoiceConnections?: boolean;
294+
useMaxConcurrency?: boolean;
294295
ws?: unknown;
295296
}
296297
interface CommandClientOptions {

lib/Client.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class Client extends EventEmitter {
148148
rest: {},
149149
restMode: false,
150150
seedVoiceConnections: false,
151+
useMaxConcurrency: false,
151152
ws: {},
152153
reconnectDelay: (lastDelay, attempts) => Math.pow(attempts + 1, 0.7) * 20000
153154
}, options);

lib/gateway/ShardManager.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ class ShardManager extends Collection {
88
constructor(client) {
99
super(Shard);
1010
this._client = client;
11-
11+
this.maxConcurrency = 1;
1212
this.connectQueue = [];
1313
this.lastConnect = 0;
1414
this.connectTimeout = null;
1515
}
1616

1717
connect(shard) {
18-
if(shard.sessionID || (this.lastConnect <= Date.now() - 5000 && !this.find((shard) => shard.connecting))) {
18+
if((shard.sessionID || (this.lastConnect <= Date.now() - 5000 && !this.find((shard) => shard.connecting))) && !this._client.options.useMaxConcurrency) {
1919
shard.connect();
2020
this.lastConnect = Date.now() + 7500;
2121
} else {
@@ -95,17 +95,31 @@ class ShardManager extends Collection {
9595
}
9696
}
9797

98+
// https://github.com/curtisf/eris/commit/6b8397da17c2fabf37f5c08f40899c5131d599fb#diff-df83f51ebdee7648e64537556e67de936a1f8a78c2e4459b49a5b850a35cdfa2
9899
tryConnect() {
99100
if(this.connectQueue.length > 0) {
100-
if(this.lastConnect <= Date.now() - 5000) {
101-
const shard = this.connectQueue.shift();
102-
shard.connect();
103-
this.lastConnect = Date.now() + 7500;
104-
} else if(!this.connectTimeout) {
105-
this.connectTimeout = setTimeout(() => {
106-
this.connectTimeout = null;
107-
this.tryConnect();
108-
}, 1000);
101+
if(!this._client.options.useMaxConcurrency) {
102+
if(this.lastConnect <= Date.now() - 5000) {
103+
const shard = this.connectQueue.shift();
104+
shard.connect();
105+
this.lastConnect = Date.now() + 7500;
106+
} else if(!this.connectTimeout) {
107+
this.connectTimeout = setTimeout(() => {
108+
this.connectTimeout = null;
109+
this.tryConnect();
110+
}, 1000);
111+
}
112+
} else {
113+
if(this.filter((shard) => shard.connecting).length < this.maxConcurrency) {
114+
const shard = this.connectQueue.shift();
115+
shard.connect();
116+
//this.lastConnect = Date.now() + 7500;
117+
} else if(!this.connectTimeout) {
118+
this.connectTimeout = setTimeout(() => {
119+
this.connectTimeout = null;
120+
this.tryConnect();
121+
}, 250);
122+
}
109123
}
110124
}
111125
}

0 commit comments

Comments
 (0)