From 4da0b71df5f810440d63c5b51cc322287c4654f9 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Tue, 29 Dec 2020 22:16:12 +0100 Subject: [PATCH] sequence puzzle voting --- modules/puzzle/src/main/PuzzleApi.scala | 38 ++++++++++++++++--------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/modules/puzzle/src/main/PuzzleApi.scala b/modules/puzzle/src/main/PuzzleApi.scala index 0dfc870ef1..e1f66c1754 100644 --- a/modules/puzzle/src/main/PuzzleApi.scala +++ b/modules/puzzle/src/main/PuzzleApi.scala @@ -13,7 +13,7 @@ final class PuzzleApi( colls: PuzzleColls, trustApi: PuzzleTrustApi, countApi: PuzzleCountApi -)(implicit ec: scala.concurrent.ExecutionContext) { +)(implicit ec: scala.concurrent.ExecutionContext, system: akka.actor.ActorSystem, mode: play.api.Mode) { import Puzzle.{ BSONFields => F } import BsonHandlers._ @@ -44,23 +44,33 @@ final class PuzzleApi( object vote { + private val sequencer = + new lila.hub.DuctSequencers( + maxSize = 16, + expiration = 5 minutes, + timeout = 3 seconds, + name = "puzzle.vote" + ) + def update(id: Puzzle.Id, user: User, vote: Boolean): Funit = - round - .find(user, id) - .flatMap { - _ ?? { prevRound => - trustApi.vote(user, prevRound, vote) flatMap { - _ ?? { weight => - val voteValue = (if (vote) 1 else -1) * weight - lila.mon.puzzle.vote(vote, prevRound.win).increment() - updatePuzzle(id, voteValue, prevRound.vote) zip - colls.round { - _.updateField($id(prevRound.id), PuzzleRound.BSONFields.vote, voteValue) - } void + sequencer(id.value) { + round + .find(user, id) + .flatMap { + _ ?? { prevRound => + trustApi.vote(user, prevRound, vote) flatMap { + _ ?? { weight => + val voteValue = (if (vote) 1 else -1) * weight + lila.mon.puzzle.vote(vote, prevRound.win).increment() + updatePuzzle(id, voteValue, prevRound.vote) zip + colls.round { + _.updateField($id(prevRound.id), PuzzleRound.BSONFields.vote, voteValue) + } void + } } } } - } + } private def updatePuzzle(puzzleId: Puzzle.Id, newVote: Int, prevVote: Option[Int]): Funit = colls.puzzle { coll =>