Skip to content

Commit

Permalink
feat(gateway): implemented virtual threads in most gateway locations
Browse files Browse the repository at this point in the history
  • Loading branch information
seailz committed Sep 23, 2023
1 parent 82469f4 commit f7aaa77
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void addListener(DiscordListener... listeners) {
*/
public void dispatchEvent(Event event, Class<? extends Event> type, DiscordJar djv) {
if (event == null) return;
new Thread(() -> {
DiscordJarThreadAllocator.requestVirtualThread(() -> {
long start = System.currentTimeMillis();
List<ListenerMethodPair> listenersForEventType = listenersByEventType.get(type);
if (listenersForEventType == null) {
Expand All @@ -91,7 +91,7 @@ public void dispatchEvent(Event event, Class<? extends Event> type, DiscordJar d
}

method.setAccessible(true);
DiscordJarThreadAllocator.requestThread(() -> {
DiscordJarThreadAllocator.requestVirtualThread(() -> {
try {
method.invoke(listenerMethodPair.listener, event);
} catch (IllegalAccessException | ArrayIndexOutOfBoundsException e) {
Expand All @@ -102,8 +102,8 @@ public void dispatchEvent(Event event, Class<? extends Event> type, DiscordJar d
System.out.println(method.getDeclaringClass().getSimpleName() + "#" + method.getName() + " threw an exception while being invoked.");
e.getCause().printStackTrace();
}
}, "djar--EventDispatcher-inner").start();
}, "djar--EventDispatcher-inner");
}
}, "djar--EventDispatcher").start();
}, "djar--EventDispatcher");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.seailz.discordjar.utils.URLS;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import com.seailz.discordjar.utils.rest.DiscordResponse;
import com.seailz.discordjar.utils.thread.DiscordJarThreadAllocator;
import com.seailz.discordjar.voice.model.VoiceServerUpdate;
import com.seailz.discordjar.voice.model.VoiceState;
import com.seailz.discordjar.ws.ExponentialBackoffLogic;
Expand Down Expand Up @@ -109,7 +110,7 @@ public GatewayFactory(DiscordJar discordJar, boolean debug, int shardId, int num
ExponentialBackoffLogic backoffReconnectLogic = new ExponentialBackoffLogic();
socket.setReEstablishConnection(backoffReconnectLogic.getFunction());
backoffReconnectLogic.setAttemptReconnect((c) -> {
new Thread(discordJar::clearMemberCaches, "djar--clearing-member-caches").start();
DiscordJarThreadAllocator.requestVirtualThread(discordJar::clearMemberCaches, "djar--clearing-member-caches");
return !shouldResume;
});

Expand Down Expand Up @@ -352,7 +353,7 @@ private void handleDispatched(JSONObject payload) {
}
if (eventClass.equals(CommandInteractionEvent.class)) return;

new Thread(() -> {
DiscordJarThreadAllocator.requestVirtualThread(() -> {
Event event;
try {
event = eventClass.getConstructor(DiscordJar.class, long.class, JSONObject.class)
Expand All @@ -373,7 +374,7 @@ private void handleDispatched(JSONObject payload) {
if (debug) {
logger.info("[DISCORD.JAR - DEBUG] Event dispatched: " + eventClass.getName());
}
}, "djar--event-dispatch-gw").start();
}, "djar--event-dispatch-gw");

if (Objects.requireNonNull(DispatchedEvents.getEventByName(payload.getString("t"))) == DispatchedEvents.READY) {
this.sessionId = payload.getJSONObject("d").getString("session_id");
Expand Down

0 comments on commit f7aaa77

Please sign in to comment.