summaryrefslogtreecommitdiff
path: root/sw-server/src/main/kotlin
diff options
context:
space:
mode:
authorJoffrey Bion <joffrey.bion@gmail.com>2021-03-14 00:58:19 +0100
committerJoffrey Bion <joffrey.bion@gmail.com>2021-03-14 00:58:27 +0100
commit8f76212fb1a5d4c5ccf278d71ff5c5c8765ace08 (patch)
tree944e939130cb197dd07db87f65c585eba72d8e67 /sw-server/src/main/kotlin
parentUnify game events (diff)
downloadseven-wonders-8f76212fb1a5d4c5ccf278d71ff5c5c8765ace08.tar.gz
seven-wonders-8f76212fb1a5d4c5ccf278d71ff5c5c8765ace08.tar.bz2
seven-wonders-8f76212fb1a5d4c5ccf278d71ff5c5c8765ace08.zip
Better metrics for connected players
Diffstat (limited to 'sw-server/src/main/kotlin')
-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