Skip to content

Commit

Permalink
fix(gateway / status rotor): fixed issues with the new gateway implem…
Browse files Browse the repository at this point in the history
…entation where having an active status rotor would cause the gateway to crash after a disconnect and attempted reconnect

This was due to the status rotor attempting to send a message when it wasn't authorized to do so, since the ready event hadn't yet been received.
  • Loading branch information
seailz committed Sep 26, 2023
1 parent f217d41 commit 1ac4c53
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/com/seailz/discordjar/DiscordJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ public void setStatus(@NotNull Status status) {
JSONObject json = new JSONObject();
json.put("d", status.compile());
json.put("op", 3);
gatewayFactory.queueMessage(json);
gatewayFactory.queueMessageUntilReady(json);
gatewayFactory.setStatus(status);
this.status = status;
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/seailz/discordjar/gateway/Gateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class Gateway {
private ReconnectInfo resumeInfo;
public static long lastSequenceNumber = -1;
private boolean readyForMessages = false;
private boolean receivedReady = false;
private HeartLogic heartbeatManager;
public static Date lastHeartbeatSent = new Date();
public static List<Long> pingHistoryMs = new ArrayList<>();
Expand Down Expand Up @@ -118,6 +119,7 @@ public void disconnect(@NotNull CloseStatus closeStatus) {
* @param closeStatus The close status of the disconnect.
*/
public void disconnectFlow(@NotNull CloseStatus closeStatus) {
setReceivedReady(false);
heartbeatManager.stop(); // Stop attempting heartbeats to avoid broken pipe errors
CloseCode closeCode = CloseCode.fromCode(closeStatus.getCode());
readyForMessages = false;
Expand Down Expand Up @@ -403,6 +405,22 @@ public void queueMessage(@NotNull JSONObject payload) {
}
}

/**
* Queues a message to be sent to the gateway.
* <br>Messages will be sent after the READY event is received.
* @param payload {@link JSONObject} containing the payload to send
*/
public void queueMessageUntilReady(@NotNull JSONObject payload) {
if (bot.isDebug()) logger.info("[Gateway] Queued message: " + payload);
while (receivedReady) {
socket.send(payload.toString());
if (bot.isDebug()) {
logger.info("[Gateway] Sent message: " + payload);
}
break;
}
}

/**
* Sends a request to the gateway to request guild members.
* @param action {@link RequestGuildMembersAction} containing extra information about the request
Expand Down Expand Up @@ -506,6 +524,10 @@ private String getGatewayUrl() throws InterruptedException {
return gatewayUrl;
}

public void setReceivedReady(boolean receivedReady) {
this.receivedReady = receivedReady;
}

/**
* Returns resume info for the next resume attempt, or null if READY was not received yet.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public enum DispatchedEvents {
READY((p, g, d) -> {
Logger.getLogger("Gateway")
.info("[Gateway] Ready to receive events");
g.setReceivedReady(true);
return ReadyEvent.class;
}),
RESUMED((p, d, g) -> GatewayResumedEvent.class),
Expand Down

0 comments on commit 1ac4c53

Please sign in to comment.