Skip to content

Commit

Permalink
fix(memory-leak): reuse the SourceQueryClient (#68)
Browse files Browse the repository at this point in the history
note: this fixes the memory leak and also makes the queries faster
  • Loading branch information
DarkAtra authored Apr 24, 2023
1 parent 79689f9 commit 7a58f68
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/main/kotlin/de/darkatra/vrising/discord/ServerQueryClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<SourcePlayer> {
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<String, String> {
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()
}
}

0 comments on commit 7a58f68

Please sign in to comment.