All Downloads are FREE. Search and download functionalities are using the official Maven repository.

games.mythical.saga.sdk.client.observer.AbstractObserver Maven / Gradle / Ivy

There is a newer version: 1.1.30
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy