Skip to content

Instantly share code, notes, and snippets.

@tteggel
Created October 20, 2017 15:22
Show Gist options
  • Save tteggel/266c0cfd443c5e4814c3d4e58a816bef to your computer and use it in GitHub Desktop.
Save tteggel/266c0cfd443c5e4814c3d4e58a816bef to your computer and use it in GitHub Desktop.
private static <T> FlowFuture<T> _exponentialWithJitter(Flows.SerCallable<FlowFuture<T>> op, RetrySettings settings, int attempt) {
Flow f = Flows.currentFlow();
try {
FlowFuture<T> future = op.call();
return future.exceptionallyCompose((e) -> {
if (attempt < settings.maxAttempts) {
long delay_max = (long) Math.min(
settings.timeUnit.toMillis(settings.delayMaxDuration),
settings.timeUnit.toMillis(settings.delayBaseDuration) * Math.pow(2, attempt));
long delay = new Random().longs(1, 0, delay_max).findFirst().getAsLong();
return f.delay(delay, TimeUnit.MILLISECONDS)
.thenCompose((a) -> _exponentialWithJitter(op, settings, attempt + 1));
} else {
return f.failedFuture(new RuntimeException());
}
});
} catch (Exception ex) {
return f.failedFuture(new RuntimeException());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment