From afb575d0e3026c0ef028dd7dc44423240085cc8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Levilain?= Date: Thu, 2 Nov 2023 23:17:16 +0100 Subject: [PATCH] fix(shulker-proxy-agent): use a dedicated thread pool for redis pubsub --- .../proxyagent/ShulkerProxyAgentCommon.kt | 7 +++-- .../adapters/pubsub/RedisPubSubAdapter.kt | 31 ++++++++++++------- .../proxyagent/commands/ListCommandHandler.kt | 4 ++- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/ShulkerProxyAgentCommon.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/ShulkerProxyAgentCommon.kt index 264f7971..6e739563 100644 --- a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/ShulkerProxyAgentCommon.kt +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/ShulkerProxyAgentCommon.kt @@ -10,10 +10,10 @@ import io.shulkermc.proxyagent.adapters.kubernetes.ImplKubernetesGatewayAdapter import io.shulkermc.proxyagent.adapters.kubernetes.KubernetesGatewayAdapter import io.shulkermc.proxyagent.adapters.mojang.HttpMojangGatewayAdapter import io.shulkermc.proxyagent.adapters.mojang.MojangGatewayAdapter -import io.shulkermc.proxyagent.adapters.pubsub.PubSubAdapter import io.shulkermc.proxyagent.adapters.pubsub.RedisPubSubAdapter import io.shulkermc.proxyagent.api.ShulkerProxyAPI import io.shulkermc.proxyagent.api.ShulkerProxyAPIImpl +import io.shulkermc.proxyagent.handlers.TeleportPlayerOnServerHandler import io.shulkermc.proxyagent.services.PlayerMovementService import io.shulkermc.proxyagent.services.ProxyLifecycleService import io.shulkermc.proxyagent.services.ServerDirectoryService @@ -33,7 +33,7 @@ class ShulkerProxyAgentCommon(val proxyInterface: ProxyInterface, val logger: Lo lateinit var fileSystem: FileSystemAdapter lateinit var mojangGateway: MojangGatewayAdapter lateinit var cache: CacheAdapter - lateinit var pubSub: PubSubAdapter + lateinit var pubSub: RedisPubSubAdapter // Services lateinit var serverDirectoryService: ServerDirectoryService @@ -69,7 +69,7 @@ class ShulkerProxyAgentCommon(val proxyInterface: ProxyInterface, val logger: Lo this.playerMovementService = PlayerMovementService(this) this.proxyLifecycleService = ProxyLifecycleService(this) - // this.pubSub.onTeleportPlayerOnServer(TeleportPlayerOnServerHandler(this)::handle) + this.pubSub.onTeleportPlayerOnServer(TeleportPlayerOnServerHandler(this)::handle) this.healthcheckTask = HealthcheckTask(this).schedule() this.lostProxyPurgeTask = LostProxyPurgeTask(this).schedule() @@ -104,6 +104,7 @@ class ShulkerProxyAgentCommon(val proxyInterface: ProxyInterface, val logger: Lo fun shutdown() { try { this.cache.unregisterProxy(Configuration.PROXY_NAME) + this.pubSub.close() this.agonesGateway.askShutdown() } catch (ex: Exception) { this.logger.severe("Failed to ask Agones sidecar to shutdown properly, stopping process manually") diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/pubsub/RedisPubSubAdapter.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/pubsub/RedisPubSubAdapter.kt index bcaa4e68..a8b12609 100644 --- a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/pubsub/RedisPubSubAdapter.kt +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/adapters/pubsub/RedisPubSubAdapter.kt @@ -2,8 +2,15 @@ package io.shulkermc.proxyagent.adapters.pubsub import redis.clients.jedis.JedisPool import redis.clients.jedis.JedisPubSub +import java.util.concurrent.Executors + +class RedisPubSubAdapter(private val jedisPool: JedisPool) : PubSubAdapter, AutoCloseable { + private val executor = Executors.newCachedThreadPool() + + override fun close() { + this.executor.shutdownNow() + } -class RedisPubSubAdapter(private val jedisPool: JedisPool) : PubSubAdapter { override fun teleportPlayerOnServer(playerId: String, serverName: String) { this.jedisPool.resource.use { jedis -> jedis.publish("shulker:teleport", "$playerId:$serverName") @@ -11,16 +18,18 @@ class RedisPubSubAdapter(private val jedisPool: JedisPool) : PubSubAdapter { } override fun onTeleportPlayerOnServer(callback: (playerId: String, serverName: String) -> Unit) { - this.jedisPool.resource.use { jedis -> - jedis.subscribe( - object : JedisPubSub() { - override fun onMessage(channel: String, message: String) { - val (playerId, serverName) = message.split(":") - callback(playerId, serverName) - } - }, - "shulker:teleport" - ) + this.executor.submit { + this.jedisPool.resource.use { jedis -> + jedis.subscribe( + object : JedisPubSub() { + override fun onMessage(channel: String, message: String) { + val (playerId, serverName) = message.split(":") + callback(playerId, serverName) + } + }, + "shulker:teleport" + ) + } } } } diff --git a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/ListCommandHandler.kt b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/ListCommandHandler.kt index 4d8aad32..a03ff696 100644 --- a/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/ListCommandHandler.kt +++ b/packages/shulker-proxy-agent/src/common/kotlin/io/shulkermc/proxyagent/commands/ListCommandHandler.kt @@ -18,6 +18,7 @@ object ListCommandHandler { serverNames.mapIndexed { index, serverName -> val boxCharacter = if (index == serverNames.size - 1) "└" else "├" val playerNames = agent.cache.getPlayerNamesFromIds(agent.cache.listPlayersInServer(serverName)).values + val playerNamesJoined = playerNames .sortedBy { it.lowercase() } .joinToString(", ") { it } @@ -25,8 +26,9 @@ object ListCommandHandler { Component.text("$boxCharacter ") .color(NamedTextColor.DARK_GRAY) .append(Component.text(serverName).color(NamedTextColor.YELLOW)) + .append(Component.text(" (${playerNames.size})").color(NamedTextColor.YELLOW)) .append(Component.text(": ").color(NamedTextColor.WHITE)) - .append(Component.text(playerNames).color(NamedTextColor.WHITE)) + .append(Component.text(playerNamesJoined).color(NamedTextColor.WHITE)) ) } }