From 000dd62318712e2327eaa68fea6205c0e86a5bec Mon Sep 17 00:00:00 2001 From: Daniel Mitterdorfer Date: Mon, 9 May 2016 11:11:59 +0200 Subject: [PATCH] Determine content length eagerly in HttpServer With this commit we eagerly evaluate content length in HttpServer and also pass the same value to ResourceHandlingHttpChannel. With this change it easier to reason about the content length that is freed leaving no doubt that it must be identical to the reserved amount. --- .../main/java/org/elasticsearch/http/HttpServer.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/http/HttpServer.java b/core/src/main/java/org/elasticsearch/http/HttpServer.java index 45abad0fb81..33a1c1304f9 100644 --- a/core/src/main/java/org/elasticsearch/http/HttpServer.java +++ b/core/src/main/java/org/elasticsearch/http/HttpServer.java @@ -106,9 +106,10 @@ public class HttpServer extends AbstractLifecycleComponent implement } RestChannel responseChannel = channel; try { - inFlightRequestsBreaker(circuitBreakerService).addEstimateBytesAndMaybeBreak(request.content().length(), ""); + int contentLength = request.content().length(); + inFlightRequestsBreaker(circuitBreakerService).addEstimateBytesAndMaybeBreak(contentLength, ""); // iff we could reserve bytes for the request we need to send the response also over this channel - responseChannel = new ResourceHandlingHttpChannel(channel, circuitBreakerService); + responseChannel = new ResourceHandlingHttpChannel(channel, circuitBreakerService, contentLength); restController.dispatchRequest(request, responseChannel, threadContext); } catch (Throwable t) { restController.sendErrorResponse(request, responseChannel, t); @@ -135,11 +136,13 @@ public class HttpServer extends AbstractLifecycleComponent implement private static final class ResourceHandlingHttpChannel implements RestChannel { private final RestChannel delegate; private final CircuitBreakerService circuitBreakerService; + private final int contentLength; private final AtomicBoolean closed = new AtomicBoolean(); - public ResourceHandlingHttpChannel(RestChannel delegate, CircuitBreakerService circuitBreakerService) { + public ResourceHandlingHttpChannel(RestChannel delegate, CircuitBreakerService circuitBreakerService, int contentLength) { this.delegate = delegate; this.circuitBreakerService = circuitBreakerService; + this.contentLength = contentLength; } @Override @@ -183,7 +186,7 @@ public class HttpServer extends AbstractLifecycleComponent implement if (closed.compareAndSet(false, true) == false) { throw new IllegalStateException("Channel is already closed"); } - inFlightRequestsBreaker(circuitBreakerService).addWithoutBreaking(-request().content().length()); + inFlightRequestsBreaker(circuitBreakerService).addWithoutBreaking(-contentLength); } }