Skip to content

Commit

Permalink
perf: 🧵 thread management (resolve #27)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzhiZhang committed Oct 2, 2024
1 parent c651082 commit 7554ba4
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/main/java/com/zhanganzhi/chathub/ChatHub.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zhanganzhi.chathub;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.inject.Inject;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
Expand All @@ -14,6 +15,8 @@
import org.slf4j.Logger;

import java.nio.file.Path;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

@Plugin(
id = "chathub",
Expand All @@ -32,6 +35,8 @@ public class ChatHub {
private final Logger logger;
private final Path dataDirectory;
@Getter
private ThreadPoolExecutor threadPoolExecutor;
@Getter
private EventHub eventHub;

@Inject
Expand All @@ -44,7 +49,8 @@ public ChatHub(ProxyServer proxyServer, Logger logger, @DataDirectory Path dataD
@Subscribe
public void onInitialize(ProxyInitializeEvent event) {
// core
Config.getInstance().loadConfig(dataDirectory);
Config config = Config.getInstance();
config.loadConfig(dataDirectory);
eventHub = new EventHub(this);

// command
Expand All @@ -53,6 +59,12 @@ public void onInitialize(ProxyInitializeEvent event) {
new VelocityCommand(this)
);

// thread pool
threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(
config.getCoreThreadPoolSize(),
new ThreadFactoryBuilder().setNameFormat("chathub-tasks-%d").build()
);

// init event hub
new Thread(() -> eventHub.start(), "chathub-event-hub-start-event").start();
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/zhanganzhi/chathub/core/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public void setIsKookEnabled(boolean isKookEnabled) {
this.tempIsKookEnabled = isKookEnabled;
}

public int getCoreThreadPoolSize() {
return configToml.getLong("core.threadPoolSize").intValue();
}

public String getServername(String server) {
String servername = configToml.getString("servername." + server);
return servername != null ? servername : server;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ public void stop() {

@Override
public void sendPublicMessage(String message) {
new Thread(() -> channel.sendMessage(message).queue()).start();
chatHub.getThreadPoolExecutor().submit(() -> channel.sendMessage(message).queue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public void restart() {

@Override
public void sendPublicMessage(String message) {
new Thread(() -> kookAPI.sendMessage(message)).start();
chatHub.getThreadPoolExecutor().submit(() -> kookAPI.sendMessage(message));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void stop() {

@Override
public void sendPublicMessage(String message) {
new Thread(() -> qqAPI.sendMessage(message, config.getQQGroupId())).start();
chatHub.getThreadPoolExecutor().submit(() -> qqAPI.sendMessage(message, config.getQQGroupId()));
}

public void eventListener() {
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[core]
threadPoolSize = 4

[servername]
lobby = '§f§l大厅服'
survival = '§2§l生存服'
Expand Down

0 comments on commit 7554ba4

Please # to comment.