Skip to content

Commit

Permalink
🐛 Int.MAX retries causes uncontrolled retry attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGraversen committed Feb 1, 2020
1 parent 28c7967 commit d06db7f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ public Duration getTimeBetweenRetries() {
public Duration getConnectionWatcherInterval() {
return connectionWatcherInterval;
}

@Override
public String toString() {
return "ConnectOptions{" +
"maxRetries=" + maxRetries +
", timeBetweenRetries=" + timeBetweenRetries +
", connectionWatcherInterval=" + connectionWatcherInterval +
'}';
}
}
16 changes: 11 additions & 5 deletions src/main/java/io/graversen/minecraft/rcon/service/ConnectTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,27 @@ class ConnectTask implements Callable<MinecraftClient> {
ConnectTask(ConnectOptions connectOptions, RconDetails rconDetails) {
this.connectOptions = connectOptions;
this.rconDetails = rconDetails;
Logger.debug(connectOptions);
Logger.debug(rconDetails);
}

@Override
public MinecraftClient call() throws Exception {
int currentAttempt = 1;
int currentAttempt = 0;

while (currentAttempt <= connectOptions.getMaxRetries() && !Thread.currentThread().isInterrupted()) {
Logger.debug("Connection attempt {}", currentAttempt);
while (currentAttempt < connectOptions.getMaxRetries() && !Thread.currentThread().isInterrupted()) {
currentAttempt++;
Logger.debug("Connection attempt {}", currentAttempt);

try {
return MinecraftClient.connect(rconDetails.getHostname(), rconDetails.getPassword(), rconDetails.getPort());
} catch (Exception e) {
Logger.debug("Connection attempt failed due to: {}", e.getMessage());

if (currentAttempt < connectOptions.getMaxRetries() + 1) {
} finally {
if (currentAttempt < connectOptions.getMaxRetries()) {
sleep();
} else {
Logger.debug("Ran out of retries after {} total attempts", currentAttempt);
}
}
}
Expand All @@ -39,8 +43,10 @@ public MinecraftClient call() throws Exception {

private void sleep() {
try {
Logger.debug("Pausing for {} ms", connectOptions.getTimeBetweenRetries().toMillis());
Thread.sleep(connectOptions.getTimeBetweenRetries().toMillis());
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ public int getPort() {
public String getPassword() {
return password;
}

@Override
public String toString() {
return "RconDetails{" +
"hostname='" + hostname + '\'' +
", port=" + port +
", password='" + "******" + '\'' +
'}';
}
}

0 comments on commit d06db7f

Please sign in to comment.