Skip to content

Commit 9f2c985

Browse files
committed
Fix removing cached player from new node instead of old
Also manages the LinkState a bit more consistently
1 parent 624e94c commit 9f2c985

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

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

-2
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,6 @@ class LavalinkClient(val userId: Long) : Closeable, Disposable {
187187
linkMap.forEach { (_, link) ->
188188
if (link.node == node) {
189189
val voiceRegion = link.cachedPlayer?.voiceRegion
190-
191-
link.state = LinkState.CONNECTING
192190
// The delay is used to prevent a race condition in Discord, causing close code 4006
193191
link.transferNode(loadBalancer.selectNode(region = voiceRegion, link.guildId), delay = Duration.ofMillis(1000))
194192
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ class LavalinkNode(
427427
*/
428428
fun getCachedPlayer(guildId: Long): LavalinkPlayer? = playerCache[guildId]
429429

430+
internal fun getAndRemoveCachedPlayer(guildId: Long): LavalinkPlayer? = playerCache.remove(guildId)
431+
430432
/**
431433
* @return an unmodifiable view of all cached players for this node.
432434
*/

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,19 @@ class Link(
5858
fun loadItem(identifier: String) = node.loadItem(identifier)
5959

6060
internal fun transferNode(newNode: LavalinkNode, delay: Duration = Duration.ZERO) {
61-
val player = node.getCachedPlayer(guildId)
61+
val player = node.getAndRemoveCachedPlayer(guildId)
6262

6363
if (player != null) {
64+
state = LinkState.CONNECTING
6465
newNode.createOrUpdatePlayer(guildId)
6566
.applyBuilder(player.stateToBuilder())
6667
.delaySubscription(delay)
67-
.subscribe({
68-
node.removeCachedPlayer(guildId)
69-
}) {
68+
.doOnError {
7069
state = LinkState.DISCONNECTED
7170
logger.error("Failed to transfer player to new node: ${newNode.name}", it)
72-
}
71+
}.subscribe()
72+
} else {
73+
state = LinkState.DISCONNECTED
7374
}
7475

7576
node = newNode

0 commit comments

Comments
 (0)