hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject [06/50] httpcomponents-core git commit: HTTPCORE-433: moved expensive connection validation code outside the pool lock
Date Tue, 09 May 2017 20:01:32 GMT
HTTPCORE-433: moved expensive connection validation code outside the pool lock

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpcore/branches/4.4.x@1770883
13f79535-47bb-0310-9956-ffa450edef68


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

Branch: refs/heads/4.4.x
Commit: 2af8359060e5ee534243003210c43e43197b5ca2
Parents: f6b4a9d
Author: Oleg Kalnichevski <olegk@apache.org>
Authored: Tue Nov 22 20:30:32 2016 +0000
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Tue Nov 22 20:30:32 2016 +0000

----------------------------------------------------------------------
 .../org/apache/http/pool/AbstractConnPool.java  | 47 ++++++++++----------
 .../java/org/apache/http/pool/PoolEntry.java    |  3 +-
 .../java/org/apache/http/pool/TestConnPool.java |  4 +-
 3 files changed, 27 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/2af83590/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java
----------------------------------------------------------------------
diff --git a/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java b/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java
index 51b0453..d34cf24 100644
--- a/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java
+++ b/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java
@@ -234,22 +234,30 @@ public abstract class AbstractConnPool<T, C, E extends PoolEntry<T,
C>>
 
             @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, C, E extends PoolEntry<T,
C>>
 
         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, C, E extends PoolEntry<T,
C>>
                     }
                     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, C, E extends PoolEntry<T,
C>>
                     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;
                 }
             }

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/2af83590/httpcore/src/main/java/org/apache/http/pool/PoolEntry.java
----------------------------------------------------------------------
diff --git a/httpcore/src/main/java/org/apache/http/pool/PoolEntry.java b/httpcore/src/main/java/org/apache/http/pool/PoolEntry.java
index e334e24..31b8ee2 100644
--- a/httpcore/src/main/java/org/apache/http/pool/PoolEntry.java
+++ b/httpcore/src/main/java/org/apache/http/pool/PoolEntry.java
@@ -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 {

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/2af83590/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java
----------------------------------------------------------------------
diff --git a/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java b/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java
index 24c2fa1..75ebb26 100644
--- a/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java
+++ b/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java
@@ -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);


Mime
View raw message