Add QuitHooks and ServerMapQuitSleepPerformancePatch for Build 42.21. Optimize server shutdown by replacing fixed sleep with client connection polling. Include hooks, unit tests, and integrate the patch into the performance patch set.

This commit is contained in:
Overlord committed 2026-10-05 19:45:28 +02:00
1 parent 99f4f49a7c
commit e61026a3d4
5 files changed
+187 -11

No files matched your search

@@ -0,0 +1,79 @@
package dev.overlord.zomboidforge.compat.b42.hooks;
import dev.overlord.zomboidforge.compat.b42.ForgeInstance;
import dev.overlord.zomboidforge.core.config.Feature;
import org.jetbrains.annotations.Nullable;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.Map;
import zombie.core.raknet.UdpEngine;
import zombie.network.GameServer;
/**
* Hooks for the shutdown sleep in {@code ServerMap.QueuedQuit} ({@link Feature#EXPERIMENTAL_PERFORMANCE}).
* After the world save, vanilla broadcasts {@code ServerQuit} and sleeps 5 s, so the clients
* receive it and disconnect, then closes the databases and the network.
*
* <p>The connections are those of {@code UdpEngine.connectionMap}: the network thread
* ({@code UdpEngine}) removes a connection from it as soon as RakNet reports the disconnect
* notification or the lost connection. {@code GameServer.udpEngine.connections} is updated only
* by the main thread, which is in {@code QueuedQuit} at this point.
*/
@SuppressWarnings("unused")
public final class QuitHooks
{
private static final long POLL_MILLIS = 10L;
/** {@code UdpEngine.connectionMap}, private; {@code null} when it cannot be reached. */
private static final @Nullable VarHandle CONNECTIONS;
static
{
VarHandle connections = null;
try
{
connections = MethodHandles.privateLookupIn(UdpEngine.class, MethodHandles.lookup())
.findVarHandle(UdpEngine.class, "connectionMap", Map.class);
}
catch (ReflectiveOperationException e) { System.out.println("[ZomboidForge] Unable to register hook 'QuitHooks.quitSleep': 'UdpEngine.connectionMap' not found: " + e); }
CONNECTIONS = connections;
}
private QuitHooks() {}
/**
* Replaces {@code Thread.sleep(5000L)} in {@code QueuedQuit}. Sleeps {@code millis} when the
* feature is off, {@code connectionMap} is unreachable, or there is no {@code udpEngine};
* otherwise returns once no client is connected, polling every 10 ms, after at most
* {@code millis}.
*/
@SuppressWarnings("BusyWait")
public static void quitSleep(long millis) throws InterruptedException
{
UdpEngine engine = GameServer.udpEngine;
if (!ForgeInstance.FORGE.settings().enabled(Feature.EXPERIMENTAL_PERFORMANCE) || QuitHooks.CONNECTIONS == null || engine == null)
{
Thread.sleep(millis);
return;
}
long deadline = System.nanoTime() + millis * 1_000_000L;
while (true)
{
// The network thread writes the map without synchronisation.
VarHandle.acquireFence();
int connected = ((Map<?, ?>) QuitHooks.CONNECTIONS.get(engine)).size();
long now = System.nanoTime();
if (connected == 0 || now >= deadline) return;
Thread.sleep(Math.min(QuitHooks.POLL_MILLIS, (deadline - now) / 1_000_000L + 1L));
}
}
}
@@ -0,0 +1,69 @@
package dev.overlord.zomboidforge.compat.b42.hooks;
import dev.overlord.zomboidforge.compat.b42.ForgeInstance;
import dev.overlord.zomboidforge.core.Forge;
import dev.overlord.zomboidforge.core.config.Feature;
import dev.overlord.zomboidforge.core.config.ForgeSettings;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import zombie.core.raknet.UdpEngine;
import zombie.network.GameServer;
import java.lang.reflect.Field;
import java.time.Duration;
import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** The paths without a network engine; the wait for disconnects is checked on a running server. */
class QuitHooksTest
{
private static final long MILLIS = 50L;
private static void settings(ForgeSettings settings) throws ReflectiveOperationException
{
Field field = Forge.class.getDeclaredField("settings");
field.setAccessible(true);
field.set(ForgeInstance.FORGE, settings);
}
private static long sleptMillis() throws InterruptedException
{
long start = System.nanoTime();
QuitHooks.quitSleep(QuitHooksTest.MILLIS);
return (System.nanoTime() - start) / 1_000_000L;
}
@AfterEach
void resetSettings() throws ReflectiveOperationException
{
QuitHooksTest.settings(ForgeSettings.DEFAULTS);
}
@Test
void sleepsTheFullTimeWithoutTheFeature() throws InterruptedException
{
assertTrue(QuitHooksTest.sleptMillis() >= QuitHooksTest.MILLIS);
}
@Test
void sleepsTheFullTimeWithoutANetworkEngine() throws Exception
{
assertNull(GameServer.udpEngine);
QuitHooksTest.settings(new ForgeSettings(Duration.ofMillis(2), Duration.ofSeconds(10), Set.of(Feature.EXPERIMENTAL_PERFORMANCE)));
assertTrue(QuitHooksTest.sleptMillis() >= QuitHooksTest.MILLIS);
}
/** The hook reads this field; without it the shutdown keeps the vanilla sleep. */
@Test
void connectionMapExistsInTheGame() throws NoSuchFieldException
{
assertEquals(Map.class, UdpEngine.class.getDeclaredField("connectionMap").getType());
}
}