Skip to content

Commit

Permalink
Obey lock return values (#9)
Browse files Browse the repository at this point in the history
* Obey lock return values

* Move lock handling to repository

---------

Co-authored-by: Brutus5000 <[email protected]>
  • Loading branch information
Sheikah45 and Brutus5000 authored Nov 26, 2023
1 parent f5217ce commit 2f5c97a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import jakarta.inject.Singleton
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.Table
import org.slf4j.LoggerFactory
import java.time.Instant
import java.util.UUID
import java.util.concurrent.TimeoutException

@Entity
@Table(name = "ice_sessions")
Expand All @@ -21,24 +23,37 @@ data class IceSessionEntity(

) : PanacheEntityBase

private val LOG = LoggerFactory.getLogger(IceSessionRepository::class.java)

@Singleton
class IceSessionRepository : PanacheRepository<IceSessionEntity> {

fun findByGameId(gameId: Long) =
find("gameId = ?1", gameId).firstResult()

fun findByCreatedAtLesserThan(instant: Instant) =
find("createdAt <= ?1", instant).list()

fun acquireGameLock(gameId: Long, timeout: Int = 10) {
getEntityManager().createNativeQuery("SELECT GET_LOCK(:lockName,:timeout)", Boolean::class.java).apply {
val lockAcquired = getEntityManager().createNativeQuery("SELECT GET_LOCK(:lockName,:timeout)", Boolean::class.java).apply {
setParameter("lockName", "game_id_$gameId")
setParameter("timeout", timeout)
}.singleResult
}.singleResult as Boolean?

if (lockAcquired != true) {
throw TimeoutException("Unable to acquire game lock for $gameId")
}
}

fun releaseGameLock(gameId: Long) {
getEntityManager().createNativeQuery("SELECT RELEASE_LOCK(:lockName)", Boolean::class.java).apply {
val lockReleased = getEntityManager().createNativeQuery("SELECT RELEASE_LOCK(:lockName)", Boolean::class.java).apply {
setParameter("lockName", "game_id_$gameId")
}.singleResult
}.singleResult as Boolean?

when (lockReleased) {
null -> LOG.warn("No lock exists for $gameId")
false -> LOG.warn("Not owner of lock for $gameId")
true -> LOG.debug("Lock released for $gameId")
}
}
}
24 changes: 12 additions & 12 deletions src/main/kotlin/com/faforever/icebreaker/service/SessionService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ class SessionService(

@Transactional
fun getSession(gameId: Long): Session {
val session: IceSessionEntity
try {
iceSessionRepository.acquireGameLock(gameId)

val session = iceSessionRepository.findByGameId(gameId)
session = iceSessionRepository.findByGameId(gameId)
?: IceSessionEntity(gameId = gameId, createdAt = Instant.now()).also {
LOG.debug("Creating session for gameId $gameId")
iceSessionRepository.persist(it)
}

val servers = activeSessionHandlers.flatMap {
it.createSession(session.id)
it.getIceServersSession(session.id)
}

return Session(
id = session.id,
servers = servers,
)
} finally {
iceSessionRepository.releaseGameLock(gameId)
}

val servers = activeSessionHandlers.flatMap {
it.createSession(session.id)
it.getIceServersSession(session.id)
}

return Session(
id = session.id,
servers = servers,
)
}

@Transactional
Expand Down

0 comments on commit 2f5c97a

Please sign in to comment.