Compare commits

..

No commits in common. "9a646b9e8d32db63dbc6b9a06d93aacca0d32936" and "7e9dbe8bd5303e6ba9fec386fa82c6c0a6f06512" have entirely different histories.

3 changed files with 34 additions and 54 deletions

View file

@ -3,7 +3,6 @@ package de.hhhammer.dchat.discord.ws;
import de.hhhammer.dchat.discord.ws.connection.BackoffRetryer; import de.hhhammer.dchat.discord.ws.connection.BackoffRetryer;
import de.hhhammer.dchat.discord.ws.connection.ConnectionManager; import de.hhhammer.dchat.discord.ws.connection.ConnectionManager;
import de.hhhammer.dchat.discord.ws.connection.Connector; import de.hhhammer.dchat.discord.ws.connection.Connector;
import de.hhhammer.dchat.discord.ws.connection.ResumeConnector;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -33,8 +32,7 @@ public final class DiscordWebSocket {
public void start() throws InterruptedException { public void start() throws InterruptedException {
final var connector = new Connector(eventHandler); final var connector = new Connector(eventHandler);
final String initGatewayUrl = "wss://gateway.discord.gg/?v=10&encoding=json"; final String initGatewayUrl = "wss://gateway.discord.gg/?v=10&encoding=json";
final ResumeConnector resumeConnector = new ResumeConnector(initGatewayUrl, discordApiKey); final var connectionManager = new ConnectionManager(initGatewayUrl, discordApiKey, retryer);
final var connectionManager = new ConnectionManager(resumeConnector, retryer);
connectionManager.start(connector); connectionManager.start(connector);
} }
} }

View file

@ -10,22 +10,49 @@ import java.time.Duration;
public final class ConnectionManager { public final class ConnectionManager {
private static final Logger logger = LoggerFactory.getLogger(ConnectionManager.class); private static final Logger logger = LoggerFactory.getLogger(ConnectionManager.class);
private final ResumeConnector resumeConnector; private final String initGatewayUrl;
private final String token;
private final Retryer retryer; private final Retryer retryer;
public ConnectionManager(final ResumeConnector resumeConnector, final Retryer retryer) { public ConnectionManager(final String initGatewayUrl, final String token, final Retryer retryer) {
this.resumeConnector = resumeConnector; this.initGatewayUrl = initGatewayUrl;
this.token = token;
this.retryer = retryer; this.retryer = retryer;
} }
public void start(final Connector connector) throws InterruptedException { public void start(final Connector connector) throws InterruptedException {
while (retryer.hasRetriesLeft()) { while (retryer.hasRetriesLeft()) {
connect(connector);
final int reconnectDelayInSeconds = retryer.nextRetryInSeconds(); final int reconnectDelayInSeconds = retryer.nextRetryInSeconds();
Thread.sleep(Duration.ofSeconds(reconnectDelayInSeconds)); Thread.sleep(Duration.ofSeconds(reconnectDelayInSeconds));
resumeConnector.connect(connector);
logger.info("Reconnecting");
} }
logger.info("No retries left");
} }
private void connect(final Connector connector) {
ConnectionConfig mutConnectionConfig = new ConnectionConfig(initGatewayUrl, new IdendificationConnectionInitiator(token));
boolean isResumable = true;
while (isResumable) {
final CloseEvent closeEvent = connector.connect(mutConnectionConfig);
isResumable = switch (closeEvent) {
case CloseEvent.ResumableCloseEvent resumableCloseEvent -> {
if (resumableCloseEvent.resumeGatewayUrl() == null) {
logger.warn("Unable to resume: Missing resume URL");
yield false;
}
mutConnectionConfig = new ConnectionConfig(
resumableCloseEvent.resumeGatewayUrl(),
new ResumeConnectionInitiator(
token,
resumableCloseEvent.sessionId(),
resumableCloseEvent.lastSequence()
)
);
yield true;
}
case CloseEvent.UnresumableCloseEvent unresumableCloseEvent -> false;
case CloseEvent.UnrecoverableCloseEvent unrecoverableCloseEvent ->
throw new IllegalStateException("Unable to establish connection to the Discord gateway");
};
}
}
} }

View file

@ -1,45 +0,0 @@
package de.hhhammer.dchat.discord.ws.connection;
import de.hhhammer.dchat.discord.ws.connection.event.CloseEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class ResumeConnector {
private static Logger logger = LoggerFactory.getLogger(ResumeConnector.class);
private final String initGatewayUrl;
private final String token;
public ResumeConnector(final String initGatewayUrl, final String token) {
this.initGatewayUrl = initGatewayUrl;
this.token = token;
}
public void connect(final Connector connector) {
ConnectionConfig mutConnectionConfig = new ConnectionConfig(initGatewayUrl, new IdendificationConnectionInitiator(token));
boolean isResumable = true;
while (isResumable) {
final CloseEvent closeEvent = connector.connect(mutConnectionConfig);
isResumable = switch (closeEvent) {
case CloseEvent.ResumableCloseEvent resumableCloseEvent -> {
if (resumableCloseEvent.resumeGatewayUrl() == null) {
logger.warn("Unable to resume: Missing resume URL");
yield false;
}
mutConnectionConfig = new ConnectionConfig(
resumableCloseEvent.resumeGatewayUrl(),
new ResumeConnectionInitiator(
token,
resumableCloseEvent.sessionId(),
resumableCloseEvent.lastSequence()
)
);
logger.info("Resuming connection");
yield true;
}
case CloseEvent.UnresumableCloseEvent unresumableCloseEvent -> false;
case CloseEvent.UnrecoverableCloseEvent unrecoverableCloseEvent ->
throw new IllegalStateException("Unable to establish connection to the Discord gateway");
};
}
}
}