diff --git a/src/client/ShardClient.ts b/src/client/ShardClient.ts index c52389d..e3a3f3a 100644 --- a/src/client/ShardClient.ts +++ b/src/client/ShardClient.ts @@ -1,6 +1,6 @@ import type { Client, ClientOptions as DiscordJsClientOptions } from 'discord.js'; import { Indomitable } from '../Indomitable'; -import {EnvProcessData, ClientEvents, ClientEventData, Delay} from '../Util'; +import { EnvProcessData, ClientEvents, ClientEventData, Delay } from '../Util'; import { ShardClientUtil } from './ShardClientUtil'; import { ConcurrencyClient } from '../concurrency/ConcurrencyClient'; @@ -58,9 +58,8 @@ export class ShardClient { if (this.concurrency) { // tests the server if it's accessible first before starting the client this.client.emit('debug', '[Indomitable]: Handle concurrency enabled! Testing the identify server before logging in...'); - await this.concurrency.waitForIdentify(0, new AbortSignal()) - this.client.emit('debug', '[Indomitable]: Identify server responded and is working, waiting 5s before starting...'); - await Delay(5000); + const date = await this.concurrency.checkServer(); + this.client.emit('debug', `[Indomitable]: Identify server responded and is working, Trip Latency: ${Math.round(Date.now() - date)}ms`); } // attach listeners this.client.once('ready', () => this.send({ op: ClientEvents.READY, data: { clusterId: this.clusterId }})); diff --git a/src/concurrency/ConcurrencyClient.ts b/src/concurrency/ConcurrencyClient.ts index d200eb5..1bc3d5a 100644 --- a/src/concurrency/ConcurrencyClient.ts +++ b/src/concurrency/ConcurrencyClient.ts @@ -59,4 +59,14 @@ export class ConcurrencyClient { signal.removeEventListener('abort', listener); } } + + public async checkServer(): Promise { + const url = new URL(`http://${this.address}:${this.port}/concurrency/acquire`); + url.searchParams.append('shardId', '0'); + const response = await Fetch(url.toString(), { + method: 'POST', + headers: { authorization: this.password } + }); + return response.body; + } } \ No newline at end of file diff --git a/src/concurrency/ConcurrencyServer.ts b/src/concurrency/ConcurrencyServer.ts index 78eb43d..934a190 100644 --- a/src/concurrency/ConcurrencyServer.ts +++ b/src/concurrency/ConcurrencyServer.ts @@ -53,6 +53,8 @@ export class ConcurrencyServer { * @private */ private async handle(request: Http.IncomingMessage, response: Http.ServerResponse): Promise { + const now = Date.now(); + if (!request.url || request.method !== 'POST' && request.method !== 'DELETE') { response.statusCode = 404; response.statusMessage = 'Not Found'; @@ -101,6 +103,12 @@ export class ConcurrencyServer { } } + if (request.method === 'POST' && request.url.includes('/concurrency/check')) { + response.statusCode = 200; + response.statusMessage = 'OK'; + return void response.end(now); + } + response.statusCode = 404; response.statusMessage = 'Not Found'; return void response.end();