diff options
author | Jon Marius Venstad <venstad@gmail.com> | 2021-06-02 08:02:51 +0200 |
---|---|---|
committer | Jon Marius Venstad <venstad@gmail.com> | 2021-06-02 08:02:51 +0200 |
commit | 1f830e7b3e58746fb9b16b0d0cfc21d9155e737e (patch) | |
tree | 5b18e696c1ea527087a44462e6045ab933f6afcd /vespa-feed-client/src | |
parent | 951b86bbe849fdf81aa25a917d4157c9cf52d173 (diff) |
Use a Clock and set start time to now()
Diffstat (limited to 'vespa-feed-client/src')
3 files changed, 12 insertions, 5 deletions
diff --git a/vespa-feed-client/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java b/vespa-feed-client/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java index 3cd3f3cb4ca..eaf84c67ac4 100644 --- a/vespa-feed-client/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java +++ b/vespa-feed-client/src/main/java/ai/vespa/feed/client/FeedClientBuilder.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.io.UncheckedIOException; import java.net.URI; import java.nio.file.Path; +import java.time.Clock; import java.util.HashMap; import java.util.Map; import java.util.function.Supplier; @@ -33,6 +34,7 @@ public class FeedClientBuilder { Path certificate; Path privateKey; Path caCertificates; + Clock clock; public static FeedClientBuilder create(URI endpoint) { return new FeedClientBuilder(endpoint); } diff --git a/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpFeedClient.java b/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpFeedClient.java index b3c711f7cc5..8a38e859ca4 100644 --- a/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpFeedClient.java +++ b/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpFeedClient.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.PrintStream; import java.net.URI; import java.net.URISyntaxException; +import java.time.Clock; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -53,7 +54,7 @@ class HttpFeedClient implements FeedClient { HttpFeedClient(FeedClientBuilder builder) throws IOException { this.endpoint = builder.endpoint; this.requestHeaders = new HashMap<>(builder.requestHeaders); - this.requestStrategy = new HttpRequestStrategy(builder); + this.requestStrategy = new HttpRequestStrategy(builder, Clock.systemUTC()); for (int i = 0; i < builder.maxConnections; i++) { CloseableHttpAsyncClient client = createHttpClient(builder); diff --git a/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpRequestStrategy.java b/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpRequestStrategy.java index d43edc9656b..53b2fd56f20 100644 --- a/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpRequestStrategy.java +++ b/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpRequestStrategy.java @@ -6,6 +6,7 @@ import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; import java.io.IOException; +import java.time.Clock; import java.time.Instant; import java.util.HashMap; import java.util.Map; @@ -34,6 +35,7 @@ class HttpRequestStrategy implements RequestStrategy { private final Map<DocumentId, CompletableFuture<Void>> inflightById = new HashMap<>(); private final Object lock = new Object(); + private final Clock clock; private final RetryStrategy wrapped; private final long maxInflight; private final long minInflight; @@ -41,13 +43,15 @@ class HttpRequestStrategy implements RequestStrategy { private long inflight = 0; private long consecutiveSuccesses = 0; private boolean failed = false; - private Instant lastSuccess = Instant.MAX; + private Instant lastSuccess; - HttpRequestStrategy(FeedClientBuilder builder) { + HttpRequestStrategy(FeedClientBuilder builder, Clock clock) { this.wrapped = builder.retryStrategy; this.maxInflight = builder.maxConnections * (long) builder.maxStreamsPerConnection; this.minInflight = builder.maxConnections * (long) min(16, builder.maxStreamsPerConnection); this.targetInflight = Math.sqrt(maxInflight) * (Math.sqrt(minInflight)); + this.clock = clock; + lastSuccess = clock.instant(); } private boolean retry(SimpleHttpRequest request, int attempt) { @@ -77,7 +81,7 @@ class HttpRequestStrategy implements RequestStrategy { } void success() { - Instant now = Instant.now(); + Instant now = clock.instant(); synchronized (lock) { ++consecutiveSuccesses; lastSuccess = now; @@ -86,7 +90,7 @@ class HttpRequestStrategy implements RequestStrategy { } void failure() { - Instant threshold = Instant.now().minusSeconds(300); + Instant threshold = clock.instant().minusSeconds(300); synchronized (lock) { consecutiveSuccesses = 0; if (lastSuccess.isBefore(threshold)) |