From 0e8fe365331bc06aace92619fe61ef0c06be6796 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Fri, 22 Oct 2021 09:59:22 +0200 Subject: [PATCH] add lila-ws round-trip latency to lag monitor --- modules/round/src/main/MoveLatMonitor.scala | 15 +++++++++------ modules/round/src/main/RoundSocket.scala | 7 +++++-- translation/source/lag.xml | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/modules/round/src/main/MoveLatMonitor.scala b/modules/round/src/main/MoveLatMonitor.scala index b171a1970b..8f0c9bc3b7 100644 --- a/modules/round/src/main/MoveLatMonitor.scala +++ b/modules/round/src/main/MoveLatMonitor.scala @@ -12,13 +12,16 @@ private object MoveLatMonitor { } private val latency = new AtomicReference(Latency()) + def record(micros: Int): Unit = latency.getAndUpdate(_ record micros).unit + + object wsLatency { + var latestMillis = 0 + def set(millis: Int) = latestMillis = millis + } + def start(scheduler: Scheduler)(implicit ec: scala.concurrent.ExecutionContext) = scheduler.scheduleWithFixedDelay(10 second, 2 second) { () => - lila.common.Bus.publish( - lila.hub.actorApi.round.Mlat(latency.getAndSet(Latency()).average), - "mlat" - ) + val full = latency.getAndSet(Latency()).average + wsLatency.latestMillis * 1000 + lila.common.Bus.publish(lila.hub.actorApi.round.Mlat(full), "mlat") } - - def record(micros: Int): Unit = latency.getAndUpdate(_ record micros).unit } diff --git a/modules/round/src/main/RoundSocket.scala b/modules/round/src/main/RoundSocket.scala index 8867de37ed..85aa76ac7b 100644 --- a/modules/round/src/main/RoundSocket.scala +++ b/modules/round/src/main/RoundSocket.scala @@ -150,7 +150,8 @@ final class RoundSocket( case RP.In.SetVersions(versions) => preloadRoundsWithVersions(versions) send(Protocol.Out.versioningReady) - case P.In.Ping(id) => send(P.Out.pong(id)) + case P.In.Ping(id) => send(P.Out.pong(id)) + case Protocol.In.WsLatency(millis) => MoveLatMonitor.wsLatency.set(millis) case P.In.WsBoot => logger.warn("Remote socket boot") // schedule termination for all game asyncActors @@ -315,6 +316,7 @@ object RoundSocket { case class Flag(gameId: Game.Id, color: Color, fromPlayerId: Option[PlayerId]) extends P.In case class Berserk(gameId: Game.Id, userId: User.ID) extends P.In case class SelfReport(fullId: FullId, ip: IpAddress, userId: Option[User.ID], name: String) extends P.In + case class WsLatency(millis: Int) extends P.In val reader: P.In.Reader = raw => raw.path match { @@ -376,7 +378,8 @@ object RoundSocket { Flag(Game.Id(gameId), _, P.In.optional(playerId) map PlayerId.apply) } } - case _ => RP.In.reader(raw) + case "r/latency" => raw.args.toIntOption map WsLatency + case _ => RP.In.reader(raw) } private def centis(s: String): Option[Centis] = diff --git a/translation/source/lag.xml b/translation/source/lag.xml index 3d7e71236c..5644f18637 100644 --- a/translation/source/lag.xml +++ b/translation/source/lag.xml @@ -7,7 +7,7 @@ Yes. It will be fixed soon! And now, the long answer! Game lag is composed of two unrelated values (lower is better): Lichess server latency - The time it takes to process a move on the server. It's the same for everybody, and only depends on the server load. The more players and the higher it gets, but Lichess developers do their best to keep it low. It rarely exceeds 10ms. + The time it takes to process a move on the server. It's the same for everybody, and only depends on the servers load. The more players and the higher it gets, but Lichess developers do their best to keep it low. It rarely exceeds 10ms. Network between Lichess and you The time it takes to send a move from your computer to Lichess server, and get the response back. It's specific to your distance to Lichess (France), and to the quality of your Internet connection. Lichess developers cannot fix your wifi or make light go faster. You can find both these values at any time, by clicking your username in the top bar.