syncloud.google.docs.ExponentialBackoff Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of syncloud-google-docs Show documentation
Show all versions of syncloud-google-docs Show documentation
Google Docs Syncloud Starage Adapter
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