3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-12-25 15:50:19 +01:00

Use System.nanoTime in place of System.currentTimeMillis when comparing timestamps. (#1174)

Dieser Commit ist enthalten in:
0xAda 2024-01-04 17:35:03 +00:00 committet von GitHub
Ursprung 897ff81915
Commit 408b420af7
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
4 geänderte Dateien mit 8 neuen und 5 gelöschten Zeilen

Datei anzeigen

@ -20,6 +20,7 @@ package com.velocitypowered.proxy;
import io.netty.util.ResourceLeakDetector; import io.netty.util.ResourceLeakDetector;
import io.netty.util.ResourceLeakDetector.Level; import io.netty.util.ResourceLeakDetector.Level;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -63,14 +64,14 @@ public class Velocity {
return; return;
} }
long startTime = System.currentTimeMillis(); long startTime = System.nanoTime();
VelocityServer server = new VelocityServer(options); VelocityServer server = new VelocityServer(options);
server.start(); server.start();
Runtime.getRuntime().addShutdownHook(new Thread(() -> server.shutdown(false), Runtime.getRuntime().addShutdownHook(new Thread(() -> server.shutdown(false),
"Shutdown thread")); "Shutdown thread"));
double bootTime = (System.currentTimeMillis() - startTime) / 1000d; double bootTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime) / 1000d;
logger.info("Done ({}s)!", new DecimalFormat("#.##").format(bootTime)); logger.info("Done ({}s)!", new DecimalFormat("#.##").format(bootTime));
server.getConsoleCommandSource().start(); server.getConsoleCommandSource().start();

Datei anzeigen

@ -137,7 +137,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler {
@Override @Override
public boolean handle(KeepAlive packet) { public boolean handle(KeepAlive packet) {
serverConn.getPendingPings().put(packet.getRandomId(), System.currentTimeMillis()); serverConn.getPendingPings().put(packet.getRandomId(), System.nanoTime());
return false; // forwards on return false; // forwards on
} }

Datei anzeigen

@ -36,6 +36,7 @@ import com.velocitypowered.proxy.protocol.util.PluginMessageUtil;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -81,7 +82,7 @@ public class ClientConfigSessionHandler implements MinecraftSessionHandler {
if (sentTime != null) { if (sentTime != null) {
MinecraftConnection smc = serverConnection.getConnection(); MinecraftConnection smc = serverConnection.getConnection();
if (smc != null) { if (smc != null) {
player.setPing(System.currentTimeMillis() - sentTime); player.setPing(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - sentTime));
smc.write(packet); smc.write(packet);
} }
} }

Datei anzeigen

@ -81,6 +81,7 @@ import java.util.Queue;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -177,7 +178,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
if (sentTime != null) { if (sentTime != null) {
MinecraftConnection smc = serverConnection.getConnection(); MinecraftConnection smc = serverConnection.getConnection();
if (smc != null) { if (smc != null) {
player.setPing(System.currentTimeMillis() - sentTime); player.setPing(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - sentTime));
smc.write(packet); smc.write(packet);
} }
} }