diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-01-27 16:48:59 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-01-27 16:51:26 +0100 |
commit | 3d069ee97754f05edc14f785b084d9b6205b2576 (patch) | |
tree | 6a3c9ad847fccef476923606fb39e4aa3908c8ea /http-utils | |
parent | b0eb3902031985e6750b518f38473cd3578dee17 (diff) |
Move functional interfaces to separate files + generify
Diffstat (limited to 'http-utils')
5 files changed, 58 insertions, 30 deletions
diff --git a/http-utils/src/main/java/ai/vespa/util/http/retry/DelayedConnectionLevelRetryHandler.java b/http-utils/src/main/java/ai/vespa/util/http/retry/DelayedConnectionLevelRetryHandler.java index f26c2c18a27..dca1907c08b 100644 --- a/http-utils/src/main/java/ai/vespa/util/http/retry/DelayedConnectionLevelRetryHandler.java +++ b/http-utils/src/main/java/ai/vespa/util/http/retry/DelayedConnectionLevelRetryHandler.java @@ -10,7 +10,6 @@ import org.apache.http.protocol.HttpContext; import java.io.IOException; import java.time.Duration; import java.util.List; -import java.util.function.BiPredicate; import java.util.function.Predicate; import java.util.logging.Logger; @@ -24,32 +23,19 @@ public class DelayedConnectionLevelRetryHandler implements HttpRequestRetryHandl private static final Logger log = Logger.getLogger(HttpRequestRetryHandler.class.getName()); - @FunctionalInterface - public interface RetryConsumer { - void onRetry(IOException exception, Duration delay, int executionCount, HttpClientContext context); - } - - @FunctionalInterface - public interface RetryFailedConsumer { - void onRetryFailed(IOException exception, int executionCount, HttpClientContext context); - } - - @FunctionalInterface - public interface RetryPredicate extends BiPredicate<IOException, HttpClientContext> {} - private final DelaySupplier delaySupplier; private final int maxRetries; - private final RetryPredicate predicate; - private final RetryConsumer retryConsumer; - private final RetryFailedConsumer retryFailedConsumer; + private final RetryPredicate<IOException> predicate; + private final RetryConsumer<IOException> retryConsumer; + private final RetryFailedConsumer<IOException> retryFailedConsumer; private final Sleeper sleeper; private DelayedConnectionLevelRetryHandler( DelaySupplier delaySupplier, int maxRetries, - RetryPredicate predicate, - RetryConsumer retryConsumer, - RetryFailedConsumer retryFailedConsumer, + RetryPredicate<IOException> predicate, + RetryConsumer<IOException> retryConsumer, + RetryFailedConsumer<IOException> retryFailedConsumer, Sleeper sleeper) { this.delaySupplier = delaySupplier; this.maxRetries = maxRetries; @@ -84,9 +70,9 @@ public class DelayedConnectionLevelRetryHandler implements HttpRequestRetryHandl private final DelaySupplier delaySupplier; private final int maxRetries; - private RetryPredicate predicate = (ioException, ctx) -> true; - private RetryConsumer retryConsumer = (exception, delay, count, ctx) -> {}; - private RetryFailedConsumer retryFailedConsumer = (exception, count, ctx) -> {}; + private RetryPredicate<IOException> predicate = (ioException, ctx) -> true; + private RetryConsumer<IOException> retryConsumer = (exception, delay, count, ctx) -> {}; + private RetryFailedConsumer<IOException> retryFailedConsumer = (exception, count, ctx) -> {}; private Sleeper sleeper = new Sleeper.Default(); private Builder(DelaySupplier delaySupplier, int maxRetries) { @@ -112,17 +98,17 @@ public class DelayedConnectionLevelRetryHandler implements HttpRequestRetryHandl return this; } - public Builder retryFor(RetryPredicate predicate) { + public Builder retryFor(RetryPredicate<IOException> predicate) { this.predicate = predicate; return this; } - public Builder onRetry(RetryConsumer consumer) { + public Builder onRetry(RetryConsumer<IOException> consumer) { this.retryConsumer = consumer; return this; } - public Builder onRetryFailed(RetryFailedConsumer consumer) { + public Builder onRetryFailed(RetryFailedConsumer<IOException> consumer) { this.retryFailedConsumer = consumer; return this; } diff --git a/http-utils/src/main/java/ai/vespa/util/http/retry/RetryConsumer.java b/http-utils/src/main/java/ai/vespa/util/http/retry/RetryConsumer.java new file mode 100644 index 00000000000..494be051673 --- /dev/null +++ b/http-utils/src/main/java/ai/vespa/util/http/retry/RetryConsumer.java @@ -0,0 +1,16 @@ +// 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 org.apache.http.client.protocol.HttpClientContext; + +import java.time.Duration; + +/** + * Invoked before performing a delay and retry. + * + * @author bjorncs + */ +@FunctionalInterface +public interface RetryConsumer<T> { + void onRetry(T data, Duration delay, int executionCount, HttpClientContext context); +} diff --git a/http-utils/src/main/java/ai/vespa/util/http/retry/RetryFailedConsumer.java b/http-utils/src/main/java/ai/vespa/util/http/retry/RetryFailedConsumer.java new file mode 100644 index 00000000000..ed326ac1210 --- /dev/null +++ b/http-utils/src/main/java/ai/vespa/util/http/retry/RetryFailedConsumer.java @@ -0,0 +1,14 @@ +// 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 org.apache.http.client.protocol.HttpClientContext; + +/** + * Invoked after the last retry has failed. + * + * @author bjorncs + */ +@FunctionalInterface +public interface RetryFailedConsumer<T> { + void onRetryFailed(T response, int executionCount, HttpClientContext context); +} diff --git a/http-utils/src/main/java/ai/vespa/util/http/retry/RetryPredicate.java b/http-utils/src/main/java/ai/vespa/util/http/retry/RetryPredicate.java new file mode 100644 index 00000000000..ccf62c9be9f --- /dev/null +++ b/http-utils/src/main/java/ai/vespa/util/http/retry/RetryPredicate.java @@ -0,0 +1,13 @@ +// 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 org.apache.http.client.protocol.HttpClientContext; + +import java.util.function.BiPredicate; + +/** + * A predicate that determines whether an operation should be retried. + * + * @author bjorncs + */ +public interface RetryPredicate<T> extends BiPredicate<T, HttpClientContext> {} diff --git a/http-utils/src/test/java/ai/vespa/util/http/retry/DelayedConnectionLevelRetryHandlerTest.java b/http-utils/src/test/java/ai/vespa/util/http/retry/DelayedConnectionLevelRetryHandlerTest.java index 57a4d862b2e..85adeae6d78 100644 --- a/http-utils/src/test/java/ai/vespa/util/http/retry/DelayedConnectionLevelRetryHandlerTest.java +++ b/http-utils/src/test/java/ai/vespa/util/http/retry/DelayedConnectionLevelRetryHandlerTest.java @@ -1,8 +1,6 @@ // 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 ai.vespa.util.http.retry.DelayedConnectionLevelRetryHandler.RetryConsumer; -import ai.vespa.util.http.retry.DelayedConnectionLevelRetryHandler.RetryFailedConsumer; import com.yahoo.vespa.jdk8compat.List; import org.apache.http.client.protocol.HttpClientContext; import org.junit.Test; @@ -23,10 +21,11 @@ import static org.mockito.Mockito.verify; */ public class DelayedConnectionLevelRetryHandlerTest { + @SuppressWarnings("unchecked") @Test public void retry_consumers_are_invoked() { - RetryConsumer retryConsumer = mock(RetryConsumer.class); - RetryFailedConsumer retryFailedConsumer = mock(RetryFailedConsumer.class); + RetryConsumer<IOException> retryConsumer = (RetryConsumer<IOException>) mock(RetryConsumer.class); + RetryFailedConsumer<IOException> retryFailedConsumer = (RetryFailedConsumer<IOException>) mock(RetryFailedConsumer.class); Duration delay = Duration.ofSeconds(10); int maxRetries = 5; |