From df60fd5c2dfbf7e87116de719b959cfb7f872139 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Tue, 14 Jan 2014 10:37:49 +0100 Subject: [PATCH] Fixed release of connections when the exchange is terminated. --- .../fcgi/client/http/HttpChannelOverFCGI.java | 12 +++++++++ .../client/http/HttpConnectionOverFCGI.java | 27 +++++++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/fcgi-http-client-transport/src/main/java/org/eclipse/jetty/fcgi/client/http/HttpChannelOverFCGI.java b/fcgi-http-client-transport/src/main/java/org/eclipse/jetty/fcgi/client/http/HttpChannelOverFCGI.java index 0befe6e9acd..cda158c3c39 100644 --- a/fcgi-http-client-transport/src/main/java/org/eclipse/jetty/fcgi/client/http/HttpChannelOverFCGI.java +++ b/fcgi-http-client-transport/src/main/java/org/eclipse/jetty/fcgi/client/http/HttpChannelOverFCGI.java @@ -27,11 +27,15 @@ import org.eclipse.jetty.client.api.Result; import org.eclipse.jetty.fcgi.generator.Flusher; import org.eclipse.jetty.fcgi.generator.Generator; import org.eclipse.jetty.http.HttpField; +import org.eclipse.jetty.http.HttpFields; +import org.eclipse.jetty.http.HttpHeader; +import org.eclipse.jetty.http.HttpHeaderValue; import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.io.IdleTimeout; public class HttpChannelOverFCGI extends HttpChannel { + private final HttpConnectionOverFCGI connection; private final Flusher flusher; private final int request; private final HttpSenderOverFCGI sender; @@ -42,6 +46,7 @@ public class HttpChannelOverFCGI extends HttpChannel public HttpChannelOverFCGI(final HttpConnectionOverFCGI connection, Flusher flusher, int request, long idleTimeout) { super(connection.getHttpDestination()); + this.connection = connection; this.flusher = flusher; this.request = request; this.sender = new HttpSenderOverFCGI(this); @@ -121,6 +126,13 @@ public class HttpChannelOverFCGI extends HttpChannel { super.exchangeTerminated(result); idle.onClose(); + boolean close = result.isFailed(); + HttpFields responseHeaders = result.getResponse().getHeaders(); + close |= responseHeaders.contains(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()); + if (close) + connection.close(); + else + connection.release(); } protected void flush(Generator.Result... results) diff --git a/fcgi-http-client-transport/src/main/java/org/eclipse/jetty/fcgi/client/http/HttpConnectionOverFCGI.java b/fcgi-http-client-transport/src/main/java/org/eclipse/jetty/fcgi/client/http/HttpConnectionOverFCGI.java index f0f800ba548..85fe70fab42 100644 --- a/fcgi-http-client-transport/src/main/java/org/eclipse/jetty/fcgi/client/http/HttpConnectionOverFCGI.java +++ b/fcgi-http-client-transport/src/main/java/org/eclipse/jetty/fcgi/client/http/HttpConnectionOverFCGI.java @@ -24,11 +24,13 @@ import java.util.LinkedList; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicBoolean; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.HttpConnection; import org.eclipse.jetty.client.HttpDestination; import org.eclipse.jetty.client.HttpExchange; +import org.eclipse.jetty.client.PoolingHttpDestination; import org.eclipse.jetty.client.api.Connection; import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.client.api.Response; @@ -49,6 +51,7 @@ public class HttpConnectionOverFCGI extends AbstractConnection implements Connec private final LinkedList requests = new LinkedList<>(); private final Map channels = new ConcurrentHashMap<>(); + private final AtomicBoolean closed = new AtomicBoolean(); private final Flusher flusher; private final HttpDestination destination; private final Delegate delegate; @@ -153,14 +156,28 @@ public class HttpConnectionOverFCGI extends AbstractConnection implements Connec return false; } + public void release() + { + if (destination instanceof PoolingHttpDestination) + { + @SuppressWarnings("unchecked") + PoolingHttpDestination fcgiDestination = + (PoolingHttpDestination)destination; + fcgiDestination.release(this); + } + } + @Override public void close() { - getHttpDestination().close(this); - getEndPoint().shutdownOutput(); - LOG.debug("{} oshut", this); - getEndPoint().close(); - LOG.debug("{} closed", this); + if (closed.compareAndSet(false, true)) + { + getHttpDestination().close(this); + getEndPoint().shutdownOutput(); + LOG.debug("{} oshut", this); + getEndPoint().close(); + LOG.debug("{} closed", this); + } } private int acquireRequest()