feat(challenge): replace scala.util.Random with ThreadLocalRandom for better performance and add native build configuration

This commit is contained in:
2026-04-28 19:14:06 +02:00
parent 4d625b013b
commit 0652dd2d2f
5 changed files with 12 additions and 6 deletions
@@ -0,0 +1,5 @@
quarkus.native.additional-build-args=\
--initialize-at-run-time=org.redisson,\
--initialize-at-run-time=io.netty
%native.quarkus.arc.exclude-types=org.redisson.*
@@ -40,13 +40,11 @@ import io.quarkus.runtime.annotations.RegisterForReflection
classOf[DeclineRequest], classOf[DeclineRequest],
classOf[ChallengeListDto], classOf[ChallengeListDto],
classOf[ErrorDto], classOf[ErrorDto],
classOf[ChallengeStatus],
classOf[ChallengeColor],
classOf[DeclineReason],
classOf[CorePlayerInfo], classOf[CorePlayerInfo],
classOf[CoreTimeControl], classOf[CoreTimeControl],
classOf[CoreCreateGameRequest], classOf[CoreCreateGameRequest],
classOf[CoreGameResponse], classOf[CoreGameResponse],
classOf[OfficialChallengeResponse],
), ),
) )
class NativeReflectionConfig class NativeReflectionConfig
@@ -6,7 +6,8 @@ import jakarta.enterprise.inject.Produces
import jakarta.inject.Inject import jakarta.inject.Inject
import org.redisson.Redisson import org.redisson.Redisson
import org.redisson.api.RedissonClient import org.redisson.api.RedissonClient
import org.redisson.config.Config import org.redisson.config.{Config, TransportMode}
import scala.compiletime.uninitialized import scala.compiletime.uninitialized
@ApplicationScoped @ApplicationScoped
@@ -14,6 +14,7 @@ import org.jboss.logging.Logger
import scala.compiletime.uninitialized import scala.compiletime.uninitialized
import java.util.UUID import java.util.UUID
import java.util.concurrent.ThreadLocalRandom
@Path("/api/challenge/official") @Path("/api/challenge/official")
@ApplicationScoped @ApplicationScoped
@@ -68,7 +69,7 @@ class OfficialChallengeResource:
val userIsWhite = playerColor match val userIsWhite = playerColor match
case "white" => true case "white" => true
case "black" => false case "black" => false
case _ => scala.util.Random.nextBoolean() case _ => ThreadLocalRandom.current().nextBoolean()
val (white, black, botColor) = val (white, black, botColor) =
if userIsWhite then if userIsWhite then
(CorePlayerInfo(user.id.toString, user.username), CorePlayerInfo(bot.id.toString, bot.name), "black") (CorePlayerInfo(user.id.toString, user.username), CorePlayerInfo(bot.id.toString, bot.name), "black")
@@ -29,6 +29,7 @@ import scala.compiletime.uninitialized
import java.time.Instant import java.time.Instant
import java.time.temporal.ChronoUnit import java.time.temporal.ChronoUnit
import java.util.UUID import java.util.UUID
import java.util.concurrent.ThreadLocalRandom
@ApplicationScoped @ApplicationScoped
class ChallengeService: class ChallengeService:
@@ -158,7 +159,7 @@ class ChallengeService:
case ChallengeColor.White => (challenger, destUser) case ChallengeColor.White => (challenger, destUser)
case ChallengeColor.Black => (destUser, challenger) case ChallengeColor.Black => (destUser, challenger)
case ChallengeColor.Random => case ChallengeColor.Random =>
if scala.util.Random.nextBoolean() then (challenger, destUser) else (destUser, challenger) if ThreadLocalRandom.current().nextBoolean() then (challenger, destUser) else (destUser, challenger)
private def buildTimeControl(challenge: Challenge): Option[CoreTimeControl] = private def buildTimeControl(challenge: Challenge): Option[CoreTimeControl] =
challenge.timeControlType match challenge.timeControlType match