You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2016/12/11 15:50:06 UTC

svn commit: r1773583 - in /httpcomponents/httpcore/trunk: httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/ httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/ httpcore5/src/main/java/org/apache/hc/core5/http/impl...

Author: olegk
Date: Sun Dec 11 15:50:05 2016
New Revision: 1773583

URL: http://svn.apache.org/viewvc?rev=1773583&view=rev
Log:
HTTPCORE-424: added ConnPool policy parameter to control connection re-use policy

Added:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/pool/ConnPoolPolicy.java   (with props)
Modified:
    httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2RequesterBootstrap.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/RequesterBootstrap.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/RequesterBootstrap.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java

Modified: httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2RequesterBootstrap.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2RequesterBootstrap.java?rev=1773583&r1=1773582&r2=1773583&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2RequesterBootstrap.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2RequesterBootstrap.java Sun Dec 11 15:50:05 2016
@@ -47,6 +47,7 @@ import org.apache.hc.core5.http2.impl.ni
 import org.apache.hc.core5.http2.impl.nio.Http2StreamListener;
 import org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy;
 import org.apache.hc.core5.pool.ConnPoolListener;
+import org.apache.hc.core5.pool.ConnPoolPolicy;
 import org.apache.hc.core5.pool.StrictConnPool;
 import org.apache.hc.core5.reactor.IOReactorConfig;
 import org.apache.hc.core5.util.Args;
@@ -66,6 +67,7 @@ public class H2RequesterBootstrap {
     private int maxTotal;
     private long timeToLive;
     private TimeUnit timeUnit;
+    private ConnPoolPolicy connPoolPolicy;
     private TlsStrategy tlsStrategy;
     private ExceptionListener exceptionListener;
     private ConnectionListener connectionListener;
@@ -129,6 +131,14 @@ public class H2RequesterBootstrap {
     }
 
     /**
+     * Assigns {@link ConnPoolPolicy} instance.
+     */
+    public final H2RequesterBootstrap setConnPoolPolicy(final ConnPoolPolicy connPoolPolicy) {
+        this.connPoolPolicy = connPoolPolicy;
+        return this;
+    }
+
+    /**
      * Assigns {@link TlsStrategy} instance.
      */
     public final H2RequesterBootstrap setTlsStrategy(final TlsStrategy tlsStrategy) {
@@ -188,6 +198,7 @@ public class H2RequesterBootstrap {
                 defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20,
                 maxTotal > 0 ? maxTotal : 50,
                 timeToLive, timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS,
+                connPoolPolicy,
                 connPoolListener);
         final AsyncPushConsumerRegistry pushConsumerRegistry = new AsyncPushConsumerRegistry();
         for (PushConsumerEntry entry: pushConsumerList) {

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/RequesterBootstrap.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/RequesterBootstrap.java?rev=1773583&r1=1773582&r2=1773583&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/RequesterBootstrap.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/RequesterBootstrap.java Sun Dec 11 15:50:05 2016
@@ -41,6 +41,7 @@ import org.apache.hc.core5.http.io.HttpC
 import org.apache.hc.core5.http.io.HttpConnectionFactory;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
 import org.apache.hc.core5.pool.ConnPoolListener;
+import org.apache.hc.core5.pool.ConnPoolPolicy;
 import org.apache.hc.core5.pool.StrictConnPool;
 
 /**
@@ -56,6 +57,7 @@ public class RequesterBootstrap {
     private int maxTotal;
     private long timeToLive;
     private TimeUnit timeUnit;
+    private ConnPoolPolicy connPoolPolicy;
     private Http1StreamListener streamListener;
     private ConnPoolListener<HttpHost> connPoolListener;
 
@@ -108,6 +110,11 @@ public class RequesterBootstrap {
         return this;
     }
 
+    public final RequesterBootstrap setConnPoolPolicy(final ConnPoolPolicy connPoolPolicy) {
+        this.connPoolPolicy = connPoolPolicy;
+        return this;
+    }
+
     public final RequesterBootstrap setStreamListener(final Http1StreamListener streamListener) {
         this.streamListener = streamListener;
         return this;
@@ -127,6 +134,7 @@ public class RequesterBootstrap {
                 defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20,
                 maxTotal > 0 ? maxTotal : 50,
                 timeToLive, timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS,
+                connPoolPolicy,
                 connPoolListener);
         return new HttpRequester(
                 requestExecutor,

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/RequesterBootstrap.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/RequesterBootstrap.java?rev=1773583&r1=1773582&r2=1773583&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/RequesterBootstrap.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/RequesterBootstrap.java Sun Dec 11 15:50:05 2016
@@ -44,6 +44,7 @@ import org.apache.hc.core5.http.nio.ssl.
 import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
 import org.apache.hc.core5.pool.ConnPoolListener;
+import org.apache.hc.core5.pool.ConnPoolPolicy;
 import org.apache.hc.core5.pool.StrictConnPool;
 import org.apache.hc.core5.reactor.IOReactorConfig;
 
@@ -60,6 +61,7 @@ public class RequesterBootstrap {
     private int maxTotal;
     private long timeToLive;
     private TimeUnit timeUnit;
+    private ConnPoolPolicy connPoolPolicy;
     private TlsStrategy tlsStrategy;
     private ExceptionListener exceptionListener;
     private ConnectionListener connectionListener;
@@ -122,6 +124,14 @@ public class RequesterBootstrap {
     }
 
     /**
+     * Assigns {@link ConnPoolPolicy} instance.
+     */
+    public final RequesterBootstrap setConnPoolPolicy(final ConnPoolPolicy connPoolPolicy) {
+        this.connPoolPolicy = connPoolPolicy;
+        return this;
+    }
+
+    /**
      * Assigns {@link TlsStrategy} instance.
      */
     public final RequesterBootstrap setTlsStrategy(final TlsStrategy tlsStrategy) {
@@ -166,6 +176,7 @@ public class RequesterBootstrap {
                 defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20,
                 maxTotal > 0 ? maxTotal : 50,
                 timeToLive, timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS,
+                connPoolPolicy,
                 connPoolListener);
         final ClientHttp1IOEventHandlerFactory ioEventHandlerFactory = new ClientHttp1IOEventHandlerFactory(
                 httpProcessor != null ? httpProcessor : HttpProcessors.client(),

Added: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/pool/ConnPoolPolicy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/pool/ConnPoolPolicy.java?rev=1773583&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/pool/ConnPoolPolicy.java (added)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/pool/ConnPoolPolicy.java Sun Dec 11 15:50:05 2016
@@ -0,0 +1,44 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.core5.pool;
+
+/**
+ * @since 5.0
+ */
+public enum ConnPoolPolicy{
+
+    /**
+     * Re-use as few connections as possible making it possible for connections to become idle and expire.
+     */
+    LIFO,
+
+    /**
+     * Re-use all connections equally preventing them from becoming idle and expiring.
+     */
+    FIFO
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/pool/ConnPoolPolicy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/pool/ConnPoolPolicy.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/pool/ConnPoolPolicy.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java?rev=1773583&r1=1773582&r2=1773583&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java Sun Dec 11 15:50:05 2016
@@ -64,7 +64,8 @@ public class StrictConnPool<T, C extends
 
     private final long timeToLive;
     private final TimeUnit timeUnit;
-    private ConnPoolListener<T> connPoolListener;
+    private final ConnPoolListener<T> connPoolListener;
+    private final ConnPoolPolicy policy;
     private final Map<T, RoutePool<T, C>> routeToPool;
     private final LinkedList<LeaseRequest<T, C>> leasingRequests;
     private final Set<PoolEntry<T, C>> leased;
@@ -85,6 +86,7 @@ public class StrictConnPool<T, C extends
             final int maxTotal,
             final long timeToLive,
             final TimeUnit timeUnit,
+            final ConnPoolPolicy policy,
             final ConnPoolListener<T> connPoolListener) {
         super();
         Args.positive(defaultMaxPerRoute, "Max per route value");
@@ -92,6 +94,7 @@ public class StrictConnPool<T, C extends
         this.timeToLive = timeToLive;
         this.timeUnit = timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS;
         this.connPoolListener = connPoolListener;
+        this.policy = policy != null ? policy : ConnPoolPolicy.LIFO;
         this.routeToPool = new HashMap<>();
         this.leasingRequests = new LinkedList<>();
         this.leased = new HashSet<>();
@@ -105,7 +108,7 @@ public class StrictConnPool<T, C extends
     }
 
     public StrictConnPool(final int defaultMaxPerRoute, final int maxTotal) {
-        this(defaultMaxPerRoute, maxTotal, 0, TimeUnit.MILLISECONDS, null);
+        this(defaultMaxPerRoute, maxTotal, 0, TimeUnit.MILLISECONDS, ConnPoolPolicy.LIFO, null);
     }
 
     public boolean isShutdown() {
@@ -197,7 +200,16 @@ public class StrictConnPool<T, C extends
                 final boolean keepAlive = entry.hasConnection() && reusable;
                 pool.free(entry, keepAlive);
                 if (keepAlive) {
-                    this.available.addFirst(entry);
+                    switch (policy) {
+                        case LIFO:
+                            this.available.addFirst(entry);
+                            break;
+                        case FIFO:
+                            this.available.addLast(entry);
+                            break;
+                        default:
+                            throw new IllegalStateException("Unexpected ConnPoolPolicy value: " + policy);
+                    }
                     if (this.connPoolListener != null) {
                         this.connPoolListener.onRelease(entry.getRoute(), this);
                     }