Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rewrite concurrency manager #8

Merged
merged 17 commits into from
Dec 22, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: missing auth header
  • Loading branch information
Deivu committed Dec 20, 2024
commit 9e910d32ddb3444e329e89d844074120fd8e3ab5
2 changes: 1 addition & 1 deletion src/client/ShardClient.ts
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ export class ShardClient {
clientOptions.shardCount = EnvProcessData.shardCount;
if (manager.handleConcurrency) {
if (!clientOptions.ws) clientOptions.ws = {};
clientOptions.ws.buildIdentifyThrottler = () => Promise.resolve(new ConcurrencyClient(new BaseWorker()));
clientOptions.ws.buildIdentifyThrottler = () => Promise.resolve(new ConcurrencyClient());
}
this.client = new manager.client(clientOptions);
// @ts-expect-error: Override shard client util with indomitable shard client util
15 changes: 9 additions & 6 deletions src/concurrency/ConcurrencyClient.ts
Original file line number Diff line number Diff line change
@@ -5,13 +5,11 @@ import { BaseWorker } from '../ipc/BaseWorker';
* Internal class that is passed to @discordjs/ws to handle concurrency
*/
export class ConcurrencyClient {
private ipc: BaseWorker;
private readonly address: string;
private readonly port: number;
private readonly password: string;

constructor(ipc: BaseWorker) {
this.ipc = ipc;
constructor() {
this.address = process.env.INDOMITABLE_CONCURRENCY_SERVER_ADDRESS!;
this.port = Number(process.env.INDOMITABLE_CONCURRENCY_SERVER_PORT!);
this.password = process.env.INDOMITABLE_CONCURRENCY_SERVER_PASSWORD!;
@@ -24,7 +22,10 @@ export class ConcurrencyClient {
const url = new URL(`http://${this.address}:${this.port}/concurrency/acquire`);
url.searchParams.append('shardId', shardId.toString());

const response = await Fetch(url.toString(), { method: 'POST' });
const response = await Fetch(url.toString(), {
method: 'POST',
headers: { authorization: this.password }
});

// 202 = acquire lock cancelled, 204 = success, allow identify, 4xx = something bad happened
if (response.code !== 204) {
@@ -39,7 +40,9 @@ export class ConcurrencyClient {
const url = new URL(`http://${this.address}:${this.port}/concurrency/cancel`);
url.searchParams.append('shardId', shardId.toString());

Fetch(url.toString(), { method: 'DELETE' })
.catch(() => null);
Fetch(url.toString(), {
method: 'DELETE',
headers: { authorization: this.password }
}).catch(() => null);
}
}
7 changes: 0 additions & 7 deletions src/ipc/MainWorker.ts
Original file line number Diff line number Diff line change
@@ -58,13 +58,6 @@ export class MainWorker extends BaseIpc {
message.reply(manager.cachedSession);
break;
}
case InternalOps.REQUEST_IDENTIFY:
await manager.concurrencyManager!.waitForIdentify(content.data.shardId);
message.reply(null);
break;
case InternalOps.CANCEL_IDENTIFY:
manager.concurrencyManager!.abortIdentify(content.data.shardId);
break;
case InternalOps.RESTART:
await manager.restart(content.data.clusterId);
break;
Loading