From f7aaa77f7b88aea179544342e6b590c29422b0b2 Mon Sep 17 00:00:00 2001 From: IoyoCode Date: Sat, 23 Sep 2023 15:07:51 +0100 Subject: [PATCH] feat(gateway): implemented virtual threads in most gateway locations --- .../com/seailz/discordjar/events/EventDispatcher.java | 8 ++++---- .../com/seailz/discordjar/gateway/GatewayFactory.java | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/seailz/discordjar/events/EventDispatcher.java b/src/main/java/com/seailz/discordjar/events/EventDispatcher.java index f74eb770..a7d13666 100644 --- a/src/main/java/com/seailz/discordjar/events/EventDispatcher.java +++ b/src/main/java/com/seailz/discordjar/events/EventDispatcher.java @@ -69,7 +69,7 @@ public void addListener(DiscordListener... listeners) { */ public void dispatchEvent(Event event, Class type, DiscordJar djv) { if (event == null) return; - new Thread(() -> { + DiscordJarThreadAllocator.requestVirtualThread(() -> { long start = System.currentTimeMillis(); List listenersForEventType = listenersByEventType.get(type); if (listenersForEventType == null) { @@ -91,7 +91,7 @@ public void dispatchEvent(Event event, Class type, DiscordJar d } method.setAccessible(true); - DiscordJarThreadAllocator.requestThread(() -> { + DiscordJarThreadAllocator.requestVirtualThread(() -> { try { method.invoke(listenerMethodPair.listener, event); } catch (IllegalAccessException | ArrayIndexOutOfBoundsException e) { @@ -102,8 +102,8 @@ public void dispatchEvent(Event event, Class 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"); } } \ No newline at end of file diff --git a/src/main/java/com/seailz/discordjar/gateway/GatewayFactory.java b/src/main/java/com/seailz/discordjar/gateway/GatewayFactory.java index 1a506540..a0e0bd1d 100644 --- a/src/main/java/com/seailz/discordjar/gateway/GatewayFactory.java +++ b/src/main/java/com/seailz/discordjar/gateway/GatewayFactory.java @@ -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; @@ -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; }); @@ -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) @@ -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");