throttle challenges socket messages

pull/10033/head
Thibault Duplessis 2021-10-26 10:04:42 +02:00
parent bcd0e8da32
commit 4dec6a6712
2 changed files with 16 additions and 10 deletions

View File

@ -148,22 +148,26 @@ final class ChallengeApi(
private def uncacheAndNotify(c: Challenge): Unit = {
c.destUserId ?? countInFor.invalidate
c.destUserId ?? notify
c.challengerUserId ?? notify
c.destUserId ?? notifyUser.apply
c.challengerUserId ?? notifyUser.apply
socketReload(c.id)
}
private def socketReload(id: Challenge.ID): Unit =
socket.foreach(_ reload id)
private def notify(userId: User.ID): Funit =
for {
all <- allFor(userId)
lang <- userRepo langOf userId map I18nLangPicker.byStrOrDefault
} yield Bus.publish(
SendTo(userId, lila.socket.Socket.makeMessage("challenges", jsonView(all)(lang))),
"socketUsers"
)
private object notifyUser {
private val throttler = new lila.hub.EarlyMultiThrottler(logger)
def apply(userId: User.ID): Unit = throttler(userId, 2.seconds) {
for {
all <- allFor(userId)
lang <- userRepo langOf userId map I18nLangPicker.byStrOrDefault
} yield Bus.publish(
SendTo(userId, lila.socket.Socket.makeMessage("challenges", jsonView(all)(lang))),
"socketUsers"
)
}
}
// work around circular dependency
private var socket: Option[ChallengeSocket] = None

View File

@ -5,4 +5,6 @@ import org.joda.time.DateTime
package object challenge extends PackageObject {
private[challenge] def inTwoWeeks = DateTime.now plusWeeks 2
private[challenge] val logger = lila log "challenge"
}