fix: limit the queue amount on the database executor
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user