mirror of
https://github.com/DiscordSRV/Ascension.git
synced 2025-01-03 18:38:26 +01:00
Add some comments to JDAConnectionManager, re-make executor services when reconnecting, change some JDABuilder settings
This commit is contained in:
parent
205cd13dd8
commit
41b469f89f
@ -77,8 +77,8 @@ public class JDAConnectionManager implements DiscordConnectionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private final DiscordSRV discordSRV;
|
private final DiscordSRV discordSRV;
|
||||||
private final ScheduledExecutorService gatewayPool;
|
private ScheduledExecutorService gatewayPool;
|
||||||
private final ScheduledExecutorService rateLimitPool;
|
private ScheduledExecutorService rateLimitPool;
|
||||||
|
|
||||||
private CompletableFuture<Void> connectionFuture;
|
private CompletableFuture<Void> connectionFuture;
|
||||||
private JDA instance;
|
private JDA instance;
|
||||||
@ -94,14 +94,6 @@ public class JDAConnectionManager implements DiscordConnectionManager {
|
|||||||
|
|
||||||
public JDAConnectionManager(DiscordSRV discordSRV) {
|
public JDAConnectionManager(DiscordSRV discordSRV) {
|
||||||
this.discordSRV = discordSRV;
|
this.discordSRV = discordSRV;
|
||||||
this.gatewayPool = new ScheduledThreadPoolExecutor(
|
|
||||||
1,
|
|
||||||
r -> new Thread(r, Scheduler.THREAD_NAME_PREFIX + "JDA Gateway")
|
|
||||||
);
|
|
||||||
this.rateLimitPool = new ScheduledThreadPoolExecutor(
|
|
||||||
5,
|
|
||||||
new CountingThreadFactory(Scheduler.THREAD_NAME_PREFIX + "JDA RateLimit #%s")
|
|
||||||
);
|
|
||||||
|
|
||||||
// Set default failure handling
|
// Set default failure handling
|
||||||
RestAction.setDefaultFailure(new DefaultFailureCallback(new NamedLogger(discordSRV, "DISCORD_REQUESTS")));
|
RestAction.setDefaultFailure(new DefaultFailureCallback(new NamedLogger(discordSRV, "DISCORD_REQUESTS")));
|
||||||
@ -224,12 +216,20 @@ public class JDAConnectionManager implements DiscordConnectionManager {
|
|||||||
return future;
|
return future;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("BusyWait")
|
|
||||||
private void connectInternal() {
|
private void connectInternal() {
|
||||||
discordSRV.discordConnectionDetails().requestGatewayIntent(GatewayIntent.GUILD_MESSAGES); // TODO: figure out how DiscordSRV required intents are going to work
|
discordSRV.discordConnectionDetails().requestGatewayIntent(GatewayIntent.GUILD_MESSAGES); // TODO: figure out how DiscordSRV required intents are going to work
|
||||||
discordSRV.discordConnectionDetails().requestGatewayIntent(GatewayIntent.GUILD_MEMBERS); // TODO: figure out how DiscordSRV required intents are going to work
|
discordSRV.discordConnectionDetails().requestGatewayIntent(GatewayIntent.GUILD_MEMBERS); // TODO: figure out how DiscordSRV required intents are going to work
|
||||||
detailsAccepted = false;
|
detailsAccepted = false;
|
||||||
|
|
||||||
|
this.gatewayPool = new ScheduledThreadPoolExecutor(
|
||||||
|
1,
|
||||||
|
r -> new Thread(r, Scheduler.THREAD_NAME_PREFIX + "JDA Gateway")
|
||||||
|
);
|
||||||
|
this.rateLimitPool = new ScheduledThreadPoolExecutor(
|
||||||
|
5,
|
||||||
|
new CountingThreadFactory(Scheduler.THREAD_NAME_PREFIX + "JDA RateLimit #%s")
|
||||||
|
);
|
||||||
|
|
||||||
ConnectionConfig.Bot botConfig = discordSRV.connectionConfig().bot;
|
ConnectionConfig.Bot botConfig = discordSRV.connectionConfig().bot;
|
||||||
DiscordConnectionDetails connectionDetails = discordSRV.discordConnectionDetails();
|
DiscordConnectionDetails connectionDetails = discordSRV.discordConnectionDetails();
|
||||||
Set<GatewayIntent> intents = connectionDetails.getGatewayIntents();
|
Set<GatewayIntent> intents = connectionDetails.getGatewayIntents();
|
||||||
@ -241,8 +241,16 @@ public class JDAConnectionManager implements DiscordConnectionManager {
|
|||||||
jdaBuilder.setMemberCachePolicy(membersIntent ? MemberCachePolicy.ALL : MemberCachePolicy.OWNER);
|
jdaBuilder.setMemberCachePolicy(membersIntent ? MemberCachePolicy.ALL : MemberCachePolicy.OWNER);
|
||||||
jdaBuilder.setChunkingFilter(membersIntent ? ChunkingFilter.ALL : ChunkingFilter.NONE);
|
jdaBuilder.setChunkingFilter(membersIntent ? ChunkingFilter.ALL : ChunkingFilter.NONE);
|
||||||
|
|
||||||
|
// We shut down JDA ourselves. Doing it at the JVM's shutdown may cause errors due to classloading
|
||||||
|
jdaBuilder.setEnableShutdownHook(false);
|
||||||
|
|
||||||
|
// We don't use MDC
|
||||||
|
jdaBuilder.setContextEnabled(false);
|
||||||
|
|
||||||
|
// Custom event manager to forward to the DiscordSRV event bus & block using JDA's event listeners
|
||||||
jdaBuilder.setEventManager(new EventManagerProxy(new JDAEventManager(discordSRV), discordSRV.scheduler().forkJoinPool()));
|
jdaBuilder.setEventManager(new EventManagerProxy(new JDAEventManager(discordSRV), discordSRV.scheduler().forkJoinPool()));
|
||||||
|
|
||||||
|
// Our own (named) threads
|
||||||
jdaBuilder.setCallbackPool(discordSRV.scheduler().forkJoinPool());
|
jdaBuilder.setCallbackPool(discordSRV.scheduler().forkJoinPool());
|
||||||
jdaBuilder.setGatewayPool(gatewayPool);
|
jdaBuilder.setGatewayPool(gatewayPool);
|
||||||
jdaBuilder.setRateLimitPool(rateLimitPool);
|
jdaBuilder.setRateLimitPool(rateLimitPool);
|
||||||
@ -257,26 +265,12 @@ public class JDAConnectionManager implements DiscordConnectionManager {
|
|||||||
WebSocketFactory webSocketFactory = new WebSocketFactory();
|
WebSocketFactory webSocketFactory = new WebSocketFactory();
|
||||||
jdaBuilder.setWebsocketFactory(webSocketFactory);
|
jdaBuilder.setWebsocketFactory(webSocketFactory);
|
||||||
|
|
||||||
int timeoutSeconds = 0;
|
try {
|
||||||
while (true) {
|
instance = jdaBuilder.build();
|
||||||
try {
|
} catch (LoginException ignored) {
|
||||||
instance = jdaBuilder.build();
|
invalidToken();
|
||||||
break;
|
} catch (Throwable t) {
|
||||||
} catch (LoginException ignored) {
|
discordSRV.logger().error("Could not create JDA instance due to an unknown error", t);
|
||||||
invalidToken();
|
|
||||||
break;
|
|
||||||
} catch (Throwable t) {
|
|
||||||
discordSRV.logger().error(t);
|
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Doubles the seconds. Min 2s, max 300s (5 minutes)
|
|
||||||
timeoutSeconds = Math.min(Math.max(1, timeoutSeconds) * 2, 300);
|
|
||||||
Thread.sleep(timeoutSeconds);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -301,6 +295,7 @@ public class JDAConnectionManager implements DiscordConnectionManager {
|
|||||||
|
|
||||||
@SuppressWarnings("BusyWait")
|
@SuppressWarnings("BusyWait")
|
||||||
private void shutdownInternal(long timeoutMillis) {
|
private void shutdownInternal(long timeoutMillis) {
|
||||||
|
detailsAccepted = true;
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
shutdownExecutors();
|
shutdownExecutors();
|
||||||
return;
|
return;
|
||||||
|
Loading…
Reference in New Issue
Block a user