Skip to content

Commit

Permalink
feat: add debug logs for concurrency server
Browse files Browse the repository at this point in the history
  • Loading branch information
Deivu committed Dec 22, 2024
1 parent 4520c4a commit 66208ea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Indomitable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ export class Indomitable extends EventEmitter {
}
if (this.handleConcurrency) {
const sessions = await this.fetchSessions();
this.concurrencyServer = new ConcurrencyServer(sessions.session_start_limit.max_concurrency);
this.concurrencyServer = new ConcurrencyServer(this, sessions.session_start_limit.max_concurrency);
const info = await this.concurrencyServer.start();
this.emit(LibraryEvents.DEBUG, `Handle concurrency is currently enabled! => Server is currently bound to:\n Address: ${info.address}:${info.port}\n Concurrency: ${sessions.session_start_limit.max_concurrency}`);
this.emit(LibraryEvents.DEBUG, `Handle concurrency is currently enabled! =>\nServer is currently bound to:\n Address: ${info.address}:${info.port}\n Concurrency: ${sessions.session_start_limit.max_concurrency}`);
}
if (typeof this.clusterCount !== 'number')
this.clusterCount = Os.cpus().length;
Expand Down
16 changes: 11 additions & 5 deletions src/concurrency/ConcurrencyServer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { AddressInfo } from 'node:net';
import { ConcurrencyManager } from './ConcurrencyManager';
import { Indomitable } from '../Indomitable';
import Http from 'node:http';
import {LibraryEvents} from "../Util";

/**
* Server that handles identify locks
*/
export class ConcurrencyServer {
private readonly manager: Indomitable;
/**
* Fastify instance of this server
* @private
Expand All @@ -15,15 +18,16 @@ export class ConcurrencyServer {
* Concurrency manager for this server
* @private
*/
private readonly manager: ConcurrencyManager;
private readonly concurrency: ConcurrencyManager;
/**
* Randomly generated password to secure this server
* @private
*/
private readonly password: string;
constructor(concurrency: number) {
constructor(manager: Indomitable, concurrency: number) {
this.manager = manager;
this.server = Http.createServer((req, res) => this.handle(req, res));
this.manager = new ConcurrencyManager(concurrency);
this.concurrency = new ConcurrencyManager(concurrency);
this.password = Math.random().toString(36).slice(2, 10);
}

Expand Down Expand Up @@ -76,16 +80,18 @@ export class ConcurrencyServer {
return void response.end('Expected shardId to be a number');
}

this.manager.emit(LibraryEvents.DEBUG, `Received a request in concurrency server! =>\n Url: ${request.url}\n Method: ${request.method}\n ShardId: ${shardId}`);

if (request.method === 'DELETE' && request.url.includes('/concurrency/cancel')) {
this.manager.abortIdentify(shardId);
this.concurrency.abortIdentify(shardId);
response.statusCode = 200;
response.statusMessage = 'OK';
return void response.end();
}

if (request.method === 'POST' && request.url.includes('/concurrency/acquire')) {
try {
await this.manager.waitForIdentify(shardId);
await this.concurrency.waitForIdentify(shardId);
response.statusCode = 204;
response.statusMessage = 'No Content';
return void response.end();
Expand Down

0 comments on commit 66208ea

Please sign in to comment.