Use a dedicated thread for result handling for feed client (parsing etc.)
@@ -12,6 +12,8 @@ import java.util.Queue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Logger;
@@ -51,6 +53,11 @@ class HttpRequestStrategy implements RequestStrategy {
private final AtomicBoolean destroyed = new AtomicBoolean(false);
private final AtomicLong delayedCount = new AtomicLong(0);
private final AtomicLong retries = new AtomicLong(0);
+ private final ExecutorService resultExecutor = Executors.newSingleThreadExecutor(runnable -> {
+ Thread thread = new Thread(runnable, "feed-client-result-executor");
+ thread.setDaemon(true);
+ return thread;
+ });
HttpRequestStrategy(FeedClientBuilder builder) throws IOException {
this(builder, new HttpCluster(builder));
@@ -222,7 +229,7 @@ class HttpRequestStrategy implements RequestStrategy {
/** Handles the result of one attempt at the given operation, retrying if necessary. */
private void handleAttempt(CompletableFuture<SimpleHttpResponse> vessel, SimpleHttpRequest request, CompletableFuture<SimpleHttpResponse> result, int attempt) {
- vessel.whenComplete((response, thrown) -> {
+ vessel.whenCompleteAsync((response, thrown) -> {
// Retry the operation if it failed with a transient error ...
if (thrown != null ? retry(request, thrown, attempt)
: retry(request, response, attempt)) {
@@ -237,7 +244,8 @@ class HttpRequestStrategy implements RequestStrategy {
if (thrown == null) result.complete(response);
else result.completeExceptionally(thrown);
}
- });
+ },
+ resultExecutor);
}
@Override
@@ -246,6 +254,7 @@ class HttpRequestStrategy implements RequestStrategy {
inflightById.values().forEach(result -> result.cancel(true));
cluster.close();
+ resultExecutor.shutdown();
}
}
