geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickmcgu...@apache.org
Subject svn commit: r612419 - in /geronimo/sandbox/AsyncHttpClient/src: main/java/org/apache/ahc/ main/java/org/apache/ahc/codec/ test/java/org/apache/ahc/
Date Wed, 16 Jan 2008 11:07:40 GMT
Author: rickmcguire
Date: Wed Jan 16 03:07:36 2008
New Revision: 612419

URL: http://svn.apache.org/viewvc?rev=612419&view=rev
Log:
GERONIMO-3749 Global session cache can cause multiple client instances to reuse incorrectly
configured connections.


Modified:
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/SessionCache.java
    geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ConnectionReuseTest.java

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java?rev=612419&r1=612418&r2=612419&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java (original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java Wed
Jan 16 03:07:36 2008
@@ -60,9 +60,6 @@
  */
 public class AsyncHttpClient {
 
-    /** The Constant DEFAULT_REUSE_CONNECTION. */
-    public static final boolean DEFAULT_REUSE_CONNECTION = false;
-    
     /** The Constant DEFAULT_CONNECTION_TIMEOUT. */
     public static final int DEFAULT_CONNECTION_TIMEOUT = 30000;
 
@@ -117,10 +114,10 @@
     
     /** The HttpIoHandler handler. */
     private final HttpIoHandler handler;
-
-    /** connection reuse option */
-    private volatile boolean reuseConnection = DEFAULT_REUSE_CONNECTION;
     
+    /** The cache for session reuse */
+    private SessionCache sessionCache; 
+
     /** The Reuse Address Socket Parameter. */
     private boolean reuseAddress = DEFAULT_REUSE_ADDRESS;
 
@@ -154,24 +151,6 @@
     public static final String EVENT_THREAD_POOL_FILTER = "eventThreadPoolFilter";
 
     /**
-     * Returns if it reuses established connections for more requests.
-     * 
-     * @return true if it reuses connections
-     */
-    public boolean isReuseConnection() {
-        return reuseConnection;
-    }
-    
-    /**
-     * Sets if it should reuse established connections for more requests.
-     * 
-     * @param reuseConnection the new value
-     */
-    public void setReuseConnection(boolean reuseConnection) {
-        this.reuseConnection = reuseConnection;
-    }
-    
-    /**
      * Checks if is reuse address.
      *
      * @return true, if is reuse address
@@ -410,7 +389,31 @@
         connector.setWorkerTimeout(1);
 
     }
-
+    
+    /**
+     * Set the session cache that should be used for 
+     * connection reuse.
+     * 
+     * @param cache  The new session cache.  If null, this will disable
+     *               future connection reuse.
+     */
+    public void setSessionCache(SessionCache cache) {
+        sessionCache = cache; 
+        // our I/O Handler instance needs to be fitted with the same 
+        // cache
+        handler.setSessionCache(cache); 
+    }
+    
+    /**
+     * Retrieve the session cache used for storing 
+     * connections for reuse. 
+     * 
+     * @return The current session cache for the client. 
+     */
+    public SessionCache getSessionCache() {
+        return sessionCache; 
+    }
+    
     /**
      * Sends a request.  The call is non-blocking, and returns a future object
      * with which the caller can synchronize on the completion of the request.
@@ -461,7 +464,8 @@
         // connection first; if not, always open a new one
         ConnectFuture future = null;
         if (!message.isProxyEnabled()) {
-            if (reuseConnection) {
+            SessionCache cache = getSessionCache(); 
+            if (cache != null) {
                 future = getCachedConnection(message);
             } else {
                 // add the Connection close header explicitly
@@ -521,11 +525,11 @@
      * 
      * @param message The message we're sending.
      * 
-     * @return A cached connection.  This returns null if there's 
+     * @return A cached connection.  This returns null if there's
      *         no available connection for the target location.
      */
     private ConnectFuture getCachedConnection(HttpRequestMessage message) {
-        IoSession cached = SessionCache.getInstance().getActiveSession(message);
+        IoSession cached = sessionCache.getActiveSession(message);
         if (cached == null) {
             return null;
         }

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java?rev=612419&r1=612418&r2=612419&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
(original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
Wed Jan 16 03:07:36 2008
@@ -62,6 +62,11 @@
      * The scheduler service to handle timeouts.
      */
     private final ScheduledExecutorService scheduler;
+    
+    /** 
+     * The session cache used for reusable connections 
+     */
+    private SessionCache sessionCache; 
 
     /**
      * Instantiates a new HttpIoHandler with a new a single-threaded executor.
@@ -78,6 +83,27 @@
     public HttpIoHandler(ScheduledExecutorService scheduler) {
         this.scheduler = scheduler;
     }
+    
+    /**
+     * Set the session cache that should be used for 
+     * connection reuse.
+     * 
+     * @param cache  The new session cache.  If null, this will disable
+     *               future connection reuse.
+     */
+    public void setSessionCache(SessionCache cache) {
+        sessionCache = cache; 
+    }
+    
+    /**
+     * Retrieve the session cache used for storing 
+     * connections for reuse. 
+     * 
+     * @return The current session cache for the client. 
+     */
+    public SessionCache getSessionCache() {
+        return sessionCache; 
+    }
 
     /**
      * Destroys the handler and shuts down the scheduler.
@@ -128,8 +154,13 @@
             //Send the redirect
             client.sendRequest(request);
 
-            // cache the session before we return
-            SessionCache.getInstance().cacheSession(ioSession);
+            // if we've been provided with a cache, put this session into 
+            // the cache. 
+            SessionCache cache = getSessionCache(); 
+            if (cache != null) {
+                // cache the session before we return
+                cache.cacheSession(ioSession);
+            }
             return;
         }
 
@@ -155,8 +186,13 @@
                 request.setAuthCount(authCount);
                 client.sendRequest(request);
 
-                // cache the session before we return
-                SessionCache.getInstance().cacheSession(ioSession);
+                // if we've been provided with a cache, put this session into 
+                // the cache. 
+                SessionCache cache = getSessionCache(); 
+                if (cache != null) {
+                    // cache the session before we return
+                    cache.cacheSession(ioSession);
+                }
                 return;
             }
         }
@@ -167,8 +203,13 @@
         ResponseFuture result = request.getResponseFuture();
         result.set(response);
 
-        // cache the session before we return
-        SessionCache.getInstance().cacheSession(ioSession);
+        // if we've been provided with a cache, put this session into 
+        // the cache. 
+        SessionCache cache = getSessionCache(); 
+        if (cache != null) {
+            // cache the session before we return
+            cache.cacheSession(ioSession);
+        }
     }
 
     /**
@@ -202,8 +243,14 @@
     public void sessionClosed(IoSession ioSession) throws Exception {
         //Clean up if any in-proccess decoding was occurring
         ioSession.removeAttribute(CURRENT_RESPONSE);
-        // remove it from the cache
-        SessionCache.getInstance().removeSession(ioSession);
+        
+        // if we've been provided with a cache, remove this session from 
+        // the cache. 
+        SessionCache cache = getSessionCache(); 
+        if (cache != null) {
+            // cache the session before we return
+            cache.removeSession(ioSession);
+        }
         HttpRequestMessage request = (HttpRequestMessage) ioSession.getAttribute(CURRENT_REQUEST);
         cancelTasks(request);
         AsyncHttpClientCallback callback = request.getCallback();

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/SessionCache.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/SessionCache.java?rev=612419&r1=612418&r2=612419&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/SessionCache.java
(original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/SessionCache.java
Wed Jan 16 03:07:36 2008
@@ -32,19 +32,10 @@
  * the host and the port.  This class is thread safe.
  */
 public final class SessionCache {
-    private static final SessionCache theInstance = new SessionCache();
-    
     private final ConcurrentMap<String,Queue<IoSession>> cachedSessions = 
             new ConcurrentHashMap<String,Queue<IoSession>>();
     
-    /**
-     * Returns a singleton instance of the session cache.
-     */
-    public static SessionCache getInstance() {
-        return theInstance;
-    }
-    
-    private SessionCache() {}
+    public SessionCache() {}
 
     /**
      * Returns an IoSession that is connected and considered usable.  Note that

Modified: geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ConnectionReuseTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ConnectionReuseTest.java?rev=612419&r1=612418&r2=612419&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ConnectionReuseTest.java
(original)
+++ geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ConnectionReuseTest.java
Wed Jan 16 03:07:36 2008
@@ -25,11 +25,14 @@
 
 import org.apache.ahc.codec.HttpRequestMessage;
 import org.apache.ahc.codec.HttpResponseMessage;
+import org.apache.ahc.codec.SessionCache;        
 
 public class ConnectionReuseTest extends AbstractTest {
     // variable that keeps count of session close's
     private final AtomicInteger closeCount = new AtomicInteger(0);
     
+    private final SessionCache cache = new SessionCache(); 
+    
     // It is important that this test case contains these methods in this order.
     // It is because to test connection reuse we need to keep the embedded 
     // server running while connections are reused.  However, AbstractTest 
@@ -107,7 +110,9 @@
         request.setParameter("TEST1", "Test One");
         request.setParameter("TEST2", "Test Two");
         AsyncHttpClient ahc = new AsyncHttpClient();
-        ahc.setReuseConnection(reuseConnection);
+        if (reuseConnection) {
+            ahc.setSessionCache(cache);  
+        }
         ahc.setTcpNoDelay(true);
         return ahc.sendRequest(request);
     }



Mime
View raw message