hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1599497 - in /httpcomponents/httpcore/trunk/httpcore/src: main/java/org/apache/http/impl/pool/BasicConnPool.java main/java/org/apache/http/pool/AbstractConnPool.java test/java/org/apache/http/pool/TestConnPool.java
Date Tue, 03 Jun 2014 12:07:45 GMT
Author: olegk
Date: Tue Jun  3 12:07:45 2014
New Revision: 1599497

URL: http://svn.apache.org/r1599497
Log:
HTTPCLIENT-1493: Conditional stale connection checking of pooled connections

Modified:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/pool/BasicConnPool.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/pool/BasicConnPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/pool/BasicConnPool.java?rev=1599497&r1=1599496&r2=1599497&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/pool/BasicConnPool.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/pool/BasicConnPool.java
Tue Jun  3 12:07:45 2014
@@ -86,4 +86,9 @@ public class BasicConnPool extends Abstr
         return new BasicPoolEntry(Long.toString(COUNTER.getAndIncrement()), host, conn);
     }
 
+    @Override
+    protected boolean validate(final BasicPoolEntry entry) {
+        return !entry.getConnection().isStale();
+    }
+
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java?rev=1599497&r1=1599496&r2=1599497&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java
Tue Jun  3 12:07:45 2014
@@ -75,6 +75,7 @@ public abstract class AbstractConnPool<T
     private volatile boolean isShutDown;
     private volatile int defaultMaxPerRoute;
     private volatile int maxTotal;
+    private volatile int validateAfterInactivity;
 
     public AbstractConnPool(
             final ConnFactory<T, C> connFactory,
@@ -109,6 +110,13 @@ public abstract class AbstractConnPool<T
     protected void onRelease(final E entry) {
     }
 
+    /**
+     * @since 4.4
+     */
+    protected boolean validate(final E entry) {
+        return true;
+    }
+
     public boolean isShutdown() {
         return this.isShutDown;
     }
@@ -226,6 +234,13 @@ public abstract class AbstractConnPool<T
                     if (entry == null) {
                         break;
                     }
+                    if (this.validateAfterInactivity > 0) {
+                        if (entry.getUpdated() + this.validateAfterInactivity <= System.currentTimeMillis())
{
+                            if (!validate(entry)) {
+                                entry.close();
+                            }
+                        }
+                    }
                     if (entry.isClosed() || entry.isExpired(System.currentTimeMillis()))
{
                         entry.close();
                         this.available.remove(entry);
@@ -543,6 +558,20 @@ public abstract class AbstractConnPool<T
         });
     }
 
+    /**
+     * @since 4.4
+     */
+    public int getValidateAfterInactivity() {
+        return this.validateAfterInactivity;
+    }
+
+    /**
+     * @since 4.4
+     */
+    public void setValidateAfterInactivity(final int ms) {
+        this.validateAfterInactivity = ms;
+    }
+
     @Override
     public String toString() {
         final StringBuilder buffer = new StringBuilder();

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java?rev=1599497&r1=1599496&r2=1599497&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java
Tue Jun  3 12:07:45 2014
@@ -85,6 +85,10 @@ public class TestConnPool {
             return new LocalPoolEntry(route, conn);
         }
 
+        @Override
+        protected boolean validate(final LocalPoolEntry entry) {
+            return !entry.getConnection().isStale();
+        }
     }
 
     @Test
@@ -792,4 +796,65 @@ public class TestConnPool {
         pool.release(new LocalPoolEntry("somehost", Mockito.mock(HttpConnection.class)),
true);
     }
 
+    @Test
+    public void testValidateConnectionNotStale() throws Exception {
+        final HttpConnection conn = Mockito.mock(HttpConnection.class);
+        Mockito.when(conn.isOpen()).thenReturn(true);
+        Mockito.when(conn.isStale()).thenReturn(false);
+
+        final LocalConnFactory connFactory = Mockito.mock(LocalConnFactory.class);
+        Mockito.when(connFactory.create(Mockito.eq("somehost"))).thenReturn(conn);
+
+        final LocalConnPool pool = new LocalConnPool(connFactory, 2, 10);
+        pool.setValidateAfterInactivity(5);
+
+        final Future<LocalPoolEntry> future1 = pool.lease("somehost", null);
+        final LocalPoolEntry entry1 = future1.get(1, TimeUnit.SECONDS);
+        Assert.assertNotNull(entry1);
+
+        pool.release(entry1, true);
+
+        Thread.sleep(10);
+
+        final Future<LocalPoolEntry> future2 = pool.lease("somehost", null);
+        final LocalPoolEntry entry2 = future2.get(1, TimeUnit.SECONDS);
+        Assert.assertNotNull(entry2);
+        Assert.assertSame(entry1, entry2);
+
+        Mockito.verify(conn, Mockito.times(1)).isStale();
+    }
+
+    @Test
+    public void testValidateConnectionStale() throws Exception {
+        final HttpConnection conn = Mockito.mock(HttpConnection.class);
+        Mockito.when(conn.isOpen()).thenReturn(true);
+        Mockito.when(conn.isStale()).thenReturn(false);
+
+        final LocalConnFactory connFactory = Mockito.mock(LocalConnFactory.class);
+        Mockito.when(connFactory.create(Mockito.eq("somehost"))).thenReturn(conn);
+
+        final LocalConnPool pool = new LocalConnPool(connFactory, 2, 10);
+        pool.setValidateAfterInactivity(5);
+
+        final Future<LocalPoolEntry> future1 = pool.lease("somehost", null);
+        final LocalPoolEntry entry1 = future1.get(1, TimeUnit.SECONDS);
+        Assert.assertNotNull(entry1);
+
+        pool.release(entry1, true);
+
+        Thread.sleep(10);
+
+        Mockito.verify(connFactory, Mockito.times(1)).create("somehost");
+        Mockito.when(conn.isStale()).thenReturn(true);
+
+        final Future<LocalPoolEntry> future2 = pool.lease("somehost", null);
+        final LocalPoolEntry entry2 = future2.get(1, TimeUnit.SECONDS);
+        Assert.assertNotNull(entry2);
+        Assert.assertNotSame(entry1, entry2);
+
+        Mockito.verify(conn, Mockito.times(1)).isStale();
+        Mockito.verify(conn, Mockito.times(1)).close();
+        Mockito.verify(connFactory, Mockito.times(2)).create("somehost");
+    }
+
 }



Mime
View raw message