From 4215678f4c8c6fd434e874b6d4fb4c099424f59a Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 20 Dec 2020 21:20:59 +0900 Subject: [PATCH] clock: Bug fix: Syncing with the system clock happens more often than expected When the specified TPS is too big (e.g., 300), the time threshold to determine whether the clock should be synchronized with the system clock can be too small, and the decision can be wrong too often. To fix this, prepare another time assuming the TPS was 60 and use the bigger one. Fixes #1443 --- internal/clock/clock.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/clock/clock.go b/internal/clock/clock.go index 40a8c98b0..612f78fab 100644 --- a/internal/clock/clock.go +++ b/internal/clock/clock.go @@ -57,6 +57,13 @@ func CurrentTPS() float64 { return v } +func max(a, b int64) int64 { + if a < b { + return b + } + return a +} + func calcCountFromTPS(tps int64, now int64) int { if tps == 0 { return 0 @@ -73,7 +80,10 @@ func calcCountFromTPS(tps int64, now int64) int { count := 0 syncWithSystemClock := false - if diff > int64(time.Second)*5/int64(tps) { + // When TPS is big (e.g. 300), the time gap can be small and diff might always exceeds the gap. + // To avoid this, prepare another gap assuming TPS was 60 and use the bigger one (#1443). + tooBigGap := max(int64(time.Second)*5/int64(tps), int64(time.Second)*5/60) + if diff > tooBigGap { // The previous time is too old. // Let's force to sync the game time with the system clock. syncWithSystemClock = true