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 2 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 @@ -26,6 +26,7 @@
package org.geysermc.geyser.network.netty;

import com.github.steveice10.packetlib.helper.TransportHelper;
import com.github.steveice10.packetlib.tcp.TcpClientSession;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
Expand Down Expand Up @@ -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 @@ -84,8 +86,9 @@ public final class GeyserServer {
private static final Transport TRANSPORT = compatibleTransport();

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;
Expand Down Expand Up @@ -132,7 +135,15 @@ public CompletableFuture<Void> bind(InetSocketAddress address) {
}

public void shutdown() {
this.group.shutdownGracefully();
try {
this.group.shutdownGracefully().sync();
this.group = null;
this.playerGroup.shutdownGracefully().sync();
this.playerGroup = null;
SkinProvider.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();
}
this.future.channel().closeFuture().syncUninterruptibly();
}

Expand All @@ -149,11 +160,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
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