Skip to content

Add functionality to nodes to transfer all players to other nodes. #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class LavalinkClient(val userId: Long) : Closeable, Disposable {
transferNodes(node)
}

private fun transferNodes(node: LavalinkNode) {
internal fun transferNodes(node: LavalinkNode) {
linkMap.forEach { (_, link) ->
if (link.node == node) {
val voiceRegion = link.cachedPlayer?.voiceRegion
Expand Down
26 changes: 25 additions & 1 deletion src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class LavalinkNode(
var available: Boolean = false
internal set

// TODO: Maybe it would make sense to add some node state enums like the links have.
var transferring: Boolean = false
internal set

/**
* A local player cache, allows us to not call the rest client every time we need a player.
*/
Expand Down Expand Up @@ -183,7 +187,10 @@ class LavalinkNode(
return rest.destroyPlayer(guildId)
.doOnSuccess {
removeCachedPlayer(guildId)
lavalink.removeDestroyedLink(guildId)
// When transferring to a new node preserve the link
if (!transferring) {
lavalink.removeDestroyedLink(guildId)
}
}
}

Expand Down Expand Up @@ -473,6 +480,23 @@ class LavalinkNode(
}
}

/**
* Puts the node into a transferring state, then starts transferring all players to other nodes.
* Once the node is put into this state, a reconnect or calling transferComplete() removes it from that state.
* Transferring state does not remove links from cache when deleting off this node, unless the transfer also fails.
*/
fun transferPlayersToOtherNodes() {
transferring = true
lavalink.transferNodes(this)
}

/**
* Manually removes this node from the transfer state.
*/
fun transferComplete() {
transferring = false
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class LavalinkSocket(private val node: LavalinkNode) : WebSocketListener(), Clos

node.sessionId = sessionId
node.available = true
node.transferring = false
logger.info("${node.name} is ready with session id $sessionId")

node.playerCache.values.forEach { player ->
Expand Down
Loading