Skip to content

Commit

Permalink
feat: add check endpoint and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Deivu committed Dec 22, 2024
1 parent 8d7a1ad commit 0f334d2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/client/ShardClient.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 }}));
Expand Down
10 changes: 10 additions & 0 deletions src/concurrency/ConcurrencyClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,14 @@ export class ConcurrencyClient {
signal.removeEventListener('abort', listener);
}
}

public async checkServer(): Promise<number> {
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;
}
}
8 changes: 8 additions & 0 deletions src/concurrency/ConcurrencyServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export class ConcurrencyServer {
* @private
*/
private async handle(request: Http.IncomingMessage, response: Http.ServerResponse): Promise<void> {
const now = Date.now();

if (!request.url || request.method !== 'POST' && request.method !== 'DELETE') {
response.statusCode = 404;
response.statusMessage = 'Not Found';
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0f334d2

Please sign in to comment.