From d4f41c589d7b6494ec617cb19d1380a6dbb3ab15 Mon Sep 17 00:00:00 2001 From: Vankka Date: Tue, 11 Jul 2023 18:22:41 +0300 Subject: [PATCH] Tweak thread pools thread counts, separate required linking thread pool --- .../BukkitRequiredLinkingListener.java | 18 ++++++---- .../BukkitRequiredLinkingModule.java | 5 +-- .../requirelinking/RequiredLinkingModule.java | 35 +++++++++++++++++++ .../ServerRequireLinkingModule.java | 13 ++++--- .../common/scheduler/StandardScheduler.java | 15 ++++---- 5 files changed, 66 insertions(+), 20 deletions(-) diff --git a/bukkit/src/main/java/com/discordsrv/bukkit/listener/BukkitRequiredLinkingListener.java b/bukkit/src/main/java/com/discordsrv/bukkit/listener/BukkitRequiredLinkingListener.java index 1506e5f5..1bb268fc 100644 --- a/bukkit/src/main/java/com/discordsrv/bukkit/listener/BukkitRequiredLinkingListener.java +++ b/bukkit/src/main/java/com/discordsrv/bukkit/listener/BukkitRequiredLinkingListener.java @@ -90,13 +90,13 @@ public class BukkitRequiredLinkingListener implements Listener { return module; } - private CompletableFuture getBlockReason(UUID playerUUID) { + private CompletableFuture getBlockReason(UUID playerUUID, String playerName) { BukkitRequiredLinkingModule module = getModule(); if (module == null) { return CompletableFuture.completedFuture(Component.text("Discord unavailable, please try again later")); } - return module.getBlockReason(playerUUID); + return module.getBlockReason(playerUUID, playerName); } // @@ -108,16 +108,19 @@ public class BukkitRequiredLinkingListener implements Listener { "AsyncPlayerPreLoginEvent", priority, event.getUniqueId(), + event.getName(), () -> event.getLoginResult() != AsyncPlayerPreLoginEvent.Result.ALLOWED ? event.getLoginResult().name() : null, text -> event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, text) ); } private void handle(PlayerLoginEvent event, EventPriority priority) { + Player player = event.getPlayer(); handle( "PlayerLoginEvent", priority, - event.getPlayer().getUniqueId(), + player.getUniqueId(), + player.getName(), () -> event.getResult() != PlayerLoginEvent.Result.ALLOWED ? event.getResult().name() : null, text -> event.disallow(PlayerLoginEvent.Result.KICK_OTHER, text) ); @@ -127,6 +130,7 @@ public class BukkitRequiredLinkingListener implements Listener { String eventType, EventPriority priority, UUID playerUUID, + String playerName, Supplier alreadyBlocked, Consumer disallow ) { @@ -138,11 +142,11 @@ public class BukkitRequiredLinkingListener implements Listener { String blockType = alreadyBlocked.get(); if (blockType != null) { - discordSRV.logger().debug(playerUUID + " is already blocked for " + eventType + "/" + priority + " (" + blockType + ")"); + discordSRV.logger().debug(playerName + " is already blocked for " + eventType + "/" + priority + " (" + blockType + ")"); return; } - Component kickReason = getBlockReason(playerUUID).join(); + Component kickReason = getBlockReason(playerUUID, playerName).join(); if (kickReason != null) { disallow.accept(BukkitComponentSerializer.legacy().serialize(kickReason)); } @@ -179,7 +183,7 @@ public class BukkitRequiredLinkingListener implements Listener { return; } - Component blockReason = getBlockReason(event.getUniqueId()).join(); + Component blockReason = getBlockReason(event.getUniqueId(), event.getName()).join(); if (blockReason != null) { frozen.put(event.getUniqueId(), blockReason); } @@ -267,7 +271,7 @@ public class BukkitRequiredLinkingListener implements Listener { player.sendMessage(Component.text("Checking...")); UUID uuid = player.uniqueId(); - getBlockReason(uuid).whenComplete((reason, t) -> { + getBlockReason(uuid, player.username()).whenComplete((reason, t) -> { if (t != null) { return; } diff --git a/bukkit/src/main/java/com/discordsrv/bukkit/requiredlinking/BukkitRequiredLinkingModule.java b/bukkit/src/main/java/com/discordsrv/bukkit/requiredlinking/BukkitRequiredLinkingModule.java index f8448d22..154e4c6a 100644 --- a/bukkit/src/main/java/com/discordsrv/bukkit/requiredlinking/BukkitRequiredLinkingModule.java +++ b/bukkit/src/main/java/com/discordsrv/bukkit/requiredlinking/BukkitRequiredLinkingModule.java @@ -19,6 +19,7 @@ package com.discordsrv.bukkit.requiredlinking; import com.discordsrv.bukkit.BukkitDiscordSRV; +import com.discordsrv.bukkit.config.main.BukkitRequiredLinkingConfig; import com.discordsrv.common.config.main.linking.RequirementsConfig; import com.discordsrv.common.linking.requirelinking.ServerRequireLinkingModule; import org.bukkit.event.Listener; @@ -30,7 +31,7 @@ public class BukkitRequiredLinkingModule extends ServerRequireLinkingModule extends AbstractModule { private final List> availableRequirements = new ArrayList<>(); + private ThreadPoolExecutor executor; public RequiredLinkingModule(T discordSRV) { super(discordSRV); } + public abstract RequiredLinkingConfig config(); + + @Override + public boolean isEnabled() { + return config().enabled; + } + + @Override + public void enable() { + executor = new ThreadPoolExecutor( + 1, + Math.max(2, Runtime.getRuntime().availableProcessors() / 2), + 10, + TimeUnit.SECONDS, + new SynchronousQueue<>(), + new CountingThreadFactory(Scheduler.THREAD_NAME_PREFIX + "RequiredLinking #%s") + ); + + super.enable(); + } + + @Override + public void disable() { + if (executor != null) { + executor.shutdown(); + } + } + @Override public void reloadNoResult() { List> requirements = new ArrayList<>(); diff --git a/common/src/main/java/com/discordsrv/common/linking/requirelinking/ServerRequireLinkingModule.java b/common/src/main/java/com/discordsrv/common/linking/requirelinking/ServerRequireLinkingModule.java index 6e299364..16786529 100644 --- a/common/src/main/java/com/discordsrv/common/linking/requirelinking/ServerRequireLinkingModule.java +++ b/common/src/main/java/com/discordsrv/common/linking/requirelinking/ServerRequireLinkingModule.java @@ -20,6 +20,7 @@ package com.discordsrv.common.linking.requirelinking; import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.config.main.linking.RequirementsConfig; +import com.discordsrv.common.config.main.linking.ServerRequiredLinkingConfig; import com.discordsrv.common.future.util.CompletableFutureUtil; import com.discordsrv.common.linking.LinkProvider; import net.kyori.adventure.text.Component; @@ -38,7 +39,8 @@ public abstract class ServerRequireLinkingModule extends R super(discordSRV); } - public abstract RequirementsConfig config(); + @Override + public abstract ServerRequiredLinkingConfig config(); @Override public void reloadNoResult() { @@ -46,14 +48,15 @@ public abstract class ServerRequireLinkingModule extends R synchronized (compiledRequirements) { compiledRequirements.clear(); - compiledRequirements.addAll(compile(config().requirements)); + compiledRequirements.addAll(compile(config().requirements.requirements)); } } - public CompletableFuture getBlockReason(UUID playerUUID) { - RequirementsConfig config = config(); + public CompletableFuture getBlockReason(UUID playerUUID, String playerName) { + RequirementsConfig config = config().requirements; if (config.bypassUUIDs.contains(playerUUID.toString())) { // Bypasses: let them through + logger().debug("Player " + playerName + " is bypassing required linking requirements"); return CompletableFuture.completedFuture(null); } @@ -93,7 +96,7 @@ public abstract class ServerRequireLinkingModule extends R pass.complete(null); } }).exceptionally(t -> { - logger().debug("Check \"" + requirement.input() + "\" failed for " + playerUUID + " / " + Long.toUnsignedString(userId), t); + logger().debug("Check \"" + requirement.input() + "\" failed for " + playerName + " / " + Long.toUnsignedString(userId), t); return null; }); } diff --git a/common/src/main/java/com/discordsrv/common/scheduler/StandardScheduler.java b/common/src/main/java/com/discordsrv/common/scheduler/StandardScheduler.java index a6b184fb..99407540 100644 --- a/common/src/main/java/com/discordsrv/common/scheduler/StandardScheduler.java +++ b/common/src/main/java/com/discordsrv/common/scheduler/StandardScheduler.java @@ -40,14 +40,17 @@ public class StandardScheduler implements Scheduler { this( discordSRV, new ThreadPoolExecutor( - 1, /* Core pool size */ - 20, /* Max pool size */ - 60, TimeUnit.SECONDS, /* Timeout */ + /* Core pool size */ + 1, + /* Max pool size: cpu cores / 2 or at least 1 */ + Math.max(1, Runtime.getRuntime().availableProcessors() / 2), + /* Timeout */ + 60, TimeUnit.SECONDS, new SynchronousQueue<>(), new CountingThreadFactory(Scheduler.THREAD_NAME_PREFIX + "Executor #%s") ), new ScheduledThreadPoolExecutor( - 0, /* Core pool size */ + 1, /* Core pool size */ new CountingThreadFactory(Scheduler.THREAD_NAME_PREFIX + "Scheduled Executor #%s") ), new ForkJoinPool( @@ -55,12 +58,12 @@ public class StandardScheduler implements Scheduler { Math.min( /* max of 10 */ 10, - /* cpu cores - 1 or at least 1 */ + /* cpu cores / 2 or at least 1 */ Math.max(1, Runtime.getRuntime().availableProcessors() - 1) ), new CountingForkJoinWorkerThreadFactory(Scheduler.THREAD_NAME_PREFIX + "ForkJoinPool Worker #%s"), null, - false /* FIFO */ + false /* async mode -> FIFO */ ) ); }