From 7a58f68ede1a7a071e93d01b03a5116642efac73 Mon Sep 17 00:00:00 2001 From: Tommy Schmidt Date: Mon, 24 Apr 2023 18:29:52 +0200 Subject: [PATCH] fix(memory-leak): reuse the SourceQueryClient (#68) note: this fixes the memory leak and also makes the queries faster --- .../vrising/discord/ServerQueryClient.kt | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/de/darkatra/vrising/discord/ServerQueryClient.kt b/src/main/kotlin/de/darkatra/vrising/discord/ServerQueryClient.kt index baeedd6..fd4fe50 100644 --- a/src/main/kotlin/de/darkatra/vrising/discord/ServerQueryClient.kt +++ b/src/main/kotlin/de/darkatra/vrising/discord/ServerQueryClient.kt @@ -5,34 +5,38 @@ import com.ibasco.agql.protocols.valve.source.query.SourceQueryClient import com.ibasco.agql.protocols.valve.source.query.SourceQueryOptions import com.ibasco.agql.protocols.valve.source.query.info.SourceServer import com.ibasco.agql.protocols.valve.source.query.players.SourcePlayer +import org.springframework.beans.factory.DisposableBean import org.springframework.stereotype.Service import java.net.InetSocketAddress @Service -class ServerQueryClient { +class ServerQueryClient : DisposableBean { - private val queryOptions = SourceQueryOptions.builder() - .option(GeneralOptions.CONNECTION_POOLING, true) - .build() + private val client by lazy { + SourceQueryClient( + SourceQueryOptions.builder() + .option(GeneralOptions.CONNECTION_POOLING, true) + .build() + ) + } fun getServerInfo(serverHostName: String, serverQueryPort: Int): SourceServer { val address = InetSocketAddress(serverHostName, serverQueryPort) - return SourceQueryClient(queryOptions).use { client -> - client.getInfo(address).join().result - } + return client.getInfo(address).join().result } fun getPlayerList(serverHostName: String, serverQueryPort: Int): List { val address = InetSocketAddress(serverHostName, serverQueryPort) - return SourceQueryClient(queryOptions).use { client -> - client.getPlayers(address).join().result - }.filter { player -> player.name.isNotBlank() } + return client.getPlayers(address).join().result + .filter { player -> player.name.isNotBlank() } } fun getRules(serverHostName: String, serverQueryPort: Int): Map { val address = InetSocketAddress(serverHostName, serverQueryPort) - return SourceQueryClient(queryOptions).use { client -> - client.getRules(address).join().result - } + return client.getRules(address).join().result + } + + override fun destroy() { + client.close() } }