Skip to content

Commit

Permalink
Change reconnect code to 4900 to avoid confusion (#1250)
Browse files Browse the repository at this point in the history
* Change reconnect code to 4900 to avoid confusion
* And update the disconnect handling to better log things
* Make resume reconnect log on debug
* Log server error on error level
* Add catch in gateway worker
  • Loading branch information
MinnDevelopment committed Apr 6, 2020
1 parent f1b31f7 commit 0eb54b3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/dv8tion/jda/api/requests/CloseCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
public enum CloseCode
{
RECONNECT( 4900, "The connection has been closed to reconnect."),
GRACEFUL_CLOSE( 1000, "The connection was closed gracefully or your heartbeats timed out."),
CLOUD_FLARE_LOAD( 1001, "The connection was closed due to CloudFlare load balancing."),
INTERNAL_SERVER_ERROR(1006, "Something broke on the remote's end, sorry 'bout that... Try reconnecting!"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void ready()
}
else
{
JDAImpl.LOG.info("Successfully resumed Session!");
JDAImpl.LOG.debug("Successfully resumed Session!");
api.handleEvent(new ResumedEvent(api, api.getResponseTotal()));
}
api.setStatus(JDA.Status.CONNECTED);
Expand Down Expand Up @@ -389,7 +389,7 @@ public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame,
api.setStatus(JDA.Status.DISCONNECTED);

CloseCode closeCode = null;
int rawCloseCode = 1000;
int rawCloseCode = 1005;
//When we get 1000 from remote close we will try to resume
// as apparently discord doesn't understand what "graceful disconnect" means
boolean isInvalidate = false;
Expand All @@ -399,27 +399,31 @@ public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame,
keepAliveThread.cancel(false);
keepAliveThread = null;
}
if (serverCloseFrame != null)
if (closedByServer && serverCloseFrame != null)
{
rawCloseCode = serverCloseFrame.getCloseCode();
String rawCloseReason = serverCloseFrame.getCloseReason();
closeCode = CloseCode.from(rawCloseCode);
if (closeCode == CloseCode.RATE_LIMITED)
LOG.error("WebSocket connection closed due to ratelimit! Sent more than 120 websocket messages in under 60 seconds!");
else if (closeCode == CloseCode.UNKNOWN_ERROR)
LOG.error("WebSocket connection closed due to server error! {}: {}", rawCloseCode, rawCloseReason);
else if (closeCode != null)
LOG.debug("WebSocket connection closed with code {}", closeCode);
else if (rawCloseReason != null)
LOG.warn("WebSocket connection closed with code {}: {}", rawCloseCode, rawCloseReason);
else
LOG.warn("WebSocket connection closed with unknown meaning for close-code {}", rawCloseCode);
}
if (clientCloseFrame != null
&& clientCloseFrame.getCloseCode() == 1000
&& Objects.equals(clientCloseFrame.getCloseReason(), INVALIDATE_REASON))
else if (clientCloseFrame != null)
{
//When we close with 1000 we properly dropped our session due to invalidation
// in that case we can be sure that resume will not work and instead we invalidate and reconnect here
isInvalidate = true;
rawCloseCode = clientCloseFrame.getCloseCode();
if (rawCloseCode == 1000 && INVALIDATE_REASON.equals(clientCloseFrame.getCloseReason()))
{
//When we close with 1000 we properly dropped our session due to invalidation
// in that case we can be sure that resume will not work and instead we invalidate and reconnect here
isInvalidate = true;
}
}

// null is considered -reconnectable- as we do not know the close-code meaning
Expand Down Expand Up @@ -492,7 +496,7 @@ private void handleReconnect(int code) throws InterruptedException
}
else // if resume is possible
{
LOG.warn("Got disconnected from WebSocket (Code: {}). Attempting to resume session", code);
LOG.debug("Got disconnected from WebSocket (Code: {}). Attempting to resume session", code);
reconnect();
}
}
Expand Down Expand Up @@ -770,7 +774,7 @@ protected void onEvent(DataObject content)
break;
case WebSocketCode.RECONNECT:
LOG.debug("Got Reconnect request (OP 7). Closing connection now...");
close(4000, "OP 7: RECONNECT");
close(4900, "OP 7: RECONNECT");
break;
case WebSocketCode.INVALIDATE_SESSION:
LOG.debug("Got Invalidate request (OP 9). Invalidating...");
Expand All @@ -780,7 +784,7 @@ protected void onEvent(DataObject content)
final boolean isResume = content.getBoolean("d");
// When d: true we can wait a bit and then try to resume again
//sending 4000 to not drop session
int closeCode = isResume ? 4000 : 1000;
int closeCode = isResume ? 4900 : 1000;
if (isResume)
LOG.debug("Session can be recovered... Closing and sending new RESUME request");
else
Expand Down Expand Up @@ -929,7 +933,7 @@ protected DataObject handleBinary(byte[] binary) throws DataFormatException
}
catch (DataFormatException e)
{
close(4000, "MALFORMED_PACKAGE");
close(4900, "MALFORMED_PACKAGE");
throw e;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ else if (!attemptedToSend)
{
LOG.debug("Main WS send thread interrupted. Most likely JDA is disconnecting the websocket.");
}
catch (Throwable ex)
{
// Log error
LOG.error("Encountered error in gateway worker", ex);
if (ex instanceof RuntimeException)
throw (RuntimeException) ex;
else
throw (Error) ex;
}
finally
{
// on any exception that might cause this lock to not release
Expand Down

0 comments on commit 0eb54b3

Please sign in to comment.