games.mythical.saga.sdk.client.observer.AbstractObserver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of saga-sdk-java Show documentation
Show all versions of saga-sdk-java Show documentation
Saga SDK for Java game servers
package games.mythical.saga.sdk.client.observer;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import java.util.concurrent.TimeUnit;
@Slf4j
public abstract class AbstractObserver implements StreamObserver {
private final static int MAX_COUNT = 16;
// retry settings
private boolean retry = false;
private int requestCount = 1;
protected void sleepBetweenReconnects() {
if (!retry) {
retry = true;
return;
}
var sleepTimeMillis = (int) Math.pow(2, requestCount) + RandomUtils.nextInt(1, 1000);
try {
log.trace("Sleeping {} milliseconds before reconnect", sleepTimeMillis);
TimeUnit.MILLISECONDS.sleep(sleepTimeMillis);
if (requestCount < MAX_COUNT) {
requestCount++;
}
} catch (InterruptedException e) {
log.error("Retry interrupted, exiting...");
Thread.currentThread().interrupt();
}
}
protected void resetConnectionRetry() {
retry = false;
requestCount = 1;
}
}