summaryrefslogtreecommitdiff
path: root/sw-server/src/main/kotlin/org/luxons
diff options
context:
space:
mode:
authorJoffrey Bion <joffrey.bion@gmail.com>2021-09-07 18:57:52 +0200
committerJoffrey Bion <joffrey.bion@gmail.com>2021-09-07 18:57:52 +0200
commit2ee70d9d02c15e1d4b7ab220318117502da77f60 (patch)
tree3712d4f4ecb3341bc48c228e7ba2e4b3e24235c6 /sw-server/src/main/kotlin/org/luxons
parentCleanup test dependencies thanks to Kotlin 1.5 (diff)
downloadseven-wonders-2ee70d9d02c15e1d4b7ab220318117502da77f60.tar.gz
seven-wonders-2ee70d9d02c15e1d4b7ab220318117502da77f60.tar.bz2
seven-wonders-2ee70d9d02c15e1d4b7ab220318117502da77f60.zip
Remove GlobalScope usage in LobbyController
Diffstat (limited to 'sw-server/src/main/kotlin/org/luxons')
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt7
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/utils/CoroutineScopedComponent.kt18
2 files changed, 21 insertions, 4 deletions
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()
+ }
+}
bgstack15