Skip to content

Commit 4d7568c

Browse files
Breaking change: Make ILoadBalancer guild-aware (#23)
Co-authored-by: Freya Arbjerg <[email protected]>
1 parent 3047756 commit 4d7568c

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkClient.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class LavalinkClient(val userId: Long) : Closeable, Disposable {
138138
@JvmOverloads
139139
fun getOrCreateLink(guildId: Long, region: VoiceRegion? = null): Link {
140140
if (!linkMap.containsKey(guildId)) {
141-
val bestNode = loadBalancer.selectNode(region)
141+
val bestNode = loadBalancer.selectNode(region, guildId)
142142
linkMap[guildId] = Link(guildId, bestNode)
143143
}
144144

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

206206
link.state = LinkState.CONNECTING
207207
// The delay is used to prevent a race condition in Discord, causing close code 4006
208-
link.transferNode(loadBalancer.selectNode(region = voiceRegion), delay = Duration.ofMillis(1000))
208+
link.transferNode(loadBalancer.selectNode(region = voiceRegion, link.guildId), delay = Duration.ofMillis(1000))
209209
}
210210
}
211211
}

src/main/kotlin/dev/arbjerg/lavalink/client/loadbalancing/ILoadBalancer.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ interface ILoadBalancer {
1313
* @see #selectNode(VoiceRegion)
1414
*/
1515
fun selectNode(): LavalinkNode {
16-
return selectNode(null)
16+
return selectNode(null, null)
1717
}
1818

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

2829
/**
2930
* Adds a penalty provider to the load balancer.

src/main/kotlin/dev/arbjerg/lavalink/client/loadbalancing/builtin/DefaultLoadBalancer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class DefaultLoadBalancer(private val client: LavalinkClient) : ILoadBalancer {
1717
penaltyProviders.remove(penaltyProvider)
1818
}
1919

20-
override fun selectNode(region: VoiceRegion?): LavalinkNode {
20+
override fun selectNode(region: VoiceRegion?, guildId: Long?): LavalinkNode {
2121
val nodes = client.nodes
2222

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

0 commit comments

Comments
 (0)