Skip to content

Commit 8b4c05a

Browse files
authored
Merge branch 'main' into slash-commands
2 parents 5dd7dd4 + adfd89b commit 8b4c05a

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

api/src/main/java/com/javadiscord/jdi/internal/api/DiscordRequestDispatcher.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class DiscordRequestDispatcher implements Runnable {
2323
private final HttpClient httpClient;
2424
private final BlockingQueue<DiscordRequestBuilder> queue;
2525
private final String botToken;
26-
private AtomicBoolean running = new AtomicBoolean(false);
26+
private final AtomicBoolean running = new AtomicBoolean(false);
2727
private int numberOfRequestsSent;
2828
private long timeSinceLastRequest;
2929

@@ -45,6 +45,8 @@ public DiscordResponseFuture queue(DiscordRequest discordRequest) {
4545
public void run() {
4646
running.set(true);
4747

48+
LOGGER.info("Request dispatcher has started");
49+
4850
while (running.get()) {
4951
long currentTime = System.currentTimeMillis();
5052
long elapsed = currentTime - timeSinceLastRequest;
@@ -64,6 +66,8 @@ public void run() {
6466
/* Ignore */
6567
}
6668
}
69+
70+
LOGGER.info("Request dispatcher has shutdown");
6771
}
6872

6973
public void stop() {

core/src/main/java/com/javadiscord/jdi/core/Discord.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,16 @@ private void loadSlashCommands() {
235235

236236
public void start() {
237237
started = true;
238-
239-
this.webSocketManager =
238+
239+
webSocketManager =
240240
new WebSocketManager(
241241
new GatewaySetting().setApiVersion(10).setEncoding(GatewayEncoding.JSON),
242242
identifyRequest,
243243
cache
244244
);
245245

246246
WebSocketManagerProxy webSocketManagerProxy =
247-
new WebSocketManagerProxy(this.webSocketManager);
247+
new WebSocketManagerProxy(webSocketManager);
248248
ConnectionDetails connectionDetails =
249249
new ConnectionDetails(gateway.url(), botToken, gatewaySetting);
250250
ConnectionMediator connectionMediator =
@@ -260,10 +260,17 @@ public void stop() {
260260
if (this.webSocketManager != null) {
261261
this.webSocketManager.stop();
262262
}
263+
264+
LOGGER.info("Shutdown initiated");
265+
266+
if (webSocketManager != null) {
267+
webSocketManager.stop();
268+
}
263269

264270
discordRequestDispatcher.stop();
265271

266272
EXECUTOR.shutdown();
273+
267274
try {
268275
if (!EXECUTOR.awaitTermination(30, TimeUnit.SECONDS)) {
269276
EXECUTOR.shutdownNow();
@@ -274,8 +281,8 @@ public void stop() {
274281
);
275282
}
276283
}
277-
} catch (InterruptedException ie) {
278-
LOGGER.error("Termination was interrupted within {} seconds.", 30);
284+
} catch (InterruptedException e) {
285+
LOGGER.error("Termination was interrupted within {} seconds", 30, e);
279286
Thread.currentThread().interrupt();
280287
}
281288
}

gateway/src/main/java/com/javadiscord/jdi/internal/gateway/WebSocketManager.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.vertx.core.Vertx;
99
import io.vertx.core.buffer.Buffer;
1010
import io.vertx.core.http.WebSocket;
11+
import io.vertx.core.http.WebSocketClient;
1112
import io.vertx.core.http.WebSocketConnectOptions;
1213
import io.vertx.core.http.WebSocketFrame;
1314
import org.apache.logging.log4j.LogManager;
@@ -21,6 +22,8 @@ public class WebSocketManager {
2122
private final WebSocketRetryHandler retryHandler;
2223
private final Cache cache;
2324
private WebSocket webSocket;
25+
private WebSocketClient webSocketClient;
26+
private boolean retryAllowed;
2427

2528
public WebSocketManager(
2629
GatewaySetting gatewaySetting, IdentifyRequest identifyRequest, Cache cache
@@ -48,8 +51,9 @@ public void start(ConnectionMediator connectionMediator) {
4851
)
4952
.setSsl(true);
5053

51-
vertx.createWebSocketClient()
52-
.connect(webSocketConnectOptions)
54+
webSocketClient = vertx.createWebSocketClient();
55+
retryAllowed = true;
56+
webSocketClient.connect(webSocketConnectOptions)
5357
.onSuccess(
5458
webSocket -> {
5559
LOGGER.info("Connected to Discord");
@@ -74,7 +78,9 @@ public void start(ConnectionMediator connectionMediator) {
7478
.onFailure(
7579
error -> {
7680
LOGGER.warn("Failed to connect to {} {}", gatewayURL, error.getCause());
77-
retryHandler.retry(() -> restart(connectionMediator));
81+
if (retryAllowed) {
82+
retryHandler.retry(() -> restart(connectionMediator));
83+
}
7884
}
7985
);
8086
}
@@ -86,14 +92,22 @@ private void frameHandler(WebSocketFrame frame, WebSocketHandler webSocketHandle
8692
}
8793

8894
public void restart(ConnectionMediator connectionMediator) {
95+
retryAllowed = true;
8996
stop();
9097
start(connectionMediator);
9198
}
9299

93100
public void stop() {
94-
if (!webSocket.isClosed()) {
101+
if (webSocket != null && !webSocket.isClosed()) {
95102
webSocket.close();
96103
}
104+
webSocketClient.close()
105+
.onSuccess(res -> LOGGER.info("Web socket client has been shutdown"))
106+
.onFailure(err -> LOGGER.error("Failed to shutdown web socket client", err));
107+
retryAllowed = false;
108+
vertx.close()
109+
.onSuccess(res -> LOGGER.info("Gateway has shutdown"))
110+
.onFailure(err -> LOGGER.error("Failed to shutdown gateway", err));
97111
}
98112

99113
public WebSocket getWebSocket() {

0 commit comments

Comments
 (0)