Skip to content

Commit

Permalink
Fixed not reconnecting after one of multiple nodes die
Browse files Browse the repository at this point in the history
  • Loading branch information
freyacodes committed Sep 17, 2017
1 parent ed78af9 commit 383db10
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void onNodeDisconnect(LavalinkSocket disconnected) {
boolean isAffected = disconnected.equals(socket.orElse(null));

LavalinkSocket newSocket = isAffected ? determineBestSocket() : socket.orElse(null);
socketMap.put(guildId, Optional.ofNullable(newSocket)); // A bit redundant, but we need this for the setSocket call
if (isAffected)
lavalink.getPlayer(guildId).setSocket(newSocket);

Expand Down Expand Up @@ -138,14 +139,14 @@ public int getNullFramePenalty() {
}

public int getTotal() {
if (!socket.isOpen()) return Integer.MAX_VALUE - 1;
if (!socket.isAvailable()) return Integer.MAX_VALUE - 1;

return playerPenalty + cpuPenalty + deficitFramePenalty + nullFramePenalty;
}

@Override
public String toString() {
if (!socket.isOpen()) return "Penalties{" +
if (!socket.isAvailable()) return "Penalties{" +
"unavailable=" + (Integer.MAX_VALUE - 1) +
'}';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class LavalinkSocket extends ReusableWebSocket {
long lastReconnectAttempt = 0;
private int reconnectsAttempted = 0;
private final URI remoteUri;
private boolean available = false;

LavalinkSocket(Lavalink lavalink, URI serverUri, Draft protocolDraft, Map<String, String> headers) {
super(serverUri, protocolDraft, headers, TIMEOUT_MS);
Expand All @@ -73,6 +74,7 @@ public class LavalinkSocket extends ReusableWebSocket {
@Override
public void onOpen(ServerHandshake handshakeData) {
log.info("Received handshake from server");
available = true;
lavalink.loadBalancer.onNodeConnect(this);
reconnectsAttempted = 0;
}
Expand Down Expand Up @@ -196,6 +198,7 @@ private void handleEvent(JSONObject json) throws IOException {

@Override
public void onClose(int code, String reason, boolean remote) {
available = false;
reason = reason == null ? "<no reason given>" : reason;
if (code == 1000) {
log.info("Connection to " + getRemoteSocketAddress() + " closed gracefully with reason: " + reason + " :: Remote=" + remote);
Expand Down Expand Up @@ -245,6 +248,10 @@ public RemoteStats getStats() {
return stats;
}

public boolean isAvailable() {
return available && isOpen() && !isClosing();
}

@Override
public String toString() {
return "LavalinkSocket{" +
Expand Down

0 comments on commit 383db10

Please sign in to comment.