-
Notifications
You must be signed in to change notification settings - Fork 80
Selection of the ideal Lavalink node according to the location of the Discord server #88
base: master
Are you sure you want to change the base?
Changes from all commits
dcbea31
9e12f6e
98b7ca2
14da024
4227345
1f66ee8
e7120af
d03e5e7
bf5cc51
1cd3265
07c8224
49ce719
fd7e259
1b0d809
8d6b7ca
bb2614e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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) | ||||||
return node.connected | ||||||
else if (Array.isArray(node.options.region)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
return node.connected && node.options.region.includes(region) | ||||||
else | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Codacy has a fix for the issue: Expected { after 'else'.
Suggested change
|
||||||
return node.connected && node.options.region.toLowerCase() == region.toLowerCase() | ||||||
}); | ||||||
|
||||||
if (!nodes) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
return null; | ||||||
else if (nodes.size === 1) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
return nodes.first(); | ||||||
else if (nodes.size > 1) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
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) { | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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" && | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
((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.'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
} | ||||||
|
||||||
export class Node { | ||||||
|
@@ -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 { | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
throw new TypeError('Player option "region" must be a non-empty string.'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
|
||||||
if (options.textChannel && !/^\d+$/.test(options.textChannel)) | ||||||
throw new TypeError( | ||||||
|
@@ -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. */ | ||||||
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) throw new RangeError("No available nodes."); | ||||||
|
||||||
|
@@ -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. */ | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -397,4 +397,4 @@ export interface PlayerUpdate { | |
time: number; | ||
}; | ||
guildId: string; | ||
} | ||
} |
There was a problem hiding this comment.
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.