summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/StompDisconnectionListener.kt26
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt12
2 files changed, 33 insertions, 5 deletions
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/StompDisconnectionListener.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/StompDisconnectionListener.kt
index a33584e0..82e6bf55 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/StompDisconnectionListener.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/StompDisconnectionListener.kt
@@ -1,24 +1,42 @@
package org.luxons.sevenwonders.server.config
+import io.micrometer.core.instrument.MeterRegistry
import org.luxons.sevenwonders.server.controllers.GameController
import org.luxons.sevenwonders.server.controllers.LobbyController
import org.luxons.sevenwonders.server.repositories.PlayerRepository
-import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationListener
import org.springframework.stereotype.Component
+import org.springframework.web.socket.messaging.AbstractSubProtocolEvent
+import org.springframework.web.socket.messaging.SessionConnectedEvent
import org.springframework.web.socket.messaging.SessionDisconnectEvent
+import java.util.concurrent.atomic.AtomicInteger
@Component
internal class StompDisconnectionListener(
private val lobbyController: LobbyController,
private val playerRepository: PlayerRepository,
private val gameController: GameController,
-) : ApplicationListener<SessionDisconnectEvent> {
+ meterRegistry: MeterRegistry,
+) : ApplicationListener<AbstractSubProtocolEvent> {
- private val logger: Logger = LoggerFactory.getLogger(StompDisconnectionListener::class.java)
+ private val logger = LoggerFactory.getLogger(StompDisconnectionListener::class.java)
- override fun onApplicationEvent(event: SessionDisconnectEvent) {
+ private val activeConnections: AtomicInteger = meterRegistry.gauge("clients.connected", AtomicInteger(0))!!
+
+ override fun onApplicationEvent(event: AbstractSubProtocolEvent) {
+ when (event) {
+ is SessionConnectedEvent -> onConnected()
+ is SessionDisconnectEvent -> onDisconnect(event)
+ }
+ }
+
+ private fun onConnected() {
+ activeConnections.incrementAndGet()
+ }
+
+ private fun onDisconnect(event: SessionDisconnectEvent) {
+ activeConnections.decrementAndGet()
val principal = event.user
if (principal == null) {
logger.error("Received session disconnect event without a principal (sessionId = ${event.sessionId})")
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt
index d1d5b211..212f7a0e 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt
@@ -1,6 +1,7 @@
package org.luxons.sevenwonders.server.repositories
import io.micrometer.core.instrument.MeterRegistry
+import io.micrometer.core.instrument.Tag
import org.luxons.sevenwonders.model.api.actions.Icon
import org.luxons.sevenwonders.server.ApiMisuseException
import org.luxons.sevenwonders.server.lobby.Player
@@ -11,7 +12,16 @@ import java.util.concurrent.ConcurrentHashMap
class PlayerRepository(
meterRegistry: MeterRegistry,
) {
- private val players = meterRegistry.gaugeMapSize("players.count", emptyList(), ConcurrentHashMap<String, Player>())!!
+ private val players = ConcurrentHashMap<String, Player>()
+
+ init {
+ meterRegistry.gauge("players.count", listOf(Tag.of("type", "human")), this) {
+ players.count { it.value.isHuman }.toDouble()
+ }
+ meterRegistry.gauge("players.count", listOf(Tag.of("type", "robot")), this) {
+ players.count { !it.value.isHuman }.toDouble()
+ }
+ }
operator fun contains(username: String): Boolean = players.containsKey(username)
bgstack15