Created
October 20, 2017 15:22
-
-
Save tteggel/266c0cfd443c5e4814c3d4e58a816bef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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