Skip to content

Commit

Permalink
Added proper backoff for IDENTIFY rate limit handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Aug 18, 2017
1 parent d58bc7f commit 68b3120
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/main/java/net/dv8tion/jda/core/requests/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class WebSocketClient extends WebSocketAdapter implements WebSocketListen
{
public static final SimpleLog LOG = SimpleLog.getLog("JDASocket");
public static final int DISCORD_GATEWAY_VERSION = 6;
public static final int IDENTIFY_DELAY = 5;

protected final JDAImpl api;
protected final JDA.ShardInfo shardInfo;
Expand Down Expand Up @@ -90,6 +91,7 @@ public class WebSocketClient extends WebSocketAdapter implements WebSocketListen

protected boolean firstInit = true;
protected boolean processingReady = true;
protected boolean handleIdentifyRateLimit = false;

public WebSocketClient(JDAImpl api)
{
Expand Down Expand Up @@ -475,13 +477,24 @@ else if (closeCode != null)

protected void reconnect()
{
LOG.warn("Got disconnected from WebSocket (Internet?!)... Attempting to reconnect in " + reconnectTimeoutS + "s");
if (!handleIdentifyRateLimit)
LOG.warn("Got disconnected from WebSocket (Internet?!)... Attempting to reconnect in " + reconnectTimeoutS + "s");
while(shouldReconnect)
{
try
{
api.setStatus(JDA.Status.WAITING_TO_RECONNECT);
Thread.sleep(reconnectTimeoutS * 1000);
if (handleIdentifyRateLimit)
{
handleIdentifyRateLimit = false;
LOG.fatal("Encountered IDENTIFY (OP " + WebSocketCode.IDENTIFY + ") Rate Limit! " +
"Waiting " + IDENTIFY_DELAY + " seconds before trying again!");
Thread.sleep(IDENTIFY_DELAY * 1000);
}
else
{
Thread.sleep(reconnectTimeoutS * 1000);
}
api.setStatus(JDA.Status.ATTEMPTING_TO_RECONNECT);
}
catch(InterruptedException ignored) {}
Expand Down Expand Up @@ -531,7 +544,7 @@ public void onTextMessage(WebSocket websocket, String message)
int closeCode = isResume ? 4000 : 1000;
if (isResume)
LOG.debug("Session can be recovered... Closing and sending new RESUME request");
else // this can also mean we got rate limited in IDENTIFY
else if (!handleIdentifyRateLimit) // this can also mean we got rate limited in IDENTIFY (no need to invalidate then)
invalidate();

close(closeCode);
Expand Down Expand Up @@ -621,6 +634,7 @@ protected void sendIdentify()
.put(shardInfo.getShardTotal()));
}
send(identify.toString(), true);
handleIdentifyRateLimit = true;
sentAuthInfo = true;
}

Expand Down Expand Up @@ -791,6 +805,7 @@ protected void handleEvent(JSONObject raw)
case "READY":
//LOG.debug(String.format("%s -> %s", type, content.toString())); already logged on trace level
processingReady = true;
handleIdentifyRateLimit = false;
sessionId = content.getString("session_id");
if (!content.isNull("_trace"))
updateTraces(content.getJSONArray("_trace"), "READY", WebSocketCode.DISPATCH);
Expand Down

0 comments on commit 68b3120

Please sign in to comment.