Skip to content

Commit

Permalink
Remove some dev/prod separation
Browse files Browse the repository at this point in the history
  • Loading branch information
WandererXII committed Jan 27, 2025
1 parent 042aa25 commit 0b9719c
Show file tree
Hide file tree
Showing 27 changed files with 33 additions and 82 deletions.
10 changes: 0 additions & 10 deletions app/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import akka.actor._
import com.softwaremill.macwire._

import lila.common.Bus
import lila.common.Lilakka
import lila.common.Strings
import lila.common.config._
import lila.game.IdGenerator
Expand Down Expand Up @@ -262,13 +261,4 @@ final class EnvBoot(

templating.Environment setEnv env

// free memory for reload workflow
if (env.isDev)
Lilakka.shutdown(shutdown, _.PhaseServiceStop, "Freeing dev memory") { () =>
Future {
templating.Environment.destroy()
lila.common.Bus.destroy()
lila.mon.destroy()
}
}
}
1 change: 0 additions & 1 deletion app/templating/Environment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ object Environment

private var envVar: Option[Env] = None
def setEnv(e: Env) = { envVar = Some(e) }
def destroy() = { envVar = None }
def env: Env = envVar.get

type FormWithCaptcha = (play.api.data.Form[_], lila.common.Captcha)
Expand Down
2 changes: 1 addition & 1 deletion app/views/lobby/home.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object home {
views.html.base.layout(
title = "",
fullTitle = Some {
s"lishogi.${if (isProd && !isStage) "org" else "dev"} - ${trans.freeOnlineShogi.txt()}"
s"lishogi.${if (isProd) "org" else "dev"} - ${trans.freeOnlineShogi.txt()}"
},
moreJs = frag(
moduleJsTag(
Expand Down
1 change: 0 additions & 1 deletion modules/challenge/src/main/ChallengeSocket.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ final private class ChallengeSocket(
remoteSocketApi: lila.socket.RemoteSocket,
)(implicit
ec: scala.concurrent.ExecutionContext,
mode: play.api.Mode,
) {

import ChallengeSocket._
Expand Down
1 change: 0 additions & 1 deletion modules/challenge/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ final class Env(
)(implicit
ec: scala.concurrent.ExecutionContext,
system: akka.actor.ActorSystem,
mode: play.api.Mode,
) {

private lazy val maxPlaying = appConfig.get[Max]("setup.max_playing")
Expand Down
48 changes: 20 additions & 28 deletions modules/common/src/main/Bus.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ object Bus {
publish = (tellable, event) => tellable ! event,
)

def keys = bus.keys
def size = bus.size
def destroy() = bus.destroy()
def keys = bus.keys
def size = bus.size

case class AskTimeout(message: String) extends lila.base.LilaException
}
Expand All @@ -85,31 +84,28 @@ final private class EventBus[Event, Channel, Subscriber](
import java.util.concurrent.ConcurrentHashMap

private val entries = new ConcurrentHashMap[Channel, Set[Subscriber]](initialCapacity)
private var alive = true

def subscribe(subscriber: Subscriber, channel: Channel): Unit =
if (alive)
entries
.compute(
channel,
(_: Channel, subs: Set[Subscriber]) => {
Option(subs).fold(Set(subscriber))(_ + subscriber)
},
)
.unit
entries
.compute(
channel,
(_: Channel, subs: Set[Subscriber]) => {
Option(subs).fold(Set(subscriber))(_ + subscriber)
},
)
.unit

def unsubscribe(subscriber: Subscriber, channel: Channel): Unit =
if (alive)
entries
.computeIfPresent(
channel,
(_: Channel, subs: Set[Subscriber]) => {
val newSubs = subs - subscriber
if (newSubs.isEmpty) null
else newSubs
},
)
.unit
entries
.computeIfPresent(
channel,
(_: Channel, subs: Set[Subscriber]) => {
val newSubs = subs - subscriber
if (newSubs.isEmpty) null
else newSubs
},
)
.unit

def publish(event: Event, channel: Channel): Unit =
Option(entries get channel) foreach {
Expand All @@ -121,8 +117,4 @@ final private class EventBus[Event, Channel, Subscriber](
def keys: Set[Channel] = entries.keySet.asScala.toSet
def size = entries.size
def sizeOf(channel: Channel) = Option(entries get channel).fold(0)(_.size)
def destroy() = {
alive = false
entries.clear()
}
}
12 changes: 4 additions & 8 deletions modules/common/src/main/LilaCache.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package lila.common

import play.api.Mode

import com.github.benmanes.caffeine.cache.Caffeine
import com.github.benmanes.caffeine.cache.Scheduler
import com.github.blemale.scaffeine.Scaffeine

object LilaCache {

def caffeine(mode: Mode): Caffeine[Any, Any] =
if (mode == Mode.Prod) Caffeine.newBuilder().scheduler(Scheduler.systemScheduler)
else Caffeine.newBuilder() // systemScheduler causes play reload classloader leaks :-/
def caffeine: Caffeine[Any, Any] =
Caffeine.newBuilder().scheduler(Scheduler.systemScheduler)

def scaffeine(mode: Mode): Scaffeine[Any, Any] =
if (mode == Mode.Prod) Scaffeine().scheduler(Scheduler.systemScheduler)
else Scaffeine() // systemScheduler causes play reload classloader leaks :-/
def scaffeine: Scaffeine[Any, Any] =
Scaffeine().scheduler(Scheduler.systemScheduler)
}
4 changes: 1 addition & 3 deletions modules/hub/src/main/AskPipeline.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,12 @@ final class AskPipelines[K, R](
)(implicit
ec: ExecutionContext,
system: akka.actor.ActorSystem,
mode: play.api.Mode,
) {

def apply(key: K): Fu[R] = pipelines.get(key).get

private val pipelines: LoadingCache[K, AskPipeline[R]] =
lila.common.LilaCache
.scaffeine(mode)
lila.common.LilaCache.scaffeine
.expireAfterAccess(expiration)
.build(key => new AskPipeline[R](() => compute(key), timeout, name = s"$name:$key"))
}
4 changes: 1 addition & 3 deletions modules/hub/src/main/DuctSequencer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@ final class DuctSequencers(
)(implicit
system: akka.actor.ActorSystem,
ec: ExecutionContext,
mode: play.api.Mode,
) {

def apply[A](key: String)(task: => Fu[A]): Fu[A] =
sequencers.get(key).run(() => task)

private val sequencers: LoadingCache[String, DuctSequencer] =
lila.common.LilaCache
.scaffeine(mode)
lila.common.LilaCache.scaffeine
.expireAfterAccess(expiration)
.build(key => new DuctSequencer(maxSize, timeout, s"$name:$key", logging))
}
Expand Down
8 changes: 2 additions & 6 deletions modules/hub/src/main/TrouperMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import scala.concurrent.Promise
import scala.concurrent.duration.FiniteDuration
import scala.jdk.CollectionConverters._

import play.api.Mode

import alleycats.Zero
import com.github.benmanes.caffeine.cache._

final class TrouperMap[T <: Trouper](
mkTrouper: String => T,
accessTimeout: FiniteDuration,
)(implicit mode: Mode) {
) {

def getOrMake(id: String): T = troupers get id

Expand Down Expand Up @@ -50,9 +48,7 @@ final class TrouperMap[T <: Trouper](
def touchOrMake(id: String): Unit = troupers.get(id).unit

private[this] val troupers: LoadingCache[String, T] =
lila.common.LilaCache
.caffeine(mode)
.recordStats
lila.common.LilaCache.caffeine.recordStats
.expireAfterAccess(accessTimeout.toMillis, TimeUnit.MILLISECONDS)
.removalListener(new RemovalListener[String, T] {
def onRemoval(id: String, trouper: T, cause: RemovalCause): Unit =
Expand Down
4 changes: 2 additions & 2 deletions modules/memo/src/main/CacheApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class CacheApi(

import CacheApi._

def scaffeine: Builder = CacheApi.scaffeine(mode)
def scaffeine: Builder = CacheApi.scaffeine

// AsyncLoadingCache with monitoring
def apply[K, V](initialCapacity: Int, name: String)(
Expand Down Expand Up @@ -78,7 +78,7 @@ object CacheApi {

private[memo] type Builder = Scaffeine[Any, Any]

def scaffeine(mode: Mode): Builder = lila.common.LilaCache scaffeine mode
def scaffeine: Builder = lila.common.LilaCache.scaffeine

def scaffeineNoScheduler: Builder = Scaffeine()

Expand Down
5 changes: 2 additions & 3 deletions modules/memo/src/main/MongoCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ object MongoCache {
db: lila.db.Db,
config: MemoConfig,
cacheApi: CacheApi,
mode: play.api.Mode,
)(implicit ec: scala.concurrent.ExecutionContext) {

private val coll = db(config.cacheColl)
Expand All @@ -81,7 +80,7 @@ object MongoCache {
keyToString,
(wrapper: LoaderWrapper[K, V]) =>
build(wrapper)(
scaffeine(mode).recordStats().initialCapacity(cacheApi.actualCapacity(initialCapacity)),
scaffeine.recordStats().initialCapacity(cacheApi.actualCapacity(initialCapacity)),
),
coll,
)
Expand All @@ -100,7 +99,7 @@ object MongoCache {
name,
dbTtl,
_ => "",
wrapper => build(wrapper)(scaffeine(mode).initialCapacity(1)),
wrapper => build(wrapper)(scaffeine.initialCapacity(1)),
coll,
)

Expand Down
1 change: 0 additions & 1 deletion modules/puzzle/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ final class Env(
)(implicit
ec: scala.concurrent.ExecutionContext,
system: akka.actor.ActorSystem,
mode: play.api.Mode,
) {

private val config = appConfig.get[PuzzleConfig]("puzzle")(AutoConfig.loader)
Expand Down
1 change: 0 additions & 1 deletion modules/puzzle/src/main/PuzzleApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ final class PuzzleApi(
)(implicit
ec: scala.concurrent.ExecutionContext,
system: akka.actor.ActorSystem,
mode: play.api.Mode,
) {

import BsonHandlers._
Expand Down
1 change: 0 additions & 1 deletion modules/puzzle/src/main/PuzzleFinisher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ final private[puzzle] class PuzzleFinisher(
)(implicit
ec: scala.concurrent.ExecutionContext,
system: akka.actor.ActorSystem,
mode: play.api.Mode,
) {

import BsonHandlers._
Expand Down
1 change: 0 additions & 1 deletion modules/room/src/main/RoomSocket.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ object RoomSocket {

def makeRoomMap(send: Send)(implicit
ec: ExecutionContext,
mode: play.api.Mode,
) =
new TrouperMap(
mkTrouper = roomId =>
Expand Down
1 change: 0 additions & 1 deletion modules/simul/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ final class Env(
)(implicit
ec: scala.concurrent.ExecutionContext,
system: ActorSystem,
mode: play.api.Mode,
) {

private val config = appConfig.get[SimulConfig]("simul")(AutoConfig.loader)
Expand Down
1 change: 0 additions & 1 deletion modules/simul/src/main/SimulApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ final class SimulApi(
)(implicit
ec: scala.concurrent.ExecutionContext,
system: akka.actor.ActorSystem,
mode: play.api.Mode,
) {

private val workQueue =
Expand Down
1 change: 0 additions & 1 deletion modules/simul/src/main/SimulSocket.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ final private class SimulSocket(
chat: lila.chat.ChatApi,
)(implicit
ec: scala.concurrent.ExecutionContext,
mode: play.api.Mode,
) {

def hostIsOn(simulId: Simul.ID, gameId: Game.ID): Unit =
Expand Down
1 change: 0 additions & 1 deletion modules/study/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ final class Env(
ec: scala.concurrent.ExecutionContext,
system: akka.actor.ActorSystem,
mat: akka.stream.Materializer,
mode: play.api.Mode,
) {

private lazy val studyDb = mongo.asyncDb("study", appConfig.get[String]("study.mongodb.uri"))
Expand Down
1 change: 0 additions & 1 deletion modules/study/src/main/StudySequencer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ final private class StudySequencer(
)(implicit
ec: scala.concurrent.ExecutionContext,
system: akka.actor.ActorSystem,
mode: play.api.Mode,
) {

private val workQueue =
Expand Down
1 change: 0 additions & 1 deletion modules/study/src/main/StudySocket.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ final private class StudySocket(
chatApi: lila.chat.ChatApi,
)(implicit
ec: scala.concurrent.ExecutionContext,
mode: play.api.Mode,
) {

import StudySocket._
Expand Down
1 change: 0 additions & 1 deletion modules/team/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ final class Env(
)(implicit
ec: scala.concurrent.ExecutionContext,
system: ActorSystem,
mode: play.api.Mode,
) {

lazy val teamRepo = new TeamRepo(db(CollName("team")))
Expand Down
1 change: 0 additions & 1 deletion modules/team/src/main/TeamSocket.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ final private class TeamSocket(
cached: Cached,
)(implicit
ec: scala.concurrent.ExecutionContext,
mode: play.api.Mode,
) {

lazy val rooms = makeRoomMap(send)
Expand Down
1 change: 0 additions & 1 deletion modules/tournament/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ final class Env(
system: ActorSystem,
mat: akka.stream.Materializer,
idGenerator: lila.game.IdGenerator,
mode: play.api.Mode,
) {

private val config = appConfig.get[TournamentConfig]("tournament")(AutoConfig.loader)
Expand Down
1 change: 0 additions & 1 deletion modules/tournament/src/main/TournamentApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ final class TournamentApi(
)(implicit
ec: scala.concurrent.ExecutionContext,
system: ActorSystem,
mode: play.api.Mode,
) {

private val workQueue =
Expand Down
1 change: 0 additions & 1 deletion modules/tournament/src/main/TournamentSocket.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ final private class TournamentSocket(
)(implicit
ec: scala.concurrent.ExecutionContext,
system: ActorSystem,
mode: play.api.Mode,
) {

private val allWaitingUsers = new ConcurrentHashMap[Tournament.ID, WaitingUsers.WithNext](64)
Expand Down

0 comments on commit 0b9719c

Please sign in to comment.