Skip to content

Commit

Permalink
don't send partial analysis to crowded broadcasts
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Dec 9, 2024
1 parent ca16b29 commit 6e83a71
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
3 changes: 1 addition & 2 deletions app/controllers/Fishnet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ final class Fishnet(env: Env) extends LilaController(env):
ClientAction[JsonApi.Request.Acquire] { _ => client =>
api
.acquire(client, slow)
.addEffect { jobOpt =>
.addEffect: jobOpt =>
lila.mon.fishnet.http.request(jobOpt.isDefined).increment()
}
.map(Right.apply)
}

Expand Down
4 changes: 3 additions & 1 deletion modules/core/src/main/study.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package study

import reactivemongo.api.bson.Macros.Annotations.Key

import lila.core.id.StudyId
import lila.core.id.{ StudyId, StudyChapterId }
import lila.core.userId.UserId

object data:
Expand Down Expand Up @@ -37,3 +37,5 @@ case class RemoveStudy(studyId: StudyId)

enum Order:
case hot, newest, oldest, updated, popular, alphabetical, mine

case class GetRelayCrowd(id: StudyId, promise: Promise[Int])
5 changes: 5 additions & 0 deletions modules/relay/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import lila.core.config.*
import lila.memo.SettingStore
import lila.memo.SettingStore.Formable.given
import lila.relay.RelayTour.WithLastRound
import lila.core.id.RelayRoundId

@Module
final class Env(
Expand Down Expand Up @@ -158,6 +159,10 @@ final class Env(
lila.common.Bus.sub[lila.study.StudyMembers.OnChange]: change =>
studyPropagation.onStudyMembersChange(change.study)

lila.common.Bus.subscribeFun("getRelayCrowd"):
case lila.core.study.GetRelayCrowd(studyId, promise) =>
roundRepo.currentCrowd(studyId.into(RelayRoundId)).map(_.orZero).foreach(promise.success)

private class RelayColls(mainDb: lila.db.Db, yoloDb: lila.db.AsyncDb @@ lila.db.YoloDb):
val round = mainDb(CollName("relay"))
val tour = mainDb(CollName("relay_tour"))
Expand Down
3 changes: 3 additions & 0 deletions modules/relay/src/main/RelayRoundRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ final private class RelayRoundRepo(val coll: Coll)(using Executor):
)
.void

def currentCrowd(id: RelayRoundId): Fu[Option[Int]] =
coll.primitiveOne[Int]($id(id), "crowd")

def nextRoundThatStartsAfterThisOneCompletes(round: RelayRound): Fu[Option[RelayRound]] = for
next <- coll
.find(
Expand Down
22 changes: 16 additions & 6 deletions modules/study/src/main/ServerEval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import chess.format.{ Fen, Uci, UciCharPair, UciPath }
import play.api.libs.json.*

import lila.core.perm.Granter
import lila.core.study.GetRelayCrowd
import lila.db.dsl.bsonWriteOpt
import lila.tree.Node.Comment
import lila.tree.{ Advice, Analysis, Branch, Info, Node, Root }
Expand Down Expand Up @@ -54,7 +55,7 @@ object ServerEval:
chapterRepo: ChapterRepo,
divider: lila.core.game.Divider,
analysisJson: lila.tree.AnalysisJson
)(using Executor):
)(using Executor, Scheduler):

def apply(analysis: Analysis, complete: Boolean): Funit = analysis.id match
case Analysis.Id.Study(studyId, chapterId) =>
Expand All @@ -67,7 +68,7 @@ object ServerEval:
.foldM(UciPath.root):
case (path, (node, (info, advOpt))) =>
saveAnalysis(chapter, node, path, info, advOpt)
.andDo(sendProgress(studyId, chapterId, analysis))
.andDo(sendProgress(studyId, chapterId, analysis, complete))
.logFailure(logger)
yield ()
case _ => funit
Expand Down Expand Up @@ -155,12 +156,13 @@ object ServerEval:
private def sendProgress(
studyId: StudyId,
chapterId: StudyChapterId,
analysis: Analysis
) =
analysis: Analysis,
complete: Boolean
): Funit =
chapterRepo
.byId(chapterId)
.foreach:
_.so: chapter =>
.flatMapz: chapter =>
reallySendToChapter(studyId, chapter, complete).mapz:
socket.onServerEval(
studyId,
ServerEval.Progress(
Expand All @@ -171,6 +173,14 @@ object ServerEval:
)
)

private def reallySendToChapter(studyId: StudyId, chapter: Chapter, complete: Boolean): Fu[Boolean] =
if complete || chapter.relay.isEmpty
then fuTrue
else
lila.common.Bus
.ask[Int]("getRelayCrowd") { GetRelayCrowd(studyId, _) }
.map(_ < 5000)

def divisionOf(chapter: Chapter) =
divider(
id = chapter.id.into(GameId),
Expand Down

0 comments on commit 6e83a71

Please sign in to comment.