Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close threads at server shutdown #4029

Merged
merged 6 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public GeyserServerInitializer(GeyserImpl geyser) {
this.geyser = geyser;
}

public DefaultEventLoopGroup getEventLoopGroup() {
return eventLoopGroup;
}

@Override
public void initSession(@Nonnull BedrockServerSession bedrockServerSession) {
try {
Expand All @@ -72,4 +76,4 @@ public void initSession(@Nonnull BedrockServerSession bedrockServerSession) {
protected BedrockPeer createPeer(Channel channel) {
return new GeyserBedrockPeer(channel, this::createSession);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.util.concurrent.Future;
import lombok.Getter;
import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;
Expand All @@ -58,6 +59,7 @@
import org.geysermc.geyser.network.netty.proxy.ProxyServerHandler;
import org.geysermc.geyser.ping.GeyserPingInfo;
import org.geysermc.geyser.ping.IGeyserPingPassthrough;
import org.geysermc.geyser.skin.SkinProvider;
import org.geysermc.geyser.text.GeyserLocale;
import org.geysermc.geyser.translator.text.MessageTranslator;

Expand All @@ -83,14 +85,21 @@ public final class GeyserServer {

private static final Transport TRANSPORT = compatibleTransport();

/**
* See {@link EventLoopGroup#shutdownGracefully(long, long, TimeUnit)}
*/
private static final int SHUTDOWN_QUIET_PERIOD_MS = 100;
private static final int SHUTDOWN_TIMEOUT_MS = 500;

private final GeyserImpl geyser;
private final EventLoopGroup group;
private EventLoopGroup group;
private final ServerBootstrap bootstrap;
private EventLoopGroup playerGroup;

@Getter
private final ExpiringMap<InetSocketAddress, InetSocketAddress> proxiedAddresses;

private ChannelFuture future;
private ChannelFuture bootstrapFuture;

public GeyserServer(GeyserImpl geyser, int threadCount) {
this.geyser = geyser;
Expand All @@ -109,15 +118,15 @@ public GeyserServer(GeyserImpl geyser, int threadCount) {

public CompletableFuture<Void> bind(InetSocketAddress address) {
CompletableFuture<Void> future = new CompletableFuture<>();
this.future = this.bootstrap.bind(address).addListener(bindResult -> {
this.bootstrapFuture = this.bootstrap.bind(address).addListener(bindResult -> {
if (bindResult.cause() != null) {
future.completeExceptionally(bindResult.cause());
return;
}
future.complete(null);
});

Channel channel = this.future.channel();
Channel channel = this.bootstrapFuture.channel();

// Add our ping handler
channel.pipeline()
Expand All @@ -132,8 +141,19 @@ public CompletableFuture<Void> bind(InetSocketAddress address) {
}

public void shutdown() {
this.group.shutdownGracefully();
this.future.channel().closeFuture().syncUninterruptibly();
try {
Future<?> future1 = this.group.shutdownGracefully(SHUTDOWN_QUIET_PERIOD_MS, SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS);
this.group = null;
Future<?> future2 = this.playerGroup.shutdownGracefully(SHUTDOWN_QUIET_PERIOD_MS, SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS);
this.playerGroup = null;
future1.sync();
future2.sync();

SkinProvider.shutdown();
} catch (InterruptedException e) {
GeyserImpl.getInstance().getLogger().severe("Exception in shutdown process", e);
}
this.bootstrapFuture.channel().closeFuture().syncUninterruptibly();
}

private ServerBootstrap createBootstrap(EventLoopGroup group) {
Expand All @@ -149,11 +169,13 @@ private ServerBootstrap createBootstrap(EventLoopGroup group) {
}
}

GeyserServerInitializer serverInitializer = new GeyserServerInitializer(this.geyser);
playerGroup = serverInitializer.getEventLoopGroup();
return new ServerBootstrap()
.channelFactory(RakChannelFactory.server(TRANSPORT.datagramChannel()))
.group(group)
.option(RakChannelOption.RAK_HANDLE_PING, true)
.childHandler(new GeyserServerInitializer(this.geyser));
.childHandler(serverInitializer);
}

public boolean onConnectionRequest(InetSocketAddress inetSocketAddress) {
Expand Down Expand Up @@ -217,7 +239,7 @@ public BedrockPong onQuery(InetSocketAddress inetSocketAddress) {
.version(GameProtocol.DEFAULT_BEDROCK_CODEC.getMinecraftVersion()) // Required to not be empty as of 1.16.210.59. Can only contain . and numbers.
.ipv4Port(this.geyser.getConfig().getBedrock().port())
.ipv6Port(this.geyser.getConfig().getBedrock().port())
.serverId(future.channel().config().getOption(RakChannelOption.RAK_GUID));
.serverId(bootstrapFuture.channel().config().getOption(RakChannelOption.RAK_GUID));

if (config.isPassthroughMotd() && pingInfo != null && pingInfo.getDescription() != null) {
String[] motd = MessageTranslator.convertMessageLenient(pingInfo.getDescription()).split("\n");
Expand Down
28 changes: 21 additions & 7 deletions core/src/main/java/org/geysermc/geyser/skin/SkinProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

public class SkinProvider {
private static final boolean ALLOW_THIRD_PARTY_CAPES = GeyserImpl.getInstance().getConfig().isAllowThirdPartyCapes();
static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(ALLOW_THIRD_PARTY_CAPES ? 21 : 14);
static ExecutorService EXECUTOR_SERVICE;

static final Skin EMPTY_SKIN;
static final Cape EMPTY_CAPE = new Cape("", "no-cape", ByteArrays.EMPTY_ARRAY, -1, true);
Expand Down Expand Up @@ -133,6 +133,20 @@ public class SkinProvider {
WEARING_CUSTOM_SKULL_SLIM = new SkinGeometry("{\"geometry\" :{\"default\" :\"geometry.humanoid.wearingCustomSkullSlim\"}}", wearingCustomSkullSlim, false);
}

private static ExecutorService getExecutorService() {
if (EXECUTOR_SERVICE == null) {
EXECUTOR_SERVICE = Executors.newFixedThreadPool(ALLOW_THIRD_PARTY_CAPES ? 21 : 14);
}
return EXECUTOR_SERVICE;
}

public static void shutdown() {
Konicai marked this conversation as resolved.
Show resolved Hide resolved
if (EXECUTOR_SERVICE != null) {
EXECUTOR_SERVICE.shutdown();
EXECUTOR_SERVICE = null;
}
}

public static void registerCacheImageTask(GeyserImpl geyser) {
// Schedule Daily Image Expiry if we are caching them
if (geyser.getConfig().getCacheImages() > 0) {
Expand Down Expand Up @@ -302,7 +316,7 @@ private static CompletableFuture<SkinAndCape> requestSkinAndCape(UUID playerId,

GeyserImpl.getInstance().getLogger().debug("Took " + (System.currentTimeMillis() - time) + "ms for " + playerId);
return skinAndCape;
}, EXECUTOR_SERVICE);
}, getExecutorService());
}

static CompletableFuture<Skin> requestSkin(UUID playerId, String textureUrl, boolean newThread) {
Expand All @@ -320,7 +334,7 @@ static CompletableFuture<Skin> requestSkin(UUID playerId, String textureUrl, boo

CompletableFuture<Skin> future;
if (newThread) {
future = CompletableFuture.supplyAsync(() -> supplySkin(playerId, textureUrl), EXECUTOR_SERVICE)
future = CompletableFuture.supplyAsync(() -> supplySkin(playerId, textureUrl), getExecutorService())
.whenCompleteAsync((skin, throwable) -> {
skin.updated = true;
CACHED_JAVA_SKINS.put(textureUrl, skin);
Expand Down Expand Up @@ -349,7 +363,7 @@ private static CompletableFuture<Cape> requestCape(String capeUrl, CapeProvider

CompletableFuture<Cape> future;
if (newThread) {
future = CompletableFuture.supplyAsync(() -> supplyCape(capeUrl, provider), EXECUTOR_SERVICE)
future = CompletableFuture.supplyAsync(() -> supplyCape(capeUrl, provider), getExecutorService())
.whenCompleteAsync((cape, throwable) -> {
CACHED_JAVA_CAPES.put(capeUrl, cape);
requestedCapes.remove(capeUrl);
Expand Down Expand Up @@ -388,7 +402,7 @@ private static CompletableFuture<Skin> requestEars(String earsUrl, boolean newTh

CompletableFuture<Skin> future;
if (newThread) {
future = CompletableFuture.supplyAsync(() -> supplyEars(skin, earsUrl), EXECUTOR_SERVICE)
future = CompletableFuture.supplyAsync(() -> supplyEars(skin, earsUrl), getExecutorService())
.whenCompleteAsync((outSkin, throwable) -> { });
} else {
Skin ears = supplyEars(skin, earsUrl); // blocking
Expand Down Expand Up @@ -620,7 +634,7 @@ public static CompletableFuture<String> requestTexturesFromUUID(String uuid) {
}
return null;
}
}, EXECUTOR_SERVICE);
}, getExecutorService());
}

/**
Expand All @@ -646,7 +660,7 @@ public static CompletableFuture<String> requestTexturesFromUsername(String usern
}
return null;
}
}, EXECUTOR_SERVICE).thenCompose(uuid -> {
}, getExecutorService()).thenCompose(uuid -> {
if (uuid == null) {
return CompletableFuture.completedFuture(null);
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protocol-connection = "3.0.0.Beta1-20230908.171156-105"
raknet = "1.0.0.CR1-20230703.195238-9"
blockstateupdater="1.20.30-20230918.203831-4"
mcauthlib = "d9d773e"
mcprotocollib = "1.20.2-1-20231001.173210-4"
mcprotocollib = "1.20.2-1-20231001.201013-5"
adventure = "4.14.0"
adventure-platform = "4.3.0"
junit = "5.9.2"
Expand Down