Skip to content
This repository has been archived by the owner on Nov 13, 2022. It is now read-only.

Selection of the ideal Lavalink node according to the location of the Discord server #88

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
45 changes: 43 additions & 2 deletions src/structures/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,49 @@ export class Manager extends EventEmitter {
});
}

/**
* Initiates the Manager class.
/**
* Returns the Node in same region as server.
* @param region
*/
public nearestNode(region: string): Node {
const nodes = this.nodes
.filter((node) => {
if (!node.options.region)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Expected { after 'if' condition.

Suggested change
if (!node.options.region)
if (!node.options.region)

return node.connected
else if (Array.isArray(node.options.region))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Expected { after 'if' condition.

Suggested change
else if (Array.isArray(node.options.region))
else if (Array.isArray(node.options.region))

return node.connected && node.options.region.includes(region)
else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Expected { after 'else'.

Suggested change
else
else

return node.connected && node.options.region.toLowerCase() == region.toLowerCase()
});

if (!nodes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Expected { after 'if' condition.

Suggested change
if (!nodes)
if (!nodes)

return null;
else if (nodes.size === 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Expected { after 'if' condition.

Suggested change
else if (nodes.size === 1)
else if (nodes.size === 1)

return nodes.first();
else if (nodes.size > 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Expected { after 'if' condition.

Suggested change
else if (nodes.size > 1)
else if (nodes.size > 1)

return this.leastLoadNodesByRegion(nodes).first();
}

/**
* Returns the least system load Nodes from provided Nodes.
* @param nodes
*/
public leastLoadNodesByRegion(nodes: Collection<string, Node>): Collection<string, Node> {
return nodes
.filter((node) => node.connected)
.sort((a, b) => {
const aload = a.stats.cpu
? (a.stats.cpu.systemLoad / a.stats.cpu.cores) * 100
: 0;
const bload = b.stats.cpu
? (b.stats.cpu.systemLoad / b.stats.cpu.cores) * 100
: 0;
return aload - bload;
});
}

/**
* Initiates the Manager class.
* @param options
*/
constructor(options: ManagerOptions) {
Expand Down
7 changes: 7 additions & 0 deletions src/structures/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ function check(options: NodeOptions) {
typeof options.retryDelay !== "number"
)
throw new TypeError('Node option "retryDelay" must be a number.');

if (typeof options.region !== "undefined" &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Expected { after 'if' condition.

Suggested change
if (typeof options.region !== "undefined" &&
if (typeof options.region !== "undefined" &&

((typeof options.region !== "string" ||
!/.+/.test(options.region)) && (!Array.isArray(options.region) || options.region.length === 0 || !/.+/.test(options.region[0]))))
throw new TypeError('Node option "region" must be a non-empty string or array.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Strings must use doublequote.

Suggested change
throw new TypeError('Node option "region" must be a non-empty string or array.');
throw new TypeError("Node option \"region\" must be a non-empty string or array.");

}

export class Node {
Expand Down Expand Up @@ -390,6 +395,8 @@ export interface NodeOptions {
retryAmount?: number;
/** The retryDelay for the node. */
retryDelay?: number;
/** Regions for which the node can be used */
region?: string | string[];
}

export interface NodeStats {
Expand Down
11 changes: 10 additions & 1 deletion src/structures/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ function check(options: PlayerOptions) {
throw new TypeError(
'Player option "guild" must be present and be a non-empty string.'
);

if (options.region && !/^\d+$/.test(options.region))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Expected { after 'if' condition.

Suggested change
if (options.region && !/^\d+$/.test(options.region))
if (options.region && !/^\d+$/.test(options.region))

throw new TypeError('Player option "region" must be a non-empty string.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Strings must use doublequote.

Suggested change
throw new TypeError('Player option "region" must be a non-empty string.');
throw new TypeError("Player option \"region\" must be a non-empty string.");


if (options.textChannel && !/^\d+$/.test(options.textChannel))
throw new TypeError(
Expand Down Expand Up @@ -62,6 +65,8 @@ export class Player {
public node: Node;
/** The guild the player. */
public guild: string;
/** The region of Discord server. */
public region: string | null = null;
/** The voice channel for the player. */
public voiceChannel: string | null = null;
/** The text channel for the player. */
Expand Down Expand Up @@ -119,7 +124,9 @@ export class Player {
if (options.textChannel) this.textChannel = options.textChannel;

const node = this.manager.nodes.get(options.node);
this.node = node || this.manager.leastLoadNodes.first();
if (node) this.node = node
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Expected { after 'if' condition.

Suggested change
if (node) this.node = node
if (node) {this.node = node}

else if (options.region) this.node = this.manager.nearestNode(options.region);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Expected { after 'if' condition.

Suggested change
else if (options.region) this.node = this.manager.nearestNode(options.region);
else if (options.region) {this.node = this.manager.nearestNode(options.region);}

if (!this.node) this.node = this.manager.leastLoadNodes.first()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Expected { after 'if' condition.

Suggested change
if (!this.node) this.node = this.manager.leastLoadNodes.first()
if (!this.node) {this.node = this.manager.leastLoadNodes.first()}


if (!this.node) throw new RangeError("No available nodes.");

Expand Down Expand Up @@ -448,6 +455,8 @@ export class Player {
export interface PlayerOptions {
/** The guild the Player belongs to. */
guild: string;
/** The region of Discord server. */
region?: string;
/** The text channel the Player belongs to. */
textChannel: string;
/** The voice channel the Player belongs to. */
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,4 @@ export interface PlayerUpdate {
time: number;
};
guildId: string;
}
}