Skip to content

Commit

Permalink
Use cached thread pool for queue handling in logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Shea4 committed Jul 24, 2021
1 parent d3b7c4c commit 30e7a68
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Sx4/src/main/java/com/sx4/bot/core/Sx4.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.jockie.bot.core.option.factory.impl.OptionFactoryImpl;
import com.jockie.bot.core.parser.IParser;
import com.jockie.bot.core.parser.ParsedResult;
import com.jockie.jda.memory.MemoryOptimizations;
import com.mongodb.client.model.Projections;
import com.sx4.api.Sx4Server;
import com.sx4.bot.annotations.argument.*;
Expand Down Expand Up @@ -1535,7 +1536,7 @@ public static void main(String[] args) throws Exception {
Sx4 bot = new Sx4();
Sx4Server.initiateWebserver(bot);

//MemoryOptimizations.installAll();
MemoryOptimizations.installOptimizations();

Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> {
System.err.println("[Uncaught]");
Expand Down
9 changes: 6 additions & 3 deletions Sx4/src/main/java/com/sx4/bot/handlers/LoggerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand All @@ -88,8 +89,10 @@ public class LoggerHandler implements EventListener {
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

private final Map<Long, LoggerManager> managers;
private final ScheduledExecutorService managerExecutor = Executors.newScheduledThreadPool(20);
private final OkHttpClient managerClient = new OkHttpClient.Builder()

private final ExecutorService managerExecutor = Executors.newCachedThreadPool();
private final ScheduledExecutorService webhookExecutor = Executors.newScheduledThreadPool(20);
private final OkHttpClient webhookClient = new OkHttpClient.Builder()
.callTimeout(3, TimeUnit.SECONDS)
.build();

Expand All @@ -108,7 +111,7 @@ private LoggerManager getManager(long channelId) {
return this.managers.get(channelId);
}

LoggerManager manager = new LoggerManager(this.bot, this.managerClient, this.managerExecutor);
LoggerManager manager = new LoggerManager(this.bot, this.managerExecutor, this.webhookClient, this.webhookExecutor);
this.managers.put(channelId, manager);

return manager;
Expand Down
6 changes: 3 additions & 3 deletions Sx4/src/main/java/com/sx4/bot/managers/LoggerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import okhttp3.OkHttpClient;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -101,13 +100,14 @@ public Document getLogger() {
private final OkHttpClient webhookClient;
private final ScheduledExecutorService webhookExecutor;

private final ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("logger-executor-%d").build());
private final ExecutorService executor;

private final Sx4 bot;

public LoggerManager(Sx4 bot, OkHttpClient webhookClient, ScheduledExecutorService webhookExecutor) {
public LoggerManager(Sx4 bot, ExecutorService executor, OkHttpClient webhookClient, ScheduledExecutorService webhookExecutor) {
this.queue = new LinkedBlockingDeque<>();
this.bot = bot;
this.executor = executor;
this.webhookExecutor = webhookExecutor;
this.webhookClient = webhookClient;
}
Expand Down

0 comments on commit 30e7a68

Please sign in to comment.