Skip to content

Commit

Permalink
Add shutdown hook to close clients properly
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed Sep 5, 2024
1 parent 2af0559 commit 6c81821
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions LavalinkServer/src/main/java/lavalink/server/io/ShutdownHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package lavalink.server.io

import org.springframework.web.socket.CloseStatus

class ShutdownHandler(private val socketServer: SocketServer) : Thread("lavalink-shutdown-handler") {
init {
isDaemon = false // we want this thread to block shutdown until it has finished running
}

override fun run() {
socketServer.contexts.forEach {
// don't care about exceptions here, the JVM's shutting down anyway.
it.runCatching { closeWebSocket(CloseStatus.GOING_AWAY.code) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ final class SocketServer(
private val statsCollector = StatsCollector(this)
private val charPool = ('a'..'z') + ('0'..'9')

init {
Runtime.getRuntime().addShutdownHook(ShutdownHandler(this))
}

companion object {
private val log = LoggerFactory.getLogger(SocketServer::class.java)

Expand Down

0 comments on commit 6c81821

Please sign in to comment.