hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject httpcomponents-core git commit: HTTPCORE-475: Elemental Reverse Proxy example to handle stale outgoing connections more gracefully
Date Mon, 03 Jul 2017 17:19:20 GMT
Repository: httpcomponents-core
Updated Branches:
  refs/heads/4.4.x 219b55b07 -> 08339dcca


HTTPCORE-475: Elemental Reverse Proxy example to handle stale outgoing connections more gracefully


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/08339dcc
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/08339dcc
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/08339dcc

Branch: refs/heads/4.4.x
Commit: 08339dcca2696da5176e89b4da6d9653886b151c
Parents: 219b55b
Author: Oleg Kalnichevski <olegk@apache.org>
Authored: Mon Jul 3 19:07:24 2017 +0200
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Mon Jul 3 19:07:24 2017 +0200

----------------------------------------------------------------------
 .../http/examples/ElementalReverseProxy.java    | 21 ++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/08339dcc/httpcore/src/examples/org/apache/http/examples/ElementalReverseProxy.java
----------------------------------------------------------------------
diff --git a/httpcore/src/examples/org/apache/http/examples/ElementalReverseProxy.java b/httpcore/src/examples/org/apache/http/examples/ElementalReverseProxy.java
index 5c7e899..014817d 100644
--- a/httpcore/src/examples/org/apache/http/examples/ElementalReverseProxy.java
+++ b/httpcore/src/examples/org/apache/http/examples/ElementalReverseProxy.java
@@ -34,12 +34,10 @@ import java.net.Socket;
 
 import org.apache.http.ConnectionClosedException;
 import org.apache.http.ConnectionReuseStrategy;
-import org.apache.http.HttpClientConnection;
 import org.apache.http.HttpException;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
-import org.apache.http.HttpServerConnection;
 import org.apache.http.impl.DefaultBHttpClientConnection;
 import org.apache.http.impl.DefaultBHttpServerConnection;
 import org.apache.http.impl.DefaultConnectionReuseStrategy;
@@ -113,9 +111,15 @@ public class ElementalReverseProxy {
                 final HttpResponse response,
                 final HttpContext context) throws HttpException, IOException {
 
-            final HttpClientConnection conn = (HttpClientConnection) context.getAttribute(
+            final DefaultBHttpClientConnection conn = (DefaultBHttpClientConnection) context.getAttribute(
                     HTTP_OUT_CONN);
 
+            if (!conn.isOpen() || conn.isStale()) {
+                final Socket outsocket = new Socket(this.target.getHostName(), this.target.getPort()
>= 0 ? this.target.getPort() : 80);
+                conn.bind(outsocket);
+                System.out.println("Outgoing connection to " + outsocket.getInetAddress());
+            }
+
             context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
             context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
 
@@ -209,10 +213,7 @@ public class ElementalReverseProxy {
                     inconn.bind(insocket);
 
                     // Set up outgoing HTTP connection
-                    final Socket outsocket = new Socket(this.target.getHostName(), this.target.getPort()
>= 0 ? this.target.getPort() : 80);
                     final DefaultBHttpClientConnection outconn = new DefaultBHttpClientConnection(bufsize);
-                    outconn.bind(outsocket);
-                    System.out.println("Outgoing connection to " + outsocket.getInetAddress());
 
                     // Start worker thread
                     final Thread t = new ProxyThread(this.httpService, inconn, outconn);
@@ -232,13 +233,13 @@ public class ElementalReverseProxy {
     static class ProxyThread extends Thread {
 
         private final HttpService httpservice;
-        private final HttpServerConnection inconn;
-        private final HttpClientConnection outconn;
+        private final DefaultBHttpServerConnection inconn;
+        private final DefaultBHttpClientConnection outconn;
 
         public ProxyThread(
                 final HttpService httpservice,
-                final HttpServerConnection inconn,
-                final HttpClientConnection outconn) {
+                final DefaultBHttpServerConnection inconn,
+                final DefaultBHttpClientConnection outconn) {
             super();
             this.httpservice = httpservice;
             this.inconn = inconn;


Mime
View raw message