From a68eda7bb7388901f4ac8e377b14bc291cb5666e Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Fri, 15 May 2020 03:49:12 +0200 Subject: Make Lobby/player repository more thread safe --- .../server/repositories/LobbyRepository.kt | 9 ++++---- .../server/repositories/PlayerRepository.kt | 24 +++++----------------- 2 files changed, 10 insertions(+), 23 deletions(-) (limited to 'sw-server') diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt index ff06f549..5036266d 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt @@ -5,19 +5,20 @@ import org.luxons.sevenwonders.server.lobby.Lobby import org.luxons.sevenwonders.server.lobby.Player import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Repository -import java.util.HashMap +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicLong @Repository class LobbyRepository @Autowired constructor() { - private val lobbies = HashMap() + private val lobbies = ConcurrentHashMap() - private var lastGameId: Long = 0 + private var lastGameId: AtomicLong = AtomicLong(0) fun list(): Collection = lobbies.values fun create(gameName: String, owner: Player): Lobby { - val id = lastGameId++ + val id = lastGameId.getAndIncrement() val lobby = Lobby(id, gameName, owner, GameDefinition.load()) lobbies[id] = lobby return lobby 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 00badf75..b31d2cee 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 @@ -3,33 +3,19 @@ package org.luxons.sevenwonders.server.repositories import org.luxons.sevenwonders.server.ApiMisuseException import org.luxons.sevenwonders.server.lobby.Player import org.springframework.stereotype.Repository -import java.util.HashMap +import java.util.concurrent.ConcurrentHashMap @Repository class PlayerRepository { - private val players = HashMap() + private val players = ConcurrentHashMap() operator fun contains(username: String): Boolean = players.containsKey(username) fun createOrUpdate(username: String, displayName: String): Player { - return if (players.containsKey(username)) { - update(username, displayName) - } else { - create(username, displayName) - } - } - - private fun create(username: String, displayName: String): Player { - val player = Player(username, displayName) - players[username] = player - return player - } - - private fun update(username: String, displayName: String): Player { - val player = find(username) - player.displayName = displayName - return player + val p = players.computeIfAbsent(username) { Player(username, displayName) } + p.displayName = displayName + return p } fun find(username: String): Player = players[username] ?: throw PlayerNotFoundException(username) -- cgit