Skip to content

Commit f2a7178

Browse files
feat: more tolerant latency check
1 parent 41d66f1 commit f2a7178

1 file changed

Lines changed: 8 additions & 16 deletions

File tree

packages/client/src/utils/timeLib.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ async function syncClockOffset() {
6464
for (let attempts = 0; attempts < 5; attempts++) {
6565
const { netClockOffset, systemOffset, oneWayLatencyEstimate } = await computeClockOffset();
6666

67-
// If the estimated one way latency estimate was less than 100ms, we'll accept the measurement.
68-
if (oneWayLatencyEstimate < 100) {
67+
// Gradually increase the threshold for how long the round trip
68+
// can take, as we become more confident in the measurement.
69+
if (oneWayLatencyEstimate < (attempts + 1) * 500) {
6970
CHAIN_TIMESTAMP_OFFSET_MS.value = Math.floor(netClockOffset);
7071
ONE_WAY_LATENCY_ESTIMATE_MS.value = Math.floor(oneWayLatencyEstimate);
7172

@@ -78,23 +79,14 @@ async function syncClockOffset() {
7879
}
7980

8081
// If it was longer, we'll try again, hopefully was just a transient issue.
81-
console.log("Round trip took dangerously long, retrying...");
82-
await new Promise((resolve) => setTimeout(resolve, 1000));
82+
console.log(
83+
`Round trip took strangely long (${oneWayLatencyEstimate.toFixed(2)}ms), retrying...`
84+
);
85+
await new Promise((resolve) => setTimeout(resolve, 250));
8386
}
84-
85-
// If we've reached this point, the round trip to the timeserver consistently took
86-
// more than 150ms. This level of latency makes it difficult to trust the accuracy of
87-
// the clock offset sync. A bad sync can make the game unplayable. We'll alert the user.
88-
console.warn("Network has really high latency, alerting user...");
89-
alert(`🕖 Your network has significant latency (>150ms).
90-
91-
👾 This will heavily degrade game performance and/or cause glitches.
92-
93-
🛜 Try switching networks, disabling any VPNs, and/or restarting your computer.
94-
95-
🔄 Refresh the page to try again.`);
9687
} catch (error) {
9788
console.error("Clock offset sync failed:", error);
89+
alert("🕖 Clock offset sync failed, please refresh the page. Contact us if this persists.");
9890
}
9991
}
10092
}

0 commit comments

Comments
 (0)