fix: limit the queue amount on the database executor

This commit is contained in:
ChomeNS
2025-03-16 20:39:12 +07:00
parent 8e20cf2285
commit 60350a1adb
5 changed files with 18 additions and 1 deletions

View File

@@ -33,6 +33,8 @@ public class FindAltsCommand extends Command {
if (bot.database == null) throw new CommandException(Component.text("Database is not enabled in the bot's config"));
bot.database.checkOverloaded();
final String flag = context.getString(false, true);
final boolean allServer = flag.equals("-allserver");

View File

@@ -40,6 +40,8 @@ public class MailCommand extends Command {
if (bot.database == null) throw new CommandException(Component.text("Database is not enabled in the bot's config"));
bot.database.checkOverloaded();
final PlayerEntry sender = context.sender;
// kinda messy ngl

View File

@@ -38,6 +38,8 @@ public class SeenCommand extends Command {
if (bot.database == null) throw new CommandException(Component.text("Database is not enabled in the bot's config"));
bot.database.checkOverloaded();
final String player = context.getString(true, true);
boolean online = false;

View File

@@ -4,11 +4,14 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.Configuration;
import me.chayapak1.chomens_bot.Main;
import me.chayapak1.chomens_bot.command.CommandException;
import me.chayapak1.chomens_bot.util.LoggerUtilities;
import net.kyori.adventure.text.Component;
import java.sql.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class DatabasePlugin {
public static final ExecutorService executorService = Executors.newFixedThreadPool(
@@ -33,6 +36,14 @@ public class DatabasePlugin {
for (Bot bot : Main.bots) bot.database = this;
}
public void checkOverloaded () throws CommandException {
final ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executorService;
if (threadPoolExecutor.getQueue().size() > 20) throw new CommandException(
Component.text("The executor service is filled with requests!")
);
}
public boolean execute (String query) throws SQLException {
final Statement statement = connection.createStatement();