Move command execution to own thread pool

This commit is contained in:
Jason Penilla committed 2023-03-21 09:03:31 -07:00
1 parent 3d6222e858
commit bf69ca0b6d
5 files changed
+86 -3

No files matched your search

@@ -29,6 +29,7 @@ import java.util.UUID;
import net.draycia.carbon.api.channels.ChannelRegistry; import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.users.UserManager; import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.common.channels.CarbonChannelRegistry; import net.draycia.carbon.common.channels.CarbonChannelRegistry;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
import net.draycia.carbon.common.config.ConfigFactory; import net.draycia.carbon.common.config.ConfigFactory;
import net.draycia.carbon.common.messages.CarbonMessageSender; import net.draycia.carbon.common.messages.CarbonMessageSender;
import net.draycia.carbon.common.messages.CarbonMessageSource; import net.draycia.carbon.common.messages.CarbonMessageSource;
@@ -98,6 +99,12 @@ public final class CarbonCommonModule extends AbstractModule {
.create(this.getClass().getClassLoader()); .create(this.getClass().getClassLoader());
} }
@Provides
@Singleton
public ExecutionCoordinatorHolder executionCoordinatorHolder(final Logger logger) {
return ExecutionCoordinatorHolder.create(logger);
}
@Override @Override
protected void configure() { protected void configure() {
this.bind(ChannelRegistry.class).to(CarbonChannelRegistry.class); this.bind(ChannelRegistry.class).to(CarbonChannelRegistry.class);
@@ -0,0 +1,55 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.command.commands;
import cloud.commandframework.CommandTree;
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.util.ConcurrentUtil;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public record ExecutionCoordinatorHolder(
Function<CommandTree<Commander>, CommandExecutionCoordinator<Commander>> executionCoordinator,
ExecutorService executorService
) {
public void shutdown() {
ConcurrentUtil.shutdownExecutor(this.executorService, TimeUnit.MILLISECONDS, 50);
}
public static ExecutionCoordinatorHolder create(final Logger logger) {
final ExecutorService executorService = Executors.newFixedThreadPool(4, ConcurrentUtil.carbonThreadFactory(logger, "Commands"));
return new ExecutionCoordinatorHolder(
AsynchronousCommandExecutionCoordinator.<Commander>builder()
.withExecutor(executorService)
.build(),
executorService
);
}
}
@@ -1,3 +1,22 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.users; package net.draycia.carbon.common.users;
import com.google.gson.Gson; import com.google.gson.Gson;
@@ -32,6 +32,7 @@ import net.draycia.carbon.api.events.CarbonEventHandler;
import net.draycia.carbon.api.users.UserManager; import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.common.ForCarbon; import net.draycia.carbon.common.ForCarbon;
import net.draycia.carbon.common.channels.CarbonChannelRegistry; import net.draycia.carbon.common.channels.CarbonChannelRegistry;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
import net.draycia.carbon.common.listeners.RadiusListener; import net.draycia.carbon.common.listeners.RadiusListener;
import net.draycia.carbon.common.messages.CarbonMessages; import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.messaging.MessagingManager; import net.draycia.carbon.common.messaging.MessagingManager;
@@ -154,6 +155,7 @@ public final class CarbonChatPaper implements CarbonChat {
void onDisable() { void onDisable() {
this.injector.getInstance(ProfileResolver.class).shutdown(); this.injector.getInstance(ProfileResolver.class).shutdown();
this.userManager.shutdown(); this.userManager.shutdown();
this.injector.getInstance(ExecutionCoordinatorHolder.class).shutdown();
} }
@Override @Override
@@ -22,7 +22,6 @@ package net.draycia.carbon.paper;
import cloud.commandframework.CommandManager; import cloud.commandframework.CommandManager;
import cloud.commandframework.brigadier.CloudBrigadierManager; import cloud.commandframework.brigadier.CloudBrigadierManager;
import cloud.commandframework.bukkit.parsers.PlayerArgument; import cloud.commandframework.bukkit.parsers.PlayerArgument;
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator;
import cloud.commandframework.paper.PaperCommandManager; import cloud.commandframework.paper.PaperCommandManager;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Injector; import com.google.inject.Injector;
@@ -36,6 +35,7 @@ import net.draycia.carbon.common.CarbonCommonModule;
import net.draycia.carbon.common.ForCarbon; import net.draycia.carbon.common.ForCarbon;
import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.argument.PlayerSuggestions; import net.draycia.carbon.common.command.argument.PlayerSuggestions;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
import net.draycia.carbon.common.messages.CarbonMessages; import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.users.ProfileResolver; import net.draycia.carbon.common.users.ProfileResolver;
import net.draycia.carbon.common.util.CloudUtils; import net.draycia.carbon.common.util.CloudUtils;
@@ -72,13 +72,13 @@ public final class CarbonChatPaperModule extends AbstractModule {
@Provides @Provides
@Singleton @Singleton
public CommandManager<Commander> commandManager(final CarbonServer server, final CarbonMessages messages) { public CommandManager<Commander> commandManager(final CarbonServer server, final CarbonMessages messages, final ExecutionCoordinatorHolder executionCoordinatorHolder) {
final PaperCommandManager<Commander> commandManager; final PaperCommandManager<Commander> commandManager;
try { try {
commandManager = new PaperCommandManager<>( commandManager = new PaperCommandManager<>(
this.bootstrap, this.bootstrap,
AsynchronousCommandExecutionCoordinator.<Commander>builder().build(), executionCoordinatorHolder.executionCoordinator(),
commandSender -> { commandSender -> {
if (commandSender instanceof Player player) { if (commandSender instanceof Player player) {
return new PaperPlayerCommander(server, player); return new PaperPlayerCommander(server, player);