Skip to content

Commit

Permalink
[MySQL] Execute MySQL queries in a separate thread
Browse files Browse the repository at this point in the history
  • Loading branch information
NEZNAMY committed Nov 3, 2024
1 parent e4e7c36 commit c15a600
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void download(@Nullable TabPlayer sender) {
sendMessage(sender, getMessages().getMySQLFailNotEnabled());
return;
}
TAB.getInstance().getCPUManager().runTask(() -> {
TAB.getInstance().getCPUManager().getMysqlThread().execute(() -> {
try {
YamlPropertyConfigurationFile groupFile = new YamlPropertyConfigurationFile(Configs.class.getClassLoader().getResourceAsStream("config/groups.yml"), new File(TAB.getInstance().getDataFolder(), "groups.yml"));
YamlPropertyConfigurationFile userFile = new YamlPropertyConfigurationFile(Configs.class.getClassLoader().getResourceAsStream("config/users.yml"), new File(TAB.getInstance().getDataFolder(), "users.yml"));
Expand Down Expand Up @@ -88,7 +88,7 @@ private void upload(@Nullable TabPlayer sender) {
sendMessage(sender, getMessages().getMySQLFailNotEnabled());
return;
}
TAB.getInstance().getCPUManager().runTask(() -> {
TAB.getInstance().getCPUManager().getMysqlThread().execute(() -> {
try {
YamlPropertyConfigurationFile groupFile = new YamlPropertyConfigurationFile(Configs.class.getClassLoader().getResourceAsStream("config/groups.yml"), new File(TAB.getInstance().getDataFolder(), "config/groups.yml"));
YamlPropertyConfigurationFile userFile = new YamlPropertyConfigurationFile(Configs.class.getClassLoader().getResourceAsStream("config/users.yml"), new File(TAB.getInstance().getDataFolder(), "config/users.yml"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package me.neznamy.tab.shared.config.mysql;

import java.sql.SQLException;
import java.util.*;

import javax.sql.rowset.CachedRowSet;

import me.neznamy.tab.shared.TAB;
import me.neznamy.tab.shared.config.PropertyConfiguration;
import me.neznamy.tab.shared.platform.TabPlayer;
import me.neznamy.tab.shared.TAB;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.sql.rowset.CachedRowSet;
import java.sql.SQLException;
import java.util.*;

public class MySQLUserConfiguration implements PropertyConfiguration {

private final MySQL mysql;
Expand Down Expand Up @@ -121,7 +120,7 @@ private TabPlayer getPlayer(@NotNull String string) {
}

public void load(@NotNull TabPlayer player) {
TAB.getInstance().getCPUManager().runTask(() -> {
TAB.getInstance().getCPUManager().getMysqlThread().execute(() -> {

try {
CachedRowSet crs = mysql.getCRS("select * from `tab_users` where `user` = ?", player.getName().toLowerCase());
Expand Down
15 changes: 11 additions & 4 deletions shared/src/main/java/me/neznamy/tab/shared/cpu/CpuManager.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package me.neznamy.tab.shared.cpu;

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
* A class which measures CPU usage of all tasks inserted into it and shows usage
*/
Expand Down Expand Up @@ -44,6 +47,9 @@ public class CpuManager {
/** Scheduler for decoding plugin messages */
private final ThreadExecutor pluginMessageDecodeThread = new ThreadExecutor("TAB Plugin Message Decoding Thread");

/** Scheduler for MySQL tasks */
private final ThreadExecutor mysqlThread = new ThreadExecutor("TAB MySQL Thread");

/** Tasks submitted to main thread before plugin was fully enabled */
private final Queue<Runnable> taskQueue = new ConcurrentLinkedQueue<>();

Expand Down Expand Up @@ -79,6 +85,7 @@ public void cancelAllTasks() {
groupRefreshingThread.shutdown();
tablistEntryCheckThread.shutdown();
pluginMessageDecodeThread.shutdown();
mysqlThread.shutdown();
}

/**
Expand Down

0 comments on commit c15a600

Please sign in to comment.