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

syncloud.google.docs.ExponentialBackoff Maven / Gradle / Ivy

The newest version!
package syncloud.google.docs;

import syncloud.core.log.Logger;
import syncloud.storage.StorageException;

import java.io.IOException;
import java.util.Random;

public abstract class ExponentialBackoff {
    private static Logger logger = Logger.getLogger(ExponentialBackoff.class);
    public static final String MESSAGE = "There has been en error, the request never succeeded.";
    private int delay = 1000;

    public T run() throws IOException {
        for (int i = 0; i <= 5; i++) {

            try {

                return process();

            } catch (NotReady e) {

                logger.info("retry " + (i + 1));
                try {
                    Thread.sleep(calculateDelay(i, delay));
                } catch (InterruptedException ignore) {
                }
            }

        }
        logger.error(MESSAGE);
        throw new StorageException(MESSAGE);
    }

    private long calculateDelay(int cycle, int delay) {
        return (long) Math.pow(2, cycle) * delay + (long) (new Random()).nextInt(delay);
    }

    public void setDelay(int delay) {
        this.delay = delay;
    }

    public abstract T process() throws NotReady, IOException;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy