From 192ce5e1521c8d54a5d910011b305f8fa44f2f37 Mon Sep 17 00:00:00 2001 From: Vankka Date: Tue, 19 Dec 2023 00:36:53 +0200 Subject: [PATCH] Single threaded dependency downloading, don't run enable if dependency downloading fails --- .../bukkit/DiscordSRVBukkitBootstrap.java | 10 ++++- .../discordsrv/common/AbstractDiscordSRV.java | 4 +- .../common/bootstrap/LifecycleManager.java | 37 +++++++++---------- .../sponge/DiscordSRVSpongeBootstrap.java | 3 +- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/bukkit/src/main/java/com/discordsrv/bukkit/DiscordSRVBukkitBootstrap.java b/bukkit/src/main/java/com/discordsrv/bukkit/DiscordSRVBukkitBootstrap.java index a8df5ec8..587835db 100644 --- a/bukkit/src/main/java/com/discordsrv/bukkit/DiscordSRVBukkitBootstrap.java +++ b/bukkit/src/main/java/com/discordsrv/bukkit/DiscordSRVBukkitBootstrap.java @@ -76,12 +76,18 @@ public class DiscordSRVBukkitBootstrap extends BukkitBootstrap implements IBoots } catch (ClassNotFoundException ignored) {} if (isFolia) { - discordSRV.invokeServerStarted(); + if (discordSRV != null) { + discordSRV.invokeServerStarted(); + } return; } // Run a task on the main thread 1 tick later, so essentially when the server has finished booting - getPlugin().getServer().getScheduler().runTaskLater(getPlugin(), () -> discordSRV.invokeServerStarted(), 1L); + getPlugin().getServer().getScheduler().runTaskLater(getPlugin(), () -> { + if (discordSRV != null) { + discordSRV.invokeServerStarted(); + } + }, 1L); } @Override diff --git a/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java b/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java index 5525be06..1e72c5bd 100644 --- a/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java +++ b/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java @@ -205,9 +205,7 @@ public abstract class AbstractDiscordSRV< .addInterceptor(chain -> { Request original = chain.request(); String host = original.url().host(); - boolean isDiscord = host.endsWith("discord.com") - || host.endsWith("discordapp.com") - || host.endsWith("discord.gg"); + boolean isDiscord = host.matches("(.*\\.|^)(?:discord\\.(?:com|gg)|(discordapp\\.com))"); String userAgent = isDiscord ? "DiscordBot (https://github.com/DiscordSRV/DiscordSRV, " + versionInfo().version() + ")" diff --git a/common/src/main/java/com/discordsrv/common/bootstrap/LifecycleManager.java b/common/src/main/java/com/discordsrv/common/bootstrap/LifecycleManager.java index 4d977cf0..74e70b26 100644 --- a/common/src/main/java/com/discordsrv/common/bootstrap/LifecycleManager.java +++ b/common/src/main/java/com/discordsrv/common/bootstrap/LifecycleManager.java @@ -22,7 +22,6 @@ import com.discordsrv.api.DiscordSRVApi; import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.dependency.DependencyLoader; import com.discordsrv.common.logging.Logger; -import com.discordsrv.common.scheduler.threadfactory.CountingForkJoinWorkerThreadFactory; import dev.vankka.dependencydownload.classpath.ClasspathAppender; import java.io.IOException; @@ -33,13 +32,14 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; -import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.function.Supplier; public class LifecycleManager { private final Logger logger; - private final ForkJoinPool taskPool; + private final ExecutorService taskPool; private final DependencyLoader dependencyLoader; private final CompletableFuture completableFuture; @@ -50,12 +50,7 @@ public class LifecycleManager { ClasspathAppender classpathAppender ) throws IOException { this.logger = logger; - this.taskPool = new ForkJoinPool( - Runtime.getRuntime().availableProcessors(), - new CountingForkJoinWorkerThreadFactory("DiscordSRV Initialization #%s"), - null, - false - ); + this.taskPool = Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, "DiscordSRV Initialization")); List resourcePaths = new ArrayList<>(Collections.singletonList( "dependencies/runtimeDownload-common.txt" @@ -74,22 +69,24 @@ public class LifecycleManager { } public void loadAndEnable(Supplier discordSRVSupplier) { - load(); - enable(discordSRVSupplier); + if (load()) { + enable(discordSRVSupplier); + } } - public void load() { + private boolean load() { try { completableFuture.get(); - } catch (ExecutionException | InterruptedException e) { - logger.error("Failed to download dependencies", e); + return true; + } catch (InterruptedException ignored) { + Thread.currentThread().interrupt(); + } catch (ExecutionException e) { + logger.error("Failed to download dependencies", e.getCause()); } + return false; } - public void enable(Supplier discordSRVSupplier) { - if (!completableFuture.isDone()) { - return; - } + private void enable(Supplier discordSRVSupplier) { discordSRVSupplier.get().runEnable(); } @@ -114,7 +111,9 @@ public class LifecycleManager { discordSRV.invokeDisable().get(/*15, TimeUnit.SECONDS*/); } catch (InterruptedException/* | TimeoutException*/ e) { logger.warning("Timed out/interrupted shutting down DiscordSRV"); - } catch (ExecutionException ignored) {} + } catch (ExecutionException e) { + logger.error("Failed to disable", e.getCause()); + } } public DependencyLoader getDependencyLoader() { diff --git a/sponge/src/main/java/com/discordsrv/sponge/DiscordSRVSpongeBootstrap.java b/sponge/src/main/java/com/discordsrv/sponge/DiscordSRVSpongeBootstrap.java index 69a0c41f..c6fc8ff3 100644 --- a/sponge/src/main/java/com/discordsrv/sponge/DiscordSRVSpongeBootstrap.java +++ b/sponge/src/main/java/com/discordsrv/sponge/DiscordSRVSpongeBootstrap.java @@ -65,7 +65,7 @@ public class DiscordSRVSpongeBootstrap extends AbstractBootstrap implements ISpo @Override public void onConstruct() { - lifecycleManager.load(); + lifecycleManager.loadAndEnable(() -> this.discordSRV = new SpongeDiscordSRV(this)); this.commandHandler = new SpongeCommandHandler(() -> discordSRV, pluginContainer); game.eventManager().registerListeners(pluginContainer, commandHandler); @@ -73,7 +73,6 @@ public class DiscordSRVSpongeBootstrap extends AbstractBootstrap implements ISpo @Override public void onStarted() { - lifecycleManager.enable(() -> this.discordSRV = new SpongeDiscordSRV(this)); if (discordSRV != null) { discordSRV.invokeServerStarted(); }