Skip to content

Commit

Permalink
Breaking change: Make ILoadBalancer guild-aware (#23)
Browse files Browse the repository at this point in the history
Co-authored-by: Freya Arbjerg <freya@arbjerg.dev>
  • Loading branch information
duncte123 and freyacodes committed Mar 10, 2024
1 parent 3047756 commit 4d7568c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class LavalinkClient(val userId: Long) : Closeable, Disposable {
@JvmOverloads
fun getOrCreateLink(guildId: Long, region: VoiceRegion? = null): Link {
if (!linkMap.containsKey(guildId)) {
val bestNode = loadBalancer.selectNode(region)
val bestNode = loadBalancer.selectNode(region, guildId)
linkMap[guildId] = Link(guildId, bestNode)
}

Expand Down Expand Up @@ -205,7 +205,7 @@ class LavalinkClient(val userId: Long) : Closeable, Disposable {

link.state = LinkState.CONNECTING
// The delay is used to prevent a race condition in Discord, causing close code 4006
link.transferNode(loadBalancer.selectNode(region = voiceRegion), delay = Duration.ofMillis(1000))
link.transferNode(loadBalancer.selectNode(region = voiceRegion, link.guildId), delay = Duration.ofMillis(1000))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ interface ILoadBalancer {
* @see #selectNode(VoiceRegion)
*/
fun selectNode(): LavalinkNode {
return selectNode(null)
return selectNode(null, null)
}

/**
* Selects a node based on the criteria of the load balancer.
* @param region A voice region may be provided to filter on the closest region to this node
* @param guildId The ID of the guild to be associated with the returned node
*
* @return The best node that matches the criteria
* @throws RuntimeException when no nodes are available
*/
fun selectNode(region: VoiceRegion?): LavalinkNode
fun selectNode(region: VoiceRegion?, guildId: Long?): LavalinkNode

/**
* Adds a penalty provider to the load balancer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DefaultLoadBalancer(private val client: LavalinkClient) : ILoadBalancer {
penaltyProviders.remove(penaltyProvider)
}

override fun selectNode(region: VoiceRegion?): LavalinkNode {
override fun selectNode(region: VoiceRegion?, guildId: Long?): LavalinkNode {
val nodes = client.nodes

// Don't bother calculating penalties if we only have one node.
Expand Down

0 comments on commit 4d7568c

Please sign in to comment.