mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 18:43:31 +01:00
fix(WebSocketManager#connect): check if we have enough sessions (#8481)
* fix(WebSocketManager#connect): check if we have enough sessions * fix: more useful error message * fix: tests Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -183,6 +183,34 @@ test('strategies', async () => {
|
|||||||
const strategy = new MockStrategy();
|
const strategy = new MockStrategy();
|
||||||
manager.setStrategy(strategy);
|
manager.setStrategy(strategy);
|
||||||
|
|
||||||
|
const data: APIGatewayBotInfo = {
|
||||||
|
shards: 1,
|
||||||
|
session_start_limit: {
|
||||||
|
max_concurrency: 3,
|
||||||
|
reset_after: 60,
|
||||||
|
remaining: 3,
|
||||||
|
total: 3,
|
||||||
|
},
|
||||||
|
url: 'wss://gateway.discord.gg',
|
||||||
|
};
|
||||||
|
|
||||||
|
const fetch = vi.fn(() => ({
|
||||||
|
data,
|
||||||
|
statusCode: 200,
|
||||||
|
responseOptions: {
|
||||||
|
headers: {
|
||||||
|
'content-type': 'application/json',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
mockPool
|
||||||
|
.intercept({
|
||||||
|
path: '/api/v10/gateway/bot',
|
||||||
|
method: 'GET',
|
||||||
|
})
|
||||||
|
.reply(fetch);
|
||||||
|
|
||||||
await manager.connect();
|
await manager.connect();
|
||||||
expect(strategy.spawn).toHaveBeenCalledWith(shardIds);
|
expect(strategy.spawn).toHaveBeenCalledWith(shardIds);
|
||||||
expect(strategy.connect).toHaveBeenCalled();
|
expect(strategy.connect).toHaveBeenCalled();
|
||||||
|
|||||||
@@ -271,6 +271,16 @@ export class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> {
|
|||||||
|
|
||||||
public async connect() {
|
public async connect() {
|
||||||
const shardCount = await this.getShardCount();
|
const shardCount = await this.getShardCount();
|
||||||
|
|
||||||
|
const data = await this.fetchGatewayInformation();
|
||||||
|
if (data.session_start_limit.remaining < shardCount) {
|
||||||
|
throw new Error(
|
||||||
|
`Not enough sessions remaining to spawn ${shardCount} shards; only ${
|
||||||
|
data.session_start_limit.remaining
|
||||||
|
} remaining; resets at ${new Date(Date.now() + data.session_start_limit.reset_after).toISOString()}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// First, make sure all our shards are spawned
|
// First, make sure all our shards are spawned
|
||||||
await this.updateShardCount(shardCount);
|
await this.updateShardCount(shardCount);
|
||||||
await this.strategy.connect();
|
await this.strategy.connect();
|
||||||
|
|||||||
Reference in New Issue
Block a user