From 2ee70d9d02c15e1d4b7ab220318117502da77f60 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Tue, 7 Sep 2021 18:57:52 +0200 Subject: Remove GlobalScope usage in LobbyController --- .../sevenwonders/server/controllers/LobbyController.kt | 7 +++---- .../server/utils/CoroutineScopedComponent.kt | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 sw-server/src/main/kotlin/org/luxons/sevenwonders/server/utils/CoroutineScopedComponent.kt diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt index 10a94579..788430c8 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt @@ -1,7 +1,6 @@ package org.luxons.sevenwonders.server.controllers import io.micrometer.core.instrument.MeterRegistry -import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withTimeoutOrNull @@ -20,6 +19,7 @@ import org.luxons.sevenwonders.server.lobby.Player import org.luxons.sevenwonders.server.metrics.playerCountsTags import org.luxons.sevenwonders.server.repositories.LobbyRepository import org.luxons.sevenwonders.server.repositories.PlayerRepository +import org.luxons.sevenwonders.server.utils.CoroutineScopedComponent import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Value import org.springframework.messaging.handler.annotation.MessageMapping @@ -28,7 +28,6 @@ import org.springframework.stereotype.Controller import org.springframework.validation.annotation.Validated import java.security.Principal import kotlin.time.Duration -import kotlin.time.milliseconds /** * Handles actions in the game's lobby. The lobby is the place where players gather before a game. @@ -40,7 +39,7 @@ class LobbyController( private val playerRepository: PlayerRepository, @Value("\${server.port}") private val serverPort: String, private val meterRegistry: MeterRegistry, -) { +) : CoroutineScopedComponent() { private val Principal.player: Player get() = playerRepository.get(name) @@ -160,7 +159,7 @@ class LobbyController( SevenWondersClient().connectBot("ws://localhost:$serverPort", action.botDisplayName, action.config) } logger.info("Starting bot {} in game '{}'", action.botDisplayName, lobby.name) - GlobalScope.launch { + componentScope.launch { val result = withTimeoutOrNull(action.globalBotTimeoutMillis) { bot.joinAndAutoPlay(lobby.id) } diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/utils/CoroutineScopedComponent.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/utils/CoroutineScopedComponent.kt new file mode 100644 index 00000000..932ed5ea --- /dev/null +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/utils/CoroutineScopedComponent.kt @@ -0,0 +1,18 @@ +package org.luxons.sevenwonders.server.utils + +import kotlinx.coroutines.CoroutineName +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.cancel +import javax.annotation.PreDestroy + +open class CoroutineScopedComponent { + + protected val componentScope: CoroutineScope = + CoroutineScope(CoroutineName("${this::class.simpleName}-coroutine") + SupervisorJob()) + + @PreDestroy + fun cancelScope() { + componentScope.cancel() + } +} -- cgit