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/11/22 20:30:32 UTC

svn commit: r1770883 - in /httpcomponents/httpcore/branches/4.4.x/httpcore/src: main/java/org/apache/http/pool/AbstractConnPool.java main/java/org/apache/http/pool/PoolEntry.java test/java/org/apache/http/pool/TestConnPool.java

Author: olegk
Date: Tue Nov 22 20:30:32 2016
New Revision: 1770883

URL: http://svn.apache.org/viewvc?rev=1770883&view=rev
Log:
HTTPCORE-433: moved expensive connection validation code outside the pool lock

Modified:
    httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java
    httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/pool/PoolEntry.java
    httpcomponents/httpcore/branches/4.4.x/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java

Modified: httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java?rev=1770883&r1=1770882&r2=1770883&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java (original)
+++ httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java Tue Nov 22 20:30:32 2016
@@ -234,22 +234,30 @@ public abstract class AbstractConnPool<T
 
             @Override
             public E get(final long timeout, final TimeUnit tunit) throws InterruptedException, ExecutionException, TimeoutException {
-                final E local = entry;
-                if (local != null) {
-                    return local;
+                if (entry != null) {
+                    return entry;
                 }
                 synchronized (this) {
                     try {
-                        if (entry != null) {
+                        for (;;) {
+                            final E leasedEntry = getPoolEntryBlocking(route, state, timeout, tunit, this);
+                            if (validateAfterInactivity > 0)  {
+                                if (leasedEntry.getUpdated() + validateAfterInactivity <= System.currentTimeMillis()) {
+                                    if (!validate(leasedEntry)) {
+                                        leasedEntry.close();
+                                        release(leasedEntry, false);
+                                        continue;
+                                    }
+                                }
+                            }
+                            entry = leasedEntry;
+                            done = true;
+                            onLease(entry);
+                            if (callback != null) {
+                                callback.completed(entry);
+                            }
                             return entry;
                         }
-                        entry = getPoolEntryBlocking(route, state, timeout, tunit, this);
-                        done = true;
-                        onLease(entry);
-                        if (callback != null) {
-                            callback.completed(entry);
-                        }
-                        return entry;
                     } catch (IOException ex) {
                         done = true;
                         if (callback != null) {
@@ -290,15 +298,13 @@ public abstract class AbstractConnPool<T
 
         Date deadline = null;
         if (timeout > 0) {
-            deadline = new Date
-                (System.currentTimeMillis() + tunit.toMillis(timeout));
+            deadline = new Date (System.currentTimeMillis() + tunit.toMillis(timeout));
         }
-
         this.lock.lock();
         try {
             final RouteSpecificPool<T, C, E> pool = getPool(route);
-            E entry = null;
-            while (entry == null) {
+            E entry;
+            for (;;) {
                 Asserts.check(!this.isShutDown, "Connection pool shut down");
                 for (;;) {
                     entry = pool.getFree(state);
@@ -307,12 +313,6 @@ public abstract class AbstractConnPool<T
                     }
                     if (entry.isExpired(System.currentTimeMillis())) {
                         entry.close();
-                    } else if (this.validateAfterInactivity > 0) {
-                        if (entry.getUpdated() + this.validateAfterInactivity <= System.currentTimeMillis()) {
-                            if (!validate(entry)) {
-                                entry.close();
-                            }
-                        }
                     }
                     if (entry.isClosed()) {
                         this.available.remove(entry);
@@ -389,8 +389,7 @@ public abstract class AbstractConnPool<T
                     this.pending.remove(future);
                 }
                 // check for spurious wakeup vs. timeout
-                if (!success && (deadline != null) &&
-                    (deadline.getTime() <= System.currentTimeMillis())) {
+                if (!success && (deadline != null && deadline.getTime() <= System.currentTimeMillis())) {
                     break;
                 }
             }

Modified: httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/pool/PoolEntry.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/pool/PoolEntry.java?rev=1770883&r1=1770882&r2=1770883&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/pool/PoolEntry.java (original)
+++ httpcomponents/httpcore/branches/4.4.x/httpcore/src/main/java/org/apache/http/pool/PoolEntry.java Tue Nov 22 20:30:32 2016
@@ -28,8 +28,8 @@ package org.apache.http.pool;
 
 import java.util.concurrent.TimeUnit;
 
-import org.apache.http.annotation.ThreadingBehavior;
 import org.apache.http.annotation.Contract;
+import org.apache.http.annotation.ThreadingBehavior;
 import org.apache.http.util.Args;
 
 /**
@@ -83,6 +83,7 @@ public abstract class PoolEntry<T, C> {
         this.route = route;
         this.conn = conn;
         this.created = System.currentTimeMillis();
+        this.updated = this.created;
         if (timeToLive > 0) {
             this.validityDeadline = this.created + tunit.toMillis(timeToLive);
         } else {

Modified: httpcomponents/httpcore/branches/4.4.x/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/4.4.x/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java?rev=1770883&r1=1770882&r2=1770883&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/4.4.x/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java (original)
+++ httpcomponents/httpcore/branches/4.4.x/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java Tue Nov 22 20:30:32 2016
@@ -806,7 +806,7 @@ public class TestConnPool {
         Mockito.when(connFactory.create(Mockito.eq("somehost"))).thenReturn(conn);
 
         final LocalConnPool pool = new LocalConnPool(connFactory, 2, 10);
-        pool.setValidateAfterInactivity(5);
+        pool.setValidateAfterInactivity(100);
 
         final Future<LocalPoolEntry> future1 = pool.lease("somehost", null);
         final LocalPoolEntry entry1 = future1.get(1, TimeUnit.SECONDS);
@@ -814,7 +814,7 @@ public class TestConnPool {
 
         pool.release(entry1, true);
 
-        Thread.sleep(10);
+        Thread.sleep(150);
 
         final Future<LocalPoolEntry> future2 = pool.lease("somehost", null);
         final LocalPoolEntry entry2 = future2.get(1, TimeUnit.SECONDS);