Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 1E8A8200CBA for ; Mon, 3 Jul 2017 19:19:23 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 1A845160BEC; Mon, 3 Jul 2017 17:19:23 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 60B4A160BD2 for ; Mon, 3 Jul 2017 19:19:22 +0200 (CEST) Received: (qmail 62622 invoked by uid 500); 3 Jul 2017 17:19:21 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 62613 invoked by uid 99); 3 Jul 2017 17:19:21 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 03 Jul 2017 17:19:21 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id C4DF9DFEF5; Mon, 3 Jul 2017 17:19:20 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: olegk@apache.org To: commits@hc.apache.org Message-Id: <80952cbe85494ebcba2b915dbfa29a24@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: httpcomponents-core git commit: HTTPCORE-475: Elemental Reverse Proxy example to handle stale outgoing connections more gracefully Date: Mon, 3 Jul 2017 17:19:20 +0000 (UTC) archived-at: Mon, 03 Jul 2017 17:19:23 -0000 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 Authored: Mon Jul 3 19:07:24 2017 +0200 Committer: Oleg Kalnichevski 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;