aboutsummaryrefslogtreecommitdiffstats
path: root/http-utils
diff options
context:
space:
mode:
authorBjørn Christian Seime <bjorncs@verizonmedia.com>2020-01-24 16:01:15 +0100
committerBjørn Christian Seime <bjorncs@verizonmedia.com>2020-01-27 16:51:26 +0100
commit2016d352e512d446d16b6f837288f69025afc661 (patch)
tree4c56e6006055eda1a515b3931b6ca8afba451d0b /http-utils
parent39a54ddf952cdaf898f2a2b6011a415610d08ffd (diff)
Move Sleeper and DelaySupplier to separate files
Diffstat (limited to 'http-utils')
-rw-r--r--http-utils/src/main/java/ai/vespa/util/http/retry/DelaySupplier.java44
-rw-r--r--http-utils/src/main/java/ai/vespa/util/http/retry/DelayedHttpRequestRetryHandler.java35
-rw-r--r--http-utils/src/main/java/ai/vespa/util/http/retry/Sleeper.java26
-rw-r--r--http-utils/src/test/java/ai/vespa/util/http/retry/DelayedHttpRequestRetryHandlerTest.java1
4 files changed, 73 insertions, 33 deletions
diff --git a/http-utils/src/main/java/ai/vespa/util/http/retry/DelaySupplier.java b/http-utils/src/main/java/ai/vespa/util/http/retry/DelaySupplier.java
new file mode 100644
index 00000000000..a97024df95c
--- /dev/null
+++ b/http-utils/src/main/java/ai/vespa/util/http/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.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;
+ }
+ }
+}
diff --git a/http-utils/src/main/java/ai/vespa/util/http/retry/DelayedHttpRequestRetryHandler.java b/http-utils/src/main/java/ai/vespa/util/http/retry/DelayedHttpRequestRetryHandler.java
index 72bb171c4c7..2627ace3c53 100644
--- a/http-utils/src/main/java/ai/vespa/util/http/retry/DelayedHttpRequestRetryHandler.java
+++ b/http-utils/src/main/java/ai/vespa/util/http/retry/DelayedHttpRequestRetryHandler.java
@@ -87,7 +87,7 @@ public class DelayedHttpRequestRetryHandler implements HttpRequestRetryHandler {
private RetryPredicate predicate = (ioException, ctx) -> true;
private RetryConsumer retryConsumer = (exception, delay, count, ctx) -> {};
private RetryFailedConsumer retryFailedConsumer = (exception, count, ctx) -> {};
- private Sleeper sleeper = new DefaultSleeper();
+ private Sleeper sleeper = new Sleeper.Default();
private Builder(DelaySupplier delaySupplier, int maxRetries) {
this.delaySupplier = delaySupplier;
@@ -95,19 +95,11 @@ public class DelayedHttpRequestRetryHandler implements HttpRequestRetryHandler {
}
public static Builder withFixedDelay(Duration delay, int maxRetries) {
- return new Builder(executionCount -> delay, maxRetries);
+ return new Builder(new DelaySupplier.Fixed(delay), maxRetries);
}
public static Builder withExponentialBackoff(Duration startDelay, Duration maxDelay, int maxRetries) {
- return new Builder(
- executionCount -> {
- Duration nextDelay = startDelay;
- for (int i = 1; i < executionCount; ++i) {
- nextDelay = nextDelay.multipliedBy(2);
- }
- return maxDelay.compareTo(nextDelay) > 0 ? nextDelay : maxDelay;
- },
- maxRetries);
+ return new Builder(new DelaySupplier.Exponential(startDelay, maxDelay), maxRetries);
}
public Builder retryForExceptions(List<Class<? extends IOException>> exceptionTypes) {
@@ -144,26 +136,5 @@ public class DelayedHttpRequestRetryHandler implements HttpRequestRetryHandler {
public DelayedHttpRequestRetryHandler build() {
return new DelayedHttpRequestRetryHandler(delaySupplier, maxRetries, predicate, retryConsumer, retryFailedConsumer, sleeper);
}
-
- private static class DefaultSleeper implements Sleeper {
- @Override
- public void sleep(Duration duration) {
- try {
- Thread.sleep(duration.toMillis());
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
- }
- }
-
- // For unit testing
- interface Sleeper {
- void sleep(Duration duration);
- }
-
- @FunctionalInterface
- private interface DelaySupplier {
- Duration getDelay(int executionCount);
}
}
diff --git a/http-utils/src/main/java/ai/vespa/util/http/retry/Sleeper.java b/http-utils/src/main/java/ai/vespa/util/http/retry/Sleeper.java
new file mode 100644
index 00000000000..06a7359f307
--- /dev/null
+++ b/http-utils/src/main/java/ai/vespa/util/http/retry/Sleeper.java
@@ -0,0 +1,26 @@
+// 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.retry;
+
+import java.time.Duration;
+
+/**
+ * An abstraction used for mocking {@link Thread#sleep(long)} in unit tests.
+ *
+ * @author bjorncs
+ */
+interface Sleeper {
+ void sleep(Duration duration);
+
+ class Default implements Sleeper {
+ @Override
+ public void sleep(Duration duration) {
+ try {
+ Thread.sleep(duration.toMillis());
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new RuntimeException(e);
+ }
+ }
+ }
+}
+
diff --git a/http-utils/src/test/java/ai/vespa/util/http/retry/DelayedHttpRequestRetryHandlerTest.java b/http-utils/src/test/java/ai/vespa/util/http/retry/DelayedHttpRequestRetryHandlerTest.java
index 51a05f6b2a7..487a16b0657 100644
--- a/http-utils/src/test/java/ai/vespa/util/http/retry/DelayedHttpRequestRetryHandlerTest.java
+++ b/http-utils/src/test/java/ai/vespa/util/http/retry/DelayedHttpRequestRetryHandlerTest.java
@@ -3,7 +3,6 @@ package ai.vespa.util.http.retry;
import ai.vespa.util.http.retry.DelayedHttpRequestRetryHandler.RetryConsumer;
import ai.vespa.util.http.retry.DelayedHttpRequestRetryHandler.RetryFailedConsumer;
-import ai.vespa.util.http.retry.DelayedHttpRequestRetryHandler.Sleeper;
import com.yahoo.vespa.jdk8compat.List;
import org.apache.http.client.protocol.HttpClientContext;
import org.junit.Test;