Skip to content

Commit

Permalink
move to lila.web.SocketTest, add distribution live setting
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Nov 18, 2024
1 parent 32386c7 commit daa26a0
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 37 deletions.
1 change: 0 additions & 1 deletion app/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ given ConfigLoader[NetConfig] = ConfigLoader(config =>
siteName = get[String]("site.name"),
socketDomains = get[List[String]]("socket.domains"),
socketAlts = get[List[String]]("socket.alts"),
socketTest = get[Boolean]("socket.test"),
crawlable = get[Boolean]("crawlable"),
rateLimit = get[RateLimit]("ratelimit"),
email = get[EmailAddress]("email"),
Expand Down
1 change: 1 addition & 0 deletions app/UiEnv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ object UiEnv
def netConfig = env.net
def contactEmailInClear = env.net.email.value
def picfitUrl = env.memo.picfitUrl
def socketTest = env.web.socketTest

given lila.core.config.NetDomain = env.net.domain
given (using ctx: PageContext): Option[Nonce] = ctx.nonce
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/Dev.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class Dev(env: Env) extends LilaController(env):
env.web.settings.noDelaySecret,
env.web.settings.prizeTournamentMakers,
env.web.settings.sitewideCoepCredentiallessHeader,
env.web.socketTest.distributionSetting,
env.tournament.reloadEndpointSetting,
env.tutor.nbAnalysisSetting,
env.tutor.parallelismSetting,
Expand Down Expand Up @@ -84,7 +85,7 @@ final class Dev(env: Env) extends LilaController(env):
.fold(
err => BadRequest(Json.obj("error" -> err.toString)),
results =>
env.api.socketTestResult
env.web.socketTest
.put(Json.obj(me.userId.toString -> results))
.inject(jsonOkResult)
)
Expand Down
7 changes: 3 additions & 4 deletions app/views/base/page.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import scalalib.StringUtils.escapeHtmlRaw
import lila.app.UiEnv.{ *, given }
import lila.common.String.html.safeJsonValue
import lila.ui.RenderedPage
import lila.api.SocketTest

object page:

Expand Down Expand Up @@ -114,9 +113,9 @@ object page:
.option(env.push.vapidPublicKey),
dataUser := ctx.userId,
dataSoundSet := pref.currentSoundSet.toString,
attr("data-socket-domains") := SocketTest.socketEndpoints(netConfig).mkString(","),
attr("data-socket-test-user") := SocketTest.isUserInTestBucket(netConfig),
attr("data-socket-test-running") := netConfig.socketTest,
attr("data-socket-domains") := socketTest.socketEndpoints(netConfig).mkString(","),
attr("data-socket-test-user") := socketTest.isUserInTestBucket(),
attr("data-socket-test-running") := socketTest.isTestRunning(),
dataAssetUrl,
dataAssetVersion := assetVersion,
dataNonce := ctx.nonce.ifTrue(sameAssetDomain).map(_.value),
Expand Down
1 change: 0 additions & 1 deletion conf/base.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ net {
domain = "localhost:9663"
socket.domains = ["localhost:9664"]
socket.alts = []
socket.test = false
asset.domain = ${net.domain}
asset.base_url = "http://"${net.asset.domain}
asset.base_url_internal = ${net.asset.base_url}
Expand Down
7 changes: 1 addition & 6 deletions modules/api/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lila.api

import akka.actor.*
import com.softwaremill.macwire.*
import com.softwaremill.tagging.*
import play.api.Mode

import lila.chat.{ GetLinkCheck, IsChatFresh }
Expand Down Expand Up @@ -59,8 +58,7 @@ final class Env(
webConfig: lila.web.WebConfig,
realPlayerApi: lila.web.RealPlayerApi,
bookmarkExists: lila.core.bookmark.BookmarkExists,
manifest: lila.web.AssetManifest,
yoloDb: lila.db.AsyncDb @@ lila.db.YoloDb // for socket test results
manifest: lila.web.AssetManifest
)(using val mode: Mode, scheduler: Scheduler)(using
Executor,
ActorSystem,
Expand Down Expand Up @@ -97,9 +95,6 @@ final class Env(

lazy val cli = wire[Cli]

lazy val socketTestResult =
SocketTestResult(yoloDb(lila.core.config.CollName("socket_test")).failingSilently())

private lazy val linkCheck = wire[LinkCheck]
lazy val chatFreshness = wire[ChatFreshness]

Expand Down
22 changes: 0 additions & 22 deletions modules/api/src/main/SocketTestResult.scala

This file was deleted.

1 change: 0 additions & 1 deletion modules/core/src/main/config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ object config:
siteName: String,
socketDomains: List[String],
socketAlts: List[String],
socketTest: Boolean,
crawlable: Boolean,
rateLimit: RateLimit,
email: EmailAddress,
Expand Down
1 change: 1 addition & 0 deletions modules/core/src/main/pref.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ trait Pref:
val resizeHandle: Int
val theme: String
val pieceSet: String
val usingAltSocket: Option[Boolean]

def hasKeyboardMove: Boolean
def hasVoice: Boolean
Expand Down
2 changes: 1 addition & 1 deletion modules/db/src/main/JSON.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import reactivemongo.api.bson.*
object JSON:

def bdoc(json: JsObject): BSONDocument =
BSONDocument(json.fields.map { case (k, v) => k -> bval(v) })
BSONDocument(json.fields.map { (k, v) => k -> bval(v) })

def bval(json: JsValue): BSONValue = json match
case JsString(value) => BSONString(value)
Expand Down
7 changes: 7 additions & 0 deletions modules/web/src/main/Env.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package lila.web

import com.softwaremill.macwire.*
import com.softwaremill.tagging.*
import play.api.libs.ws.StandaloneWSClient

@Module
final class Env(
appConfig: play.api.Configuration,
environment: play.api.Environment,
cacheApi: lila.memo.CacheApi,
yoloDb: lila.db.AsyncDb @@ lila.db.YoloDb,
settingStore: lila.memo.SettingStore.Builder,
ws: StandaloneWSClient,
net: lila.core.config.NetConfig
Expand All @@ -33,6 +35,11 @@ final class Env(
if mode.isProd then scheduler.scheduleOnce(5 seconds)(influxEvent.start())
private lazy val pagerDuty = wire[PagerDuty]

val socketTest = SocketTest(
yoloDb(lila.core.config.CollName("socket_test")).failingSilently(),
settingStore
)

lila.common.Bus.subscribeFun("announce"):
case lila.core.socket.Announce(msg, date, _) if msg.contains("will restart") =>
pagerDuty.lilaRestart(date)
Expand Down
35 changes: 35 additions & 0 deletions modules/web/src/main/SocketTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package lila.web

import play.api.libs.json.*

import lila.db.JSON
import lila.core.config.NetConfig
import lila.ui.Context

final class SocketTest(
resultsDb: lila.db.AsyncCollFailingSilently,
settingStore: lila.memo.SettingStore.Builder
)(using Executor):

val distributionSetting = settingStore[Int](
"socketTestDistribution",
default = 0,
text = "Participates to socket test if userId.hashCode % distribution == 0".some
)

def put(results: JsObject) = resultsDb: coll =>
coll.insert.one(JSON.bdoc(results)).void

def isTestRunning() = distributionSetting.get() > 0

def isUserInTestBucket()(using ctx: Context) =
isTestRunning() &&
ctx.pref.usingAltSocket.isEmpty &&
ctx.userId.exists(_.value.hashCode % distributionSetting.get() == 0)

def socketEndpoints(net: NetConfig)(using ctx: Context): List[String] =
ctx.pref.usingAltSocket.match
case Some(true) => net.socketAlts
case Some(false) => net.socketDomains
case _ if isUserInTestBucket() => net.socketDomains.head :: net.socketAlts.headOption.toList
case _ => net.socketDomains

0 comments on commit daa26a0

Please sign in to comment.