Skip to content

Commit

Permalink
Implement updated weight average calc
Browse files Browse the repository at this point in the history
  • Loading branch information
WarpWing authored Apr 20, 2024
1 parent c2779b9 commit 67331a8
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/main/scala/Lag.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import lila.ws.util.Domain
final class Lag(lilaRedis: Lila, groupedWithin: util.GroupedWithin):

private type TrustedMillis = Int
private val trustedRefreshFactor = 0.1f
private val trustedSpikeRefreshFactor = 0.05f
private val trustedNormalRefreshFactor = 0.1f
private val maxMillis = 5000

private val trustedStats: Cache[User.Id, TrustedMillis] = Scaffeine()
.expireAfterWrite(1 hour)
Expand All @@ -28,5 +30,7 @@ final class Lag(lilaRedis: Lila, groupedWithin: util.GroupedWithin):
uid,
sessionLag(uid)
.fold(millis): prev =>
(prev * (1 - trustedRefreshFactor) + millis * trustedRefreshFactor).toInt
val weight = if (millis < prev) trustedNormalRefreshFactor else trustedSpikeRefreshFactor
val cappedMillis = millis.min(maxMillis)
(prev * (1 - weight) + cappedMillis * weight).toInt
)

0 comments on commit 67331a8

Please sign in to comment.