diff options
Diffstat (limited to 'http-utils/src/main/java/ai/vespa/util/http/hc4/retry/DelaySupplier.java')
-rw-r--r-- | http-utils/src/main/java/ai/vespa/util/http/hc4/retry/DelaySupplier.java | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/http-utils/src/main/java/ai/vespa/util/http/hc4/retry/DelaySupplier.java b/http-utils/src/main/java/ai/vespa/util/http/hc4/retry/DelaySupplier.java new file mode 100644 index 00000000000..b202966c412 --- /dev/null +++ b/http-utils/src/main/java/ai/vespa/util/http/hc4/retry/DelaySupplier.java @@ -0,0 +1,44 @@ +// Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.util.http.hc4.retry; + +import java.time.Duration; + +/** + * An abstraction that calculates the next delay based on the current retry count. + * + * @author bjorncs + */ +@FunctionalInterface +interface DelaySupplier { + Duration getDelay(int executionCount); + + class Fixed implements DelaySupplier { + private final Duration delay; + + Fixed(Duration delay) { + this.delay = delay; + } + + @Override + public Duration getDelay(int executionCount) { return delay; } + } + + class Exponential implements DelaySupplier { + private final Duration startDelay; + private final Duration maxDelay; + + Exponential(Duration startDelay, Duration maxDelay) { + this.startDelay = startDelay; + this.maxDelay = maxDelay; + } + + @Override + public Duration getDelay(int executionCount) { + Duration nextDelay = startDelay; + for (int i = 1; i < executionCount; ++i) { + nextDelay = nextDelay.multipliedBy(2); + } + return maxDelay.compareTo(nextDelay) > 0 ? nextDelay : maxDelay; + } + } +} |